From 5f420d7047749bf0c676864cb4f4fdfc636994c5 Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Sat, 8 Dec 2018 16:58:44 -0500 Subject: [PATCH] Fix number->size conversion --- src/calibre/gui2/tweak_book/editor/syntax/html.c | 16 ++++++++++++++-- src/tinycss/tokenizer.c | 11 ++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/tweak_book/editor/syntax/html.c b/src/calibre/gui2/tweak_book/editor/syntax/html.c index 319ca79ff2..bf45d92bde 100644 --- a/src/calibre/gui2/tweak_book/editor/syntax/html.c +++ b/src/calibre/gui2/tweak_book/editor/syntax/html.c @@ -386,6 +386,18 @@ html_init(PyObject *self, PyObject *args) { Py_RETURN_NONE; } +static inline long number_to_long(PyObject *number) { +#if PY_VERSION_HEX >= 0x03030000 + return PyLong_AsLong(number); +#else + if(PyInt_Check(number)) { + return PyInt_AS_LONG(number); + } else { + return PyLong_AsLong(number); + } +#endif +} + static PyObject* html_check_spelling(PyObject *self, PyObject *args) { PyObject *ans = NULL, *temp = NULL, *items = NULL, *text = NULL, *fmt = NULL, *locale = NULL, *sfmt = NULL, *_store_locale = NULL, *t = NULL, *utmp = NULL; @@ -410,9 +422,9 @@ html_check_spelling(PyObject *self, PyObject *args) { for (i = 0, j = 0; i < PyList_GET_SIZE(items); i++) { temp = PyList_GET_ITEM(items, i); - start = PyLong_AsLong(PyTuple_GET_ITEM(temp, 0)); + start = number_to_long(PyTuple_GET_ITEM(temp, 0)); if(start == -1 && PyErr_Occurred() != NULL) goto error; - length = PyLong_AsLong(PyTuple_GET_ITEM(temp, 1)); + length = number_to_long(PyTuple_GET_ITEM(temp, 1)); if(length == -1 && PyErr_Occurred() != NULL) goto error; temp = NULL; diff --git a/src/tinycss/tokenizer.c b/src/tinycss/tokenizer.c index d06e4a1200..ddb5201519 100644 --- a/src/tinycss/tokenizer.c +++ b/src/tinycss/tokenizer.c @@ -179,7 +179,9 @@ tokenize_init(PyObject *self, PyObject *args) { Py_INCREF(COMPILED_TOKEN_REGEXPS); Py_INCREF(UNICODE_UNESCAPE); Py_INCREF(NEWLINE_UNESCAPE); Py_INCREF(SIMPLE_UNESCAPE); Py_INCREF(FIND_NEWLINES); Py_INCREF(TOKEN_DISPATCH); Py_INCREF(COLON); Py_INCREF(SCOLON); Py_INCREF(LPAR); Py_INCREF(RPAR); Py_INCREF(LBRACE); Py_INCREF(RBRACE); Py_INCREF(LBOX); Py_INCREF(RBOX); Py_INCREF(DELIM_TOK); Py_INCREF(INTEGER); Py_INCREF(STRING_TOK); -#define SETCONST(x) x = PyLong_AsSsize_t(PyDict_GetItemString(cti, #x)) +#define SETCONST(x) do { (x) = PyNumber_AsSsize_t(PyDict_GetItemString(cti, #x), PyExc_OverflowError); \ + if((x) == -1 && PyErr_Occurred() != NULL) { return NULL; } \ + } while(0) SETCONST(BAD_COMMENT); SETCONST(BAD_STRING); SETCONST(PERCENTAGE); SETCONST(DIMENSION); SETCONST(ATKEYWORD); SETCONST(FUNCTION); SETCONST(COMMENT); SETCONST(NUMBER); SETCONST(STRING); SETCONST(IDENT); SETCONST(HASH); SETCONST(URI); Py_RETURN_NONE; @@ -278,7 +280,8 @@ tokenize_flat(PyObject *self, PyObject *args) { if (match != Py_None) { css_value = PyObject_CallMethod(match, "group", NULL); if (css_value == NULL) { goto error; } - type_ = PyLong_AsSsize_t(PyTuple_GET_ITEM(item, 0)); + type_ = PyNumber_AsSsize_t(PyTuple_GET_ITEM(item, 0), PyExc_OverflowError); + if(type_ == -1 && PyErr_Occurred() != NULL) { goto error; } type_name = PyTuple_GET_ITEM(item, 1); Py_INCREF(type_name); break; @@ -392,7 +395,9 @@ tokenize_flat(PyObject *self, PyObject *args) { line += PyList_Size(newlines); item = PyObject_CallMethod(PyList_GET_ITEM(newlines, PyList_Size(newlines) - 1), "end", NULL); if (item == NULL) { Py_DECREF(newlines); newlines = NULL; goto error; } - column = length - PyInt_AsSsize_t(item) + 1; + column = PyNumber_AsSsize_t(item, PyExc_OverflowError); + if(column == -1 && PyErr_Occurred()) { Py_DECREF(newlines); newlines = NULL; goto error; } + column = length - column + 1; Py_DECREF(item); item = NULL; } else column += length; Py_DECREF(newlines); newlines = NULL;