From d384984e3541a527f36174459714a5f0cdbd8182 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Sun, 19 Sep 2004 05:28:59 +0000 Subject: r2415: Throw a TypeError exception if a scalar value doesn't have the correct type, or the argument to a to_python function isn't a dictionary. (This used to be commit 0f58ffb142a9b8c5c745b3a2c93a1659ea8282e5) --- source4/build/pidl/swig.pm | 72 +++++++++++++++++++++++++++++------------ source4/scripting/swig/dcerpc.i | 56 +++++++++++++++++++++++++++----- 2 files changed, 100 insertions(+), 28 deletions(-) diff --git a/source4/build/pidl/swig.pm b/source4/build/pidl/swig.pm index 6e54033a42..8c8efc0f86 100644 --- a/source4/build/pidl/swig.pm +++ b/source4/build/pidl/swig.pm @@ -69,16 +69,14 @@ sub ArrayFromPython($$) $result .= "\t\tint i;\n\n"; $result .= "\t\tfor (i = 0; i < $array_len; i++) {\n"; if (util::is_scalar_type($e->{TYPE})) { - $result .= "\t\t\ts->$prefix$e->{NAME}\[i\] = $e->{TYPE}_from_python(PyList_GetItem(PyDict_GetItem(obj, PyString_FromString(\"$e->{NAME}\")), i));\n"; + $result .= "\t\t\ts->$prefix$e->{NAME}\[i\] = $e->{TYPE}_from_python(PyList_GetItem(PyDict_GetItem(obj, PyString_FromString(\"$e->{NAME}\")), i), \"$e->{NAME}\");\n"; } else { - $result .= "\t\t\t$e->{TYPE}_from_python(mem_ctx, &s->$prefix$e->{NAME}\[i\], PyList_GetItem(PyDict_GetItem(obj, PyString_FromString(\"$e->{NAME}\")), i));\n"; + $result .= "\t\t\t$e->{TYPE}_from_python(mem_ctx, &s->$prefix$e->{NAME}\[i\], PyList_GetItem(PyDict_GetItem(obj, PyString_FromString(\"$e->{NAME}\")), i), \"$e->{NAME}\");\n"; } $result .= "\t\t}\n"; $result .= "\t}\n"; -# $result .= "\tmemcpy(s->$prefix$e->{NAME}, PyString_AsString(PyDict_GetItem(obj, PyString_FromString(\"$e->{NAME}\"))), PyString_Size(PyDict_GetItem(obj, PyString_FromString(\"$e->{NAME}\"))));\n"; - return $result; } @@ -92,7 +90,7 @@ sub XFromPython($$) # Special cases if ($e->{TYPE} eq "string" && $e->{POINTERS} == 1) { - $result .= "\ts->$prefix$e->{NAME} = string_ptr_from_python(mem_ctx, $obj);\n"; + $result .= "\ts->$prefix$e->{NAME} = string_ptr_from_python(mem_ctx, $obj, \"$e->{NAME}\");\n"; return $result; } @@ -103,7 +101,7 @@ sub XFromPython($$) if ($e->{ARRAY_LEN}) { $result .= ArrayFromPython($e, $prefix); } else { - $result .= "\ts->$prefix$e->{NAME} = $e->{TYPE}_from_python($obj);\n"; + $result .= "\ts->$prefix$e->{NAME} = $e->{TYPE}_from_python($obj, \"$e->{NAME}\");\n"; } } else { $result .= "\t// Pointer to scalar\n"; @@ -114,10 +112,10 @@ sub XFromPython($$) if ($e->{ARRAY_LEN}) { $result .= ArrayFromPython($e, $prefix); } else { - $result .= "\t$e->{TYPE}_from_python(mem_ctx, &s->$prefix$e->{NAME}, $obj);\n"; + $result .= "\t$e->{TYPE}_from_python(mem_ctx, &s->$prefix$e->{NAME}, $obj, \"$e->{NAME}\");\n"; } } else { - $result .= "\ts->$prefix$e->{NAME} = $e->{TYPE}_ptr_from_python(mem_ctx, $obj);\n"; + $result .= "\ts->$prefix$e->{NAME} = $e->{TYPE}_ptr_from_python(mem_ctx, $obj, \"$e->{NAME}\");\n"; } } @@ -215,10 +213,17 @@ sub ParseFunction($) $res .= "/* Convert Python dict to struct $fn->{NAME}.in */\n\n"; - $res .= "struct $fn->{NAME} *$fn->{NAME}_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj)\n"; + $res .= "struct $fn->{NAME} *$fn->{NAME}_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name)\n"; $res .= "{\n"; - $res .= "\tstruct $fn->{NAME} *s = talloc(mem_ctx, sizeof(struct $fn->{NAME}));\n\n"; + $res .= "\tstruct $fn->{NAME} *s;\n\n"; + + $res .= "\tif (!PyDict_Check(obj)) {\n"; + $res .= "\t\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for %s\", name);\n"; + $res .= "\t\treturn NULL;\n"; + $res .= "\t}\n\n"; + + $res .= "\ts = talloc(mem_ctx, sizeof(struct $fn->{NAME}));\n\n"; # Remove this when all elements are initialised $res .= "\tmemset(s, 0, sizeof(struct $fn->{NAME}));\n\n"; @@ -252,7 +257,7 @@ sub ParseFunction($) $res .= "%typemap(in) struct $fn->{NAME} * {\n"; $res .= "\tTALLOC_CTX *mem_ctx = talloc_init(\"typemap(int) $fn->{NAME}\");\n\n"; - $res .= "\t\$1 = $fn->{NAME}_ptr_from_python(mem_ctx, \$input);\n"; + $res .= "\t\$1 = $fn->{NAME}_ptr_from_python(mem_ctx, \$input, \"\");\n"; $res .= "}\n\n"; # Output typemap @@ -262,6 +267,10 @@ sub ParseFunction($) $res .= "\tlong status = PyLong_AsLong(resultobj);\n"; $res .= "\tPyObject *dict;\n"; $res .= "\n"; + + $res .= "\tif (PyErr_Occurred())\n"; + $res .= "\t\t\treturn NULL;\n\n"; + $res .= "\tif (status != 0) {\n"; $res .= "\t\tset_ntstatus_exception(status);\n"; $res .= "\t\treturn NULL;\n"; @@ -285,7 +294,7 @@ sub ParseStruct($) $res .= "%{\n\n"; $res .= "/* Convert Python dict to struct $s->{NAME} pointer */\n\n"; - $res .= "struct $s->{NAME} *$s->{NAME}_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj)\n"; + $res .= "struct $s->{NAME} *$s->{NAME}_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name)\n"; $res .= "{\n"; $res .= "\tstruct $s->{NAME} *s;\n\n"; @@ -293,6 +302,11 @@ sub ParseStruct($) $res .= "\t\treturn NULL;\n"; $res .= "\t}\n\n"; + $res .= "\tif (!PyDict_Check(obj)) {\n"; + $res .= "\t\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for %s\", name);\n"; + $res .= "\t\treturn NULL;\n"; + $res .= "\t}\n\n"; + $res .= "\ts = talloc(mem_ctx, sizeof(struct $s->{NAME}));\n\n"; foreach my $e (@{$s->{DATA}{ELEMENTS}}) { @@ -305,9 +319,14 @@ sub ParseStruct($) $res .= "/* Convert Python dict to struct $s->{NAME} */\n\n"; - $res .= "void $s->{NAME}_from_python(TALLOC_CTX *mem_ctx, struct $s->{NAME} *s, PyObject *obj)\n"; + $res .= "void $s->{NAME}_from_python(TALLOC_CTX *mem_ctx, struct $s->{NAME} *s, PyObject *obj, char *name)\n"; $res .= "{\n"; + $res .= "\tif (!PyDict_Check(obj)) {\n"; + $res .= "\t\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for %s\", name);\n"; + $res .= "\t\treturn;\n"; + $res .= "\t}\n\n"; + foreach my $e (@{$s->{DATA}{ELEMENTS}}) { $res .= XFromPython($e, ""); } @@ -344,18 +363,25 @@ sub ParseUnion($) $res .= "%{\n\n"; $res .= "/* Convert Python dict to union $u->{NAME} pointer */\n\n"; - $res .= "union $u->{NAME} *$u->{NAME}_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj)\n"; + $res .= "union $u->{NAME} *$u->{NAME}_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name)\n"; $res .= "{\n"; - $res .= "\tunion $u->{NAME} *u = talloc(mem_ctx, sizeof(union $u->{NAME}));\n"; + $res .= "\tunion $u->{NAME} *u;\n"; $res .= "\tPyObject *dict;\n\n"; + $res .= "\tif (!PyDict_Check(obj)) {\n"; + $res .= "\t\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for %s\", name);\n"; + $res .= "\t\treturn NULL;\n"; + $res .= "\t}\n\n"; + + $res .= "\tu = talloc(mem_ctx, sizeof(union $u->{NAME}));\n\n"; + for my $e (@{$u->{DATA}{DATA}}) { $res .= "\tif ((dict = PyDict_GetItem(obj, PyString_FromString(\"$e->{DATA}{NAME}\")))) {\n"; if ($e->{DATA}{POINTERS} == 0) { - $res .= "\t\t$e->{DATA}{TYPE}_from_python(mem_ctx, &u->$e->{DATA}{NAME}, dict);\n"; + $res .= "\t\t$e->{DATA}{TYPE}_from_python(mem_ctx, &u->$e->{DATA}{NAME}, dict, \"$e->{DATA}{NAME}\");\n"; } elsif ($e->{DATA}{POINTERS} == 1) { - $res .= "\t\tu->$e->{DATA}{NAME} = $e->{DATA}{TYPE}_ptr_from_python(mem_ctx, dict);\n"; + $res .= "\t\tu->$e->{DATA}{NAME} = $e->{DATA}{TYPE}_ptr_from_python(mem_ctx, dict, \"$e->{DATA}{NAME}\");\n"; } else { $res .= "\t\t// $e->{DATA}{TYPE} pointers=$e->{DATA}{POINTERS}\n"; } @@ -382,15 +408,21 @@ sub ParseUnion($) $res .= "/* Convert Python dict to union $u->{NAME} */\n\n"; - $res .= "void $u->{NAME}_from_python(TALLOC_CTX *mem_ctx, union $u->{NAME} *u, PyObject *obj)\n"; + $res .= "void $u->{NAME}_from_python(TALLOC_CTX *mem_ctx, union $u->{NAME} *u, PyObject *obj, char *name)\n"; $res .= "{\n"; $res .= "\tPyObject *dict;\n\n"; + + $res .= "\tif (!PyDict_Check(obj)) {\n"; + $res .= "\t\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for %s\", name);\n"; + $res .= "\t\treturn;\n"; + $res .= "\t}\n\n"; + for my $e (@{$u->{DATA}{DATA}}) { $res .= "\tif ((dict = PyDict_GetItem(obj, PyString_FromString(\"$e->{DATA}{NAME}\")))) {\n"; if ($e->{DATA}{POINTERS} == 0) { - $res .= "\t\t$e->{DATA}{TYPE}_from_python(mem_ctx, &u->$e->{DATA}{NAME}, dict);\n"; + $res .= "\t\t$e->{DATA}{TYPE}_from_python(mem_ctx, &u->$e->{DATA}{NAME}, dict, \"$e->{DATA}{NAME}\");\n"; } elsif ($e->{DATA}{POINTERS} == 1) { - $res .= "\t\tu->$e->{DATA}{NAME} = $e->{DATA}{TYPE}_ptr_from_python(mem_ctx, dict);\n"; + $res .= "\t\tu->$e->{DATA}{NAME} = $e->{DATA}{TYPE}_ptr_from_python(mem_ctx, dict, \"$e->{DATA}{NAME}\");\n"; } else { $res .= "\t\t// $e->{DATA}{TYPE} pointers=$e->{DATA}{POINTERS}\n"; } diff --git a/source4/scripting/swig/dcerpc.i b/source4/scripting/swig/dcerpc.i index 64d757c634..a09c3f2377 100644 --- a/source4/scripting/swig/dcerpc.i +++ b/source4/scripting/swig/dcerpc.i @@ -49,8 +49,13 @@ void set_ntstatus_exception(int status) /* Conversion functions for scalar types */ -uint8 uint8_from_python(PyObject *obj) +uint8 uint8_from_python(PyObject *obj, char *name) { + if (!PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name); + return 0; + } + return (uint8)PyInt_AsLong(obj); } @@ -59,8 +64,13 @@ PyObject *uint8_to_python(uint8 obj) return PyInt_FromLong(obj); } -uint16 uint16_from_python(PyObject *obj) +uint16 uint16_from_python(PyObject *obj, char *name) { + if (!PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name); + return 0; + } + return (uint16)PyInt_AsLong(obj); } @@ -69,8 +79,13 @@ PyObject *uint16_to_python(uint16 obj) return PyInt_FromLong(obj); } -uint32 uint32_from_python(PyObject *obj) +uint32 uint32_from_python(PyObject *obj, char *name) { + if (!PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name); + return 0; + } + return (uint32)PyInt_AsLong(obj); } @@ -79,8 +94,13 @@ PyObject *uint32_to_python(uint32 obj) return PyInt_FromLong(obj); } -int64 int64_from_python(PyObject *obj) +int64 int64_from_python(PyObject *obj, char *name) { + if (!PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name); + return 0; + } + return (int64)PyLong_AsLong(obj); } @@ -89,8 +109,13 @@ PyObject *int64_to_python(int64 obj) return PyLong_FromLong(obj); } -uint64 uint64_from_python(PyObject *obj) +uint64 uint64_from_python(PyObject *obj, char *name) { + if (!PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name); + return 0; + } + return (uint64)PyLong_AsLong(obj); } @@ -99,8 +124,13 @@ PyObject *uint64_to_python(uint64 obj) return PyLong_FromLong(obj); } -NTTIME NTTIME_from_python(PyObject *obj) +NTTIME NTTIME_from_python(PyObject *obj, char *name) { + if (!PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting integer value for %s", name); + return 0; + } + return (NTTIME)PyLong_AsLong(obj); } @@ -109,8 +139,13 @@ PyObject *NTTIME_to_python(NTTIME obj) return PyLong_FromLong(obj); } -HYPER_T HYPER_T_from_python(PyObject *obj) +HYPER_T HYPER_T_from_python(PyObject *obj, char *name) { + if (!PyInt_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting integer value for %s", name); + return 0; + } + return (HYPER_T)PyLong_AsLong(obj); } @@ -122,11 +157,16 @@ PyObject *HYPER_T_to_python(HYPER_T obj) /* Conversion functions for types that we don't want generated automatically. This is mostly security realted stuff in misc.idl */ -char *string_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj) +char *string_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name) { if (obj == Py_None) return NULL; + if (!PyString_Check(obj)) { + PyErr_Format(PyExc_TypeError, "Expecting string value for %s", name); + return 0; + } + return PyString_AsString(obj); } -- cgit