Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-99240: Reset pointer to NULL when the pointed memory is freed in argument parsing#99890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kumaraditya303 merged 9 commits intopython:mainfromcolorfulappl:clear_freed_pointer
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from6 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
4 changes: 4 additions & 0 deletionsLib/test/test_capi/test_getargs.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1085,6 +1085,10 @@ def test_Z_hash(self):
with self.assertWarns(DeprecationWarning):
self.assertIsNone(getargs_Z_hash(None))

def test_gh_99240_clear_args(self):
from _testcapi import gh_99240_clear_args
self.assertRaises(TypeError, gh_99240_clear_args, 'a', '\0b')


class Object_TestCase(unittest.TestCase):
def test_S(self):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
In argument parsing, after deallocating newly allocated memory, reset its
pointer to NULL.
18 changes: 18 additions & 0 deletionsModules/_testcapi/getargs.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -854,6 +854,23 @@ getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs)
Py_RETURN_NONE;
}

static PyObject *
gh_99240_clear_args(PyObject *self, PyObject *args) {
char *a = NULL;
char *b = NULL;

if (!PyArg_ParseTuple(args, "eses", "idna", &a, "idna", &b)) {
if (a || b) {
PyErr_Clear();
PyErr_SetString(PyExc_AssertionError, "Arguments is not cleared.");
}
return NULL;
}
PyMem_Free(a);
PyMem_Free(b);
Py_RETURN_NONE;
}

static PyMethodDef test_methods[] = {
{"get_args", get_args, METH_VARARGS},
{"get_kwargs", _PyCFunction_CAST(get_kwargs), METH_VARARGS|METH_KEYWORDS},
Expand DownExpand Up@@ -906,6 +923,7 @@ static PyMethodDef test_methods[] = {
{"test_empty_argparse", test_empty_argparse, METH_NOARGS},
{"test_k_code", test_k_code, METH_NOARGS},
{"test_s_code", test_s_code, METH_NOARGS},
{"gh_99240_clear_args", gh_99240_clear_args, METH_VARARGS},
{NULL},
};

Expand Down
10 changes: 5 additions & 5 deletionsPython/getargs.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -202,9 +202,9 @@ _PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
static int
cleanup_ptr(PyObject *self, void *ptr)
{
if (ptr) {
PyMem_Free(ptr);
}
void **pptr = (void **)ptr;
PyMem_Free(*pptr);
*pptr = NULL;
return 0;
}

Expand DownExpand Up@@ -1116,7 +1116,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
PyErr_NoMemory();
RETURN_ERR_OCCURRED;
}
if (addcleanup(*buffer, freelist, cleanup_ptr)) {
if (addcleanup(buffer, freelist, cleanup_ptr)) {
Py_DECREF(s);
return converterr(
"(cleanup problem)",
Expand DownExpand Up@@ -1162,7 +1162,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
PyErr_NoMemory();
RETURN_ERR_OCCURRED;
}
if (addcleanup(*buffer, freelist, cleanup_ptr)) {
if (addcleanup(buffer, freelist, cleanup_ptr)) {
Py_DECREF(s);
return converterr("(cleanup problem)",
arg, msgbuf, bufsize);
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp