Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 36 additions & 8 deletions netsnmp/client_intf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1535,8 +1535,12 @@ netsnmp_get(PyObject *self, PyObject *args)

if (varlist) {
PyObject *varlist_iter = PyObject_GetIter(varlist);
if (!varlist_iter) {
snmp_free_pdu(pdu);
goto done;
}

while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {
while ((varbind = PyIter_Next(varlist_iter))) {
if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0 ||
py_netsnmp_attr_string(varbind, "iid", &iid, NULL) < 0)
{
Expand Down Expand Up @@ -1682,6 +1686,10 @@ netsnmp_get(PyObject *self, PyObject *args)

done:
SAFE_FREE(oid_arr);
if (PyErr_Occurred()) {
Py_XDECREF(val_tuple);
return NULL;
}
return (val_tuple ? val_tuple : Py_BuildValue(""));
}

Expand Down Expand Up @@ -1753,8 +1761,12 @@ netsnmp_getnext(PyObject *self, PyObject *args)

if (varlist) {
PyObject *varlist_iter = PyObject_GetIter(varlist);
if (!varlist_iter) {
snmp_free_pdu(pdu);
goto done;
}

while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {
while ((varbind = PyIter_Next(varlist_iter))) {
if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0 ||
py_netsnmp_attr_string(varbind, "iid", &iid, NULL) < 0)
{
Expand Down Expand Up @@ -1902,6 +1914,10 @@ netsnmp_getnext(PyObject *self, PyObject *args)

done:
SAFE_FREE(oid_arr);
if (PyErr_Occurred()) {
Py_XDECREF(val_tuple);
return NULL;
}
return (val_tuple ? val_tuple : Py_BuildValue(""));
}

Expand Down Expand Up @@ -1991,8 +2007,12 @@ netsnmp_walk(PyObject *self, PyObject *args)

/* we need an initial count for memory allocation */
varlist_iter = PyObject_GetIter(varlist);
if (!varlist_iter) {
snmp_free_pdu(pdu);
goto done;
}
varlist_len = 0;
while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {
while ((varbind = PyIter_Next(varlist_iter))) {
varlist_len++;
}
Py_DECREF(varlist_iter);
Expand Down Expand Up @@ -2523,14 +2543,16 @@ netsnmp_getbulk(PyObject *self, PyObject *args)
/* propagate error */
if (verbose)
printf("error: getbulk response processing: unknown python error");
if (val_tuple)
Py_DECREF(val_tuple);
val_tuple = NULL;
Py_CLEAR(val_tuple);
}
}

done:
SAFE_FREE(oid_arr);
if (PyErr_Occurred()) {
Py_XDECREF(val_tuple);
return NULL;
}
return (val_tuple ? val_tuple : Py_BuildValue(""));
}

Expand All @@ -2539,7 +2561,7 @@ netsnmp_set(PyObject *self, PyObject *args)
{
PyObject *session;
PyObject *varlist;
PyObject *varbind;
PyObject *varbind = NULL;
PyObject *ret = NULL;
netsnmp_session *ss;
netsnmp_pdu *pdu, *response;
Expand Down Expand Up @@ -2587,8 +2609,12 @@ netsnmp_set(PyObject *self, PyObject *args)

if (varlist) {
PyObject *varlist_iter = PyObject_GetIter(varlist);
if (!varlist_iter) {
snmp_free_pdu(pdu);
goto done;
}

while (varlist_iter && (varbind = PyIter_Next(varlist_iter))) {
while ((varbind = PyIter_Next(varlist_iter))) {
if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0 ||
py_netsnmp_attr_string(varbind, "iid", &iid, NULL) < 0)
{
Expand Down Expand Up @@ -2673,6 +2699,8 @@ netsnmp_set(PyObject *self, PyObject *args)
done:
Py_XDECREF(varbind);
SAFE_FREE(oid_arr);
if (PyErr_Occurred())
return NULL;
return (ret ? ret : Py_BuildValue(""));
}

Expand Down
17 changes: 17 additions & 0 deletions netsnmp/tests/system/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@


class ErrorPathTests(unittest.TestCase):
def test_session_methods_reject_bare_varbind(self):
session = netsnmp.Session(**READ_ARGS)
varbind = netsnmp.Varbind(
'.1.3.6.1.2.1.1.3', '0', b'1', 'INTEGER')
calls = (
(TypeError, lambda: session.get(varbind)),
(TypeError, lambda: session.getnext(varbind)),
(TypeError, lambda: session.set(varbind)),
(AttributeError, lambda: session.walk(varbind)),
(AttributeError, lambda: session.getbulk(0, 1, varbind)),
)

for error_type, call in calls:
with self.subTest(call=call):
with self.assertRaises(error_type):
call()

def test_unknown_oid_returns_no_value(self):
varbind = netsnmp.Varbind('.1.3.6.1.2.1.1.999', '0')
values = netsnmp.snmpget(varbind, **READ_ARGS)
Expand Down
Loading