summaryrefslogtreecommitdiff
path: root/c/ffi_obj.c
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2015-06-02 19:22:45 +0200
committerArmin Rigo <arigo@tunes.org>2015-06-02 19:22:45 +0200
commit784ccaeb4e8d92dcbd0ebf53898549a91abd2c70 (patch)
tree3d49532d9be78c6b36c295a8fc6990288803de8f /c/ffi_obj.c
parent5c4f9ba700b3466717c5ddfc9011bf94dea8d32d (diff)
downloadcffi-784ccaeb4e8d92dcbd0ebf53898549a91abd2c70.tar.gz
In the type parser, escape error messages and don't display the input
type if too huge
Diffstat (limited to 'c/ffi_obj.c')
-rw-r--r--c/ffi_obj.c44
1 files changed, 35 insertions, 9 deletions
diff --git a/c/ffi_obj.c b/c/ffi_obj.c
index 134e303..911b654 100644
--- a/c/ffi_obj.c
+++ b/c/ffi_obj.c
@@ -140,6 +140,38 @@ static PyObject *ffi_fetch_int_constant(FFIObject *ffi, char *name,
#define ACCEPT_ALL (ACCEPT_STRING | ACCEPT_CTYPE | ACCEPT_CDATA)
#define CONSIDER_FN_AS_FNPTR 8
+static CTypeDescrObject *_ffi_bad_type(FFIObject *ffi, char *input_text)
+{
+ size_t length = strlen(input_text);
+ char *extra;
+
+ if (length > 500) {
+ extra = "";
+ }
+ else {
+ char *p;
+ size_t i, num_spaces = ffi->info.error_location;
+ extra = alloca(length + num_spaces + 4);
+ p = extra;
+ *p++ = '\n';
+ for (i = 0; i < length; i++) {
+ if (' ' <= input_text[i] && input_text[i] < 0x7f)
+ *p++ = input_text[i];
+ else if (input_text[i] == '\t' || input_text[i] == '\n')
+ *p++ = ' ';
+ else
+ *p++ = '?';
+ }
+ *p++ = '\n';
+ memset(p, ' ', num_spaces);
+ p += num_spaces;
+ *p++ = '^';
+ *p++ = 0;
+ }
+ PyErr_Format(FFIError, "%s%s", ffi->info.error_message, extra);
+ return NULL;
+}
+
static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,
int accept)
{
@@ -153,15 +185,9 @@ static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,
if (x == NULL) {
char *input_text = PyText_AS_UTF8(arg);
int err, index = parse_c_type(&ffi->info, input_text);
- if (index < 0) {
- size_t num_spaces = ffi->info.error_location;
- char *spaces = alloca(num_spaces + 1);
- memset(spaces, ' ', num_spaces);
- spaces[num_spaces] = '\0';
- PyErr_Format(FFIError, "%s\n%s\n%s^", ffi->info.error_message,
- input_text, spaces);
- return NULL;
- }
+ if (index < 0)
+ return _ffi_bad_type(ffi, input_text);
+
x = realize_c_type_or_func(&ffi->types_builder,
ffi->info.output, index);
if (x == NULL)