Skip to content

Commit 428f23f

Browse files
committed
Fix out-of-bounds access of output vars.
1 parent 2f87efa commit 428f23f

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

graalpython/com.oracle.graal.python.cext/src/modsupport.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,13 @@ MUST_INLINE static int _PyTruffleArg_ParseTupleAndKeywords(PyObject *argv, PyObj
121121
case 'y':
122122
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
123123
if (format[format_idx + 1] == '*') {
124-
format_idx++; // skip over '*'
125-
PyErr_Format(PyExc_TypeError, "%c* not supported", c);
126-
return 0;
124+
Py_buffer* p = PyTruffleVaArg(poly_args, offset, va, Py_buffer*);
125+
const char* buf;
126+
format_idx++; // skip over '*'
127+
if (getbuffer(arg, p, &buf) < 0) {
128+
PyErr_Format(PyExc_TypeError, "expected bytes, got %R", Py_TYPE(arg));
129+
return 0;
130+
}
127131
} else if (arg == Py_None) {
128132
if (c == 'z') {
129133
PyTruffle_WriteOut(poly_args, offset, va, const char*, NULL);
@@ -374,7 +378,6 @@ int _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *argv, PyObject *kwds, const c
374378
return _PyTruffleArg_ParseTupleAndKeywords(argv, kwds, format, kwdnames, va, NULL, 0);
375379
}
376380

377-
378381
int PyArg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const char *format, char** kwdnames, ...) {
379382
CallWithPolyglotArgs(int result, kwdnames, 4, _PyTruffleArg_ParseTupleAndKeywords, argv, kwds, format, kwdnames);
380383
return result;
@@ -394,24 +397,27 @@ MUST_INLINE PyObject* PyTruffle_Stack2Tuple(PyObject** args, Py_ssize_t nargs) {
394397
return argv;
395398
}
396399

397-
int PyArg_ParseStack(PyObject **args, Py_ssize_t nargs, PyObject *kwds, struct _PyArg_Parser *parser, ...) {
398-
CallWithPolyglotArgs(int result, parser, 4, _PyTruffleArg_ParseTupleAndKeywords, PyTruffle_Stack2Tuple(args, nargs), kwds, parser->format, parser->keywords);
400+
int PyArg_ParseStack(PyObject **args, Py_ssize_t nargs, const char* format, ...) {
401+
// TODO(fa) Converting the stack to a tuple is rather slow. We should refactor
402+
// '_PyTruffleArg_ParseTupleAndKeywords' (like CPython) into smaller operations.
403+
CallWithPolyglotArgs(int result, parser, 3, _PyTruffleArg_ParseTupleAndKeywords, PyTruffle_Stack2Tuple(args, nargs), PyDict_New(), format, NULL);
399404
return result;
400405
}
401406

402-
int _PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs, PyObject *kwds, struct _PyArg_Parser *parser, ...) {
403-
CallWithPolyglotArgs(int result, parser, 4, _PyTruffleArg_ParseTupleAndKeywords, PyTruffle_Stack2Tuple(args, nargs), kwds, parser->format, parser->keywords);
407+
int _PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs, const char* format, ...) {
408+
// TODO(fa) Avoid usage of 'PyTruffle_Stack2Tuple'; see 'PyArg_ParseStack'.
409+
CallWithPolyglotArgs(int result, parser, 3, _PyTruffleArg_ParseTupleAndKeywords, PyTruffle_Stack2Tuple(args, nargs), PyDict_New(), format, NULL);
404410
return result;
405411
}
406412

407413
int _PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject* kwnames, struct _PyArg_Parser* parser, ...) {
408-
// TODO(fa) That's not very fast and we should refactor these functions.
414+
// TODO(fa) Avoid usage of 'PyTruffle_Stack2Tuple'; see 'PyArg_ParseStack'.
409415
CallWithPolyglotArgs(int result, parser, 4, _PyTruffleArg_ParseTupleAndKeywords, PyTruffle_Stack2Tuple(args, nargs), kwnames, parser->format, parser->keywords);
410416
return result;
411417
}
412418

413419
int _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject* kwnames, struct _PyArg_Parser* parser, ...) {
414-
// TODO(fa) That's not very fast and we should refactor these functions.
420+
// TODO(fa) Avoid usage of 'PyTruffle_Stack2Tuple'; see 'PyArg_ParseStack'.
415421
CallWithPolyglotArgs(int result, parser, 4, _PyTruffleArg_ParseTupleAndKeywords, PyTruffle_Stack2Tuple(args, nargs), kwnames, parser->format, parser->keywords);
416422
return result;
417423
}

0 commit comments

Comments
 (0)