diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2009-09-22 19:19:02 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2009-09-22 19:19:02 +0300 |
commit | da74188fa654246ec74e0375440f326e5dc714f2 (patch) | |
tree | 81ab358d268b42147278025a23e8a1c701a98c3d | |
parent | 00d683ce88748080ce78d1e2d802a401b2d30261 (diff) | |
download | rpm-da74188fa654246ec74e0375440f326e5dc714f2.tar.gz rpm-da74188fa654246ec74e0375440f326e5dc714f2.tar.bz2 rpm-da74188fa654246ec74e0375440f326e5dc714f2.zip |
Sanitize python object -> tag number exception handling
- raise exception in tagNumFromPyObject(), not in 12 different callers
- check against RPMTAG_NOT_FOUND consistently instead of -1 and whatnot
- unknown tags are value, not key or type errors
-rw-r--r-- | python/header-py.c | 21 | ||||
-rw-r--r-- | python/rpmds-py.c | 15 | ||||
-rw-r--r-- | python/rpmfi-py.c | 10 | ||||
-rw-r--r-- | python/rpmmi-py.c | 6 | ||||
-rw-r--r-- | python/rpmte-py.c | 5 | ||||
-rw-r--r-- | python/rpmts-py.c | 3 |
6 files changed, 19 insertions, 41 deletions
diff --git a/python/header-py.c b/python/header-py.c index ebae98d8e..e8f4bf717 100644 --- a/python/header-py.c +++ b/python/header-py.c @@ -333,19 +333,21 @@ static void hdr_dealloc(hdrObject * s) PyObject_Del(s); } -/** \ingroup py_c - */ rpmTag tagNumFromPyObject (PyObject *item) { - char * str; + rpmTag tag = RPMTAG_NOT_FOUND; if (PyInt_Check(item)) { - return PyInt_AsLong(item); + /* XXX we should probably validate tag numbers too */ + tag = PyInt_AsLong(item); } else if (PyString_Check(item)) { - str = PyString_AsString(item); - return rpmTagGetValue(str); + tag = rpmTagGetValue(PyString_AsString(item)); + } + if (tag == RPMTAG_NOT_FOUND) { + PyErr_SetString(PyExc_ValueError, "unknown header tag"); } - return RPMTAG_NOT_FOUND; + + return tag; } /** \ingroup py_c @@ -362,10 +364,7 @@ static PyObject * hdr_subscript(hdrObject * s, PyObject * item) struct rpmtd_s td; tag = tagNumFromPyObject (item); - if (tag == RPMTAG_NOT_FOUND) { - PyErr_SetString(PyExc_KeyError, "unknown header tag"); - return NULL; - } + if (tag == RPMTAG_NOT_FOUND) return NULL; tagtype = rpmTagGetType(tag); forceArray = (tagtype & RPM_MASK_RETURN_TYPE) == RPM_ARRAY_RETURN_TYPE; diff --git a/python/rpmds-py.c b/python/rpmds-py.c index 223c27d9d..125b3d252 100644 --- a/python/rpmds-py.c +++ b/python/rpmds-py.c @@ -507,10 +507,7 @@ static PyObject * rpmds_new(PyTypeObject * subtype, PyObject *args, PyObject *kw if (to != NULL) { tagN = tagNumFromPyObject(to); - if (tagN == -1) { - PyErr_SetString(PyExc_KeyError, "unknown header tag"); - return NULL; - } + if (tagN == RPMTAG_NOT_FOUND) return NULL; } s->ds = rpmdsNew(hdrGetHeader(ho), tagN, 0); @@ -603,10 +600,7 @@ rpmds_Single(PyObject * s, PyObject * args, PyObject * kwds) if (to != NULL) { tagN = tagNumFromPyObject(to); - if (tagN == -1) { - PyErr_SetString(PyExc_KeyError, "unknown header tag"); - return NULL; - } + if (tagN == RPMTAG_NOT_FOUND) return NULL; } if (N != NULL) N = xstrdup(N); if (EVR != NULL) EVR = xstrdup(EVR); @@ -628,10 +622,7 @@ hdr_dsFromHeader(PyObject * s, PyObject * args, PyObject * kwds) if (to != NULL) { tagN = tagNumFromPyObject(to); - if (tagN == -1) { - PyErr_SetString(PyExc_KeyError, "unknown header tag"); - return NULL; - } + if (tagN == RPMTAG_NOT_FOUND) return NULL; } return rpmds_Wrap( rpmdsNew(hdrGetHeader(ho), tagN, 0) ); } diff --git a/python/rpmfi-py.c b/python/rpmfi-py.c index 70bc6db2f..45e2d298b 100644 --- a/python/rpmfi-py.c +++ b/python/rpmfi-py.c @@ -400,10 +400,7 @@ static PyObject * rpmfi_new(PyTypeObject * subtype, PyObject *args, PyObject *kw if (to != NULL) { tagN = tagNumFromPyObject(to); - if (tagN == -1) { - PyErr_SetString(PyExc_KeyError, "unknown header tag"); - return NULL; - } + if (tagN == RPMTAG_NOT_FOUND) return NULL; } s->fi = rpmfiNew(NULL, hdrGetHeader(ho), tagN, flags); @@ -495,10 +492,7 @@ hdr_fiFromHeader(PyObject * s, PyObject * args, PyObject * kwds) if (to != NULL) { tagN = tagNumFromPyObject(to); - if (tagN == -1) { - PyErr_SetString(PyExc_KeyError, "unknown header tag"); - return NULL; - } + if (tagN == RPMTAG_NOT_FOUND) return NULL; } return rpmfi_Wrap( rpmfiNew(ts, hdrGetHeader(ho), tagN, flags) ); } diff --git a/python/rpmmi-py.c b/python/rpmmi-py.c index 95060ab13..08b02e840 100644 --- a/python/rpmmi-py.c +++ b/python/rpmmi-py.c @@ -152,10 +152,8 @@ rpmmi_Pattern(rpmmiObject * s, PyObject * args, PyObject * kwds) &TagN, &type, &pattern)) return NULL; - if ((tag = tagNumFromPyObject (TagN)) == -1) { - PyErr_SetString(PyExc_TypeError, "unknown tag type"); - return NULL; - } + tag = tagNumFromPyObject (TagN); + if (tag == RPMTAG_NOT_FOUND) return NULL; rpmdbSetIteratorRE(s->mi, tag, type, pattern); diff --git a/python/rpmte-py.c b/python/rpmte-py.c index bfafada1a..0a761cc0c 100644 --- a/python/rpmte-py.c +++ b/python/rpmte-py.c @@ -193,10 +193,7 @@ rpmte_DS(rpmteObject * s, PyObject * args, PyObject * kwds) return NULL; tag = tagNumFromPyObject(TagN); - if (tag == -1) { - PyErr_SetString(PyExc_TypeError, "unknown tag type"); - return NULL; - } + if (tag == RPMTAG_NOT_FOUND) return NULL; ds = rpmteDS(s->te, tag); if (ds == NULL) { diff --git a/python/rpmts-py.c b/python/rpmts-py.c index 355e2f9f6..7fd3172d6 100644 --- a/python/rpmts-py.c +++ b/python/rpmts-py.c @@ -1079,8 +1079,7 @@ fprintf(stderr, "*** rpmts_Match(%p) ts %p\n", s, s->ts); &TagN, &Key)) return NULL; - if (TagN && (tag = tagNumFromPyObject (TagN)) == -1) { - PyErr_SetString(PyExc_TypeError, "unknown tag type"); + if (TagN && (tag = tagNumFromPyObject (TagN)) == RPMTAG_NOT_FOUND) { return NULL; } |