Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changes
1.26 (unreleased)
~~~~~~~~~~~~~~~~~

* Reject oversized datetime, UUID and parse mode values before narrowing them to
their internal representations.

* Add Windows ARM64 build (`PR #242`__), thanks to Nikhil Dabas

__ https://github.com/python-rapidjson/python-rapidjson/pull/242
Expand Down
14 changes: 7 additions & 7 deletions rapidjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ datetime_mode_format(unsigned mode) {


static inline bool
valid_datetime_mode(int mode) {
valid_datetime_mode(long mode) {
int format = datetime_mode_format(mode);
return (mode >= 0 && mode < DM_MAX
&& (format <= DM_UNIX_TIME)
Expand Down Expand Up @@ -1785,7 +1785,7 @@ load(PyObject* self, PyObject* args, PyObject* kwargs)
if (datetimeModeObj == Py_None) {
datetimeMode = DM_NONE;
} else if (PyLong_Check(datetimeModeObj)) {
int mode = PyLong_AsLong(datetimeModeObj);
long mode = PyLong_AsLong(datetimeModeObj);
if (!valid_datetime_mode(mode)) {
PyErr_SetString(PyExc_ValueError, "Invalid datetime_mode");
return NULL;
Expand All @@ -1808,7 +1808,7 @@ load(PyObject* self, PyObject* args, PyObject* kwargs)
if (uuidModeObj == Py_None) {
uuidMode = UM_NONE;
} else if (PyLong_Check(uuidModeObj)) {
int mode = PyLong_AsLong(uuidModeObj);
long mode = PyLong_AsLong(uuidModeObj);
if (mode < 0 || mode >= UM_MAX) {
PyErr_SetString(PyExc_ValueError, "Invalid uuid_mode");
return NULL;
Expand All @@ -1825,7 +1825,7 @@ load(PyObject* self, PyObject* args, PyObject* kwargs)
if (parseModeObj == Py_None) {
parseMode = PM_NONE;
} else if (PyLong_Check(parseModeObj)) {
int mode = PyLong_AsLong(parseModeObj);
long mode = PyLong_AsLong(parseModeObj);
if (mode < 0 || mode >= PM_MAX) {
PyErr_SetString(PyExc_ValueError, "Invalid parse_mode");
return NULL;
Expand Down Expand Up @@ -2223,7 +2223,7 @@ decoder_new(PyTypeObject* type, PyObject* args, PyObject* kwargs)
if (datetimeModeObj == Py_None) {
datetimeMode = DM_NONE;
} else if (PyLong_Check(datetimeModeObj)) {
int mode = PyLong_AsLong(datetimeModeObj);
long mode = PyLong_AsLong(datetimeModeObj);
if (!valid_datetime_mode(mode)) {
PyErr_SetString(PyExc_ValueError, "Invalid datetime_mode");
return NULL;
Expand All @@ -2246,7 +2246,7 @@ decoder_new(PyTypeObject* type, PyObject* args, PyObject* kwargs)
if (uuidModeObj == Py_None) {
uuidMode = UM_NONE;
} else if (PyLong_Check(uuidModeObj)) {
int mode = PyLong_AsLong(uuidModeObj);
long mode = PyLong_AsLong(uuidModeObj);
if (mode < 0 || mode >= UM_MAX) {
PyErr_SetString(PyExc_ValueError, "Invalid uuid_mode");
return NULL;
Expand All @@ -2263,7 +2263,7 @@ decoder_new(PyTypeObject* type, PyObject* args, PyObject* kwargs)
if (parseModeObj == Py_None) {
parseMode = PM_NONE;
} else if (PyLong_Check(parseModeObj)) {
int mode = PyLong_AsLong(parseModeObj);
long mode = PyLong_AsLong(parseModeObj);
if (mode < 0 || mode >= PM_MAX) {
PyErr_SetString(PyExc_ValueError, "Invalid parse_mode");
return NULL;
Expand Down
14 changes: 14 additions & 0 deletions tests/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
import rapidjson as rj


@pytest.mark.parametrize('mode', ['datetime_mode', 'uuid_mode', 'parse_mode'])
@pytest.mark.parametrize('value', [2**32, 2**32 + 1, -(2**32)])
def test_large_decode_mode_is_rejected(loads, mode, value):
with pytest.raises((ValueError, OverflowError)):
loads('null', **{mode: value})


@pytest.mark.parametrize('mode', ['datetime_mode', 'uuid_mode'])
@pytest.mark.parametrize('value', [2**32, 2**32 + 1, -(2**32)])
def test_large_encode_mode_is_rejected(dumps, mode, value):
with pytest.raises((ValueError, OverflowError)):
dumps(None, **{mode: value})


def test_skipkeys():
o = {True: False, -1: 1, 1.1: 1.1, (1,2): "foo", b"asdf": 1, None: None}

Expand Down
Loading