r2416: More argument checks. Raise an exception instead of segfaulting if a
authorTim Potter <tpot@samba.org>
Sun, 19 Sep 2004 08:58:56 +0000 (08:58 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:58:50 +0000 (12:58 -0500)
dictionary does not contain a required key.

source/build/pidl/swig.pm
source/scripting/swig/dcerpc.i

index 8c8efc0f86f51d52fa0249c00a2a849eb55e0a5c..071f49d399cbe7ceb7d5f13668a8b69be9ab966b 100644 (file)
@@ -219,7 +219,7 @@ sub ParseFunction($)
     $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\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for key '%s'\", name);\n";
     $res .= "\t\treturn NULL;\n";
     $res .= "\t}\n\n";
 
@@ -257,7 +257,11 @@ 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, \"<function params>\");\n";
+    $res .= "\t\$1 = $fn->{NAME}_ptr_from_python(mem_ctx, \$input, \"<function params>\");\n\n";
+
+    $res .= "\tif (PyErr_Occurred())\n";
+    $res .= "\t\t\treturn NULL;\n\n";
+
     $res .= "}\n\n";
 
     # Output typemap
@@ -268,9 +272,6 @@ sub ParseFunction($)
     $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";
@@ -298,12 +299,17 @@ sub ParseStruct($)
     $res .= "{\n";
     $res .= "\tstruct $s->{NAME} *s;\n\n";
 
+    $res .= "\tif (obj == NULL) {\n";
+    $res .= "\t\tPyErr_Format(PyExc_ValueError, \"Expecting key '%s'\", name);\n";
+    $res .= "\t\treturn NULL;\n";
+    $res .= "\t}\n\n";
+
     $res .= "\tif (obj == Py_None) {\n";
     $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\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for key '%s'\", name);\n";
     $res .= "\t\treturn NULL;\n";
     $res .= "\t}\n\n";
 
@@ -322,8 +328,13 @@ sub ParseStruct($)
     $res .= "void $s->{NAME}_from_python(TALLOC_CTX *mem_ctx, struct $s->{NAME} *s, PyObject *obj, char *name)\n";
     $res .= "{\n";
 
+    $res .= "\tif (obj == NULL) {\n";
+    $res .= "\t\tPyErr_Format(PyExc_ValueError, \"Expecting key '%s'\", name);\n";
+    $res .= "\t\treturn;\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\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for key '%s'\", name);\n";
     $res .= "\t\treturn;\n";
     $res .= "\t}\n\n";
 
@@ -369,8 +380,13 @@ sub ParseUnion($)
     $res .= "\tunion $u->{NAME} *u;\n";
     $res .= "\tPyObject *dict;\n\n";
     
+    $res .= "\tif (obj == NULL) {\n";
+    $res .= "\t\tPyErr_Format(PyExc_ValueError, \"Expecting key '%s'\", name);\n";
+    $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\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for key '%s'\", name);\n";
     $res .= "\t\treturn NULL;\n";
     $res .= "\t}\n\n";
 
@@ -412,8 +428,13 @@ sub ParseUnion($)
     $res .= "{\n";
     $res .= "\tPyObject *dict;\n\n";
 
+    $res .= "\tif (obj == NULL) {\n";
+    $res .= "\t\tPyErr_Format(PyExc_ValueError, \"Expecting key '%s'\", name);\n";
+    $res .= "\t\treturn;\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\tPyErr_Format(PyExc_TypeError, \"Expecting dict value for key '%s'\", name);\n";
     $res .= "\t\treturn;\n";
     $res .= "\t}\n\n";
 
index a09c3f2377b3aaf9fc4e81d82ef26d00a3c37a10..a90ac90b5a7b060b1cded6e8285a31343b21e468 100644 (file)
@@ -51,6 +51,11 @@ void set_ntstatus_exception(int status)
 
 uint8 uint8_from_python(PyObject *obj, char *name)
 {
+       if (obj == NULL) {
+               PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
+               return 0;
+       }
+
        if (!PyInt_Check(obj)) {
                PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
                return 0;
@@ -66,6 +71,11 @@ PyObject *uint8_to_python(uint8 obj)
 
 uint16 uint16_from_python(PyObject *obj, char *name)
 {
+       if (obj == NULL) {
+               PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
+               return 0;
+       }
+
        if (!PyInt_Check(obj)) {
                PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
                return 0;
@@ -81,6 +91,11 @@ PyObject *uint16_to_python(uint16 obj)
 
 uint32 uint32_from_python(PyObject *obj, char *name)
 {
+       if (obj == NULL) {
+               PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
+               return 0;
+       }
+
        if (!PyInt_Check(obj)) {
                PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
                return 0;
@@ -96,6 +111,11 @@ PyObject *uint32_to_python(uint32 obj)
 
 int64 int64_from_python(PyObject *obj, char *name)
 {
+       if (obj == NULL) {
+               PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
+               return 0;
+       }
+
        if (!PyInt_Check(obj)) {
                PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
                return 0;
@@ -111,6 +131,11 @@ PyObject *int64_to_python(int64 obj)
 
 uint64 uint64_from_python(PyObject *obj, char *name)
 {
+       if (obj == NULL) {
+               PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
+               return 0;
+       }
+
        if (!PyInt_Check(obj)) {
                PyErr_Format(PyExc_TypeError, "Expecting int value for %s", name);
                return 0;
@@ -126,6 +151,11 @@ PyObject *uint64_to_python(uint64 obj)
 
 NTTIME NTTIME_from_python(PyObject *obj, char *name)
 {
+       if (obj == NULL) {
+               PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
+               return 0;
+       }
+
        if (!PyInt_Check(obj)) {
                PyErr_Format(PyExc_TypeError, "Expecting integer value for %s", name);
                return 0;
@@ -141,6 +171,11 @@ PyObject *NTTIME_to_python(NTTIME obj)
 
 HYPER_T HYPER_T_from_python(PyObject *obj, char *name)
 {
+       if (obj == NULL) {
+               PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
+               return 0;
+       }
+
        if (!PyInt_Check(obj)) {
                PyErr_Format(PyExc_TypeError, "Expecting integer value for %s", name);
                return 0;
@@ -159,12 +194,17 @@ PyObject *HYPER_T_to_python(HYPER_T obj)
 
 char *string_ptr_from_python(TALLOC_CTX *mem_ctx, PyObject *obj, char *name)
 {
+       if (obj == NULL) {
+               PyErr_Format(PyExc_ValueError, "Expecting key %s", name);
+               return NULL;
+       }
+
        if (obj == Py_None)
                return NULL;
 
        if (!PyString_Check(obj)) {
                PyErr_Format(PyExc_TypeError, "Expecting string value for %s", name);
-               return 0;
+               return NULL;
        }
 
        return PyString_AsString(obj);