Skip to content

Commit 38a26a2

Browse files
Merge pull request #48 from frenzymadness/CWE_fixes
CWE fixes
2 parents e7511d8 + 45a1dc5 commit 38a26a2

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

python-ethtool/ethtool.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static PyObject *get_devices(PyObject *self __unused, PyObject *args __unused)
9494
ret = fgets(buffer, 256, fd);
9595
ret = fgets(buffer, 256, fd);
9696
if (!ret) {
97+
fclose(fd);
9798
return PyErr_SetFromErrno(PyExc_OSError);
9899
}
99100

@@ -665,7 +666,8 @@ static PyObject *get_wireless_protocol (PyObject *self __unused, PyObject *args)
665666

666667
/* Setup our request structure. */
667668
memset(&iwr, 0, sizeof(iwr));
668-
strncpy(iwr.ifr_name, devname, IFNAMSIZ);
669+
strncpy(iwr.ifr_name, devname, IFNAMSIZ-1);
670+
iwr.ifr_name[IFNAMSIZ-1] = 0;
669671

670672
/* Open control socket. */
671673
fd = socket(AF_INET, SOCK_DGRAM, 0);
@@ -728,7 +730,7 @@ static PyObject *__struct_desc_create_dict(struct struct_desc *table,
728730
PyObject *dict = PyDict_New();
729731

730732
if (dict == NULL)
731-
goto out;
733+
return NULL;
732734

733735
for (i = 0; i < nr_entries; ++i) {
734736
struct struct_desc *d = &table[i];
@@ -741,21 +743,21 @@ static PyObject *__struct_desc_create_dict(struct struct_desc *table,
741743
break;
742744
}
743745

744-
if (objval == NULL)
745-
goto free_dict;
746+
if (objval == NULL) {
747+
Py_DECREF(dict);
748+
return NULL;
749+
}
746750

747751
if (PyDict_SetItemString(dict, d->name, objval) != 0) {
748752
Py_DECREF(objval);
749-
goto free_dict;
753+
Py_DECREF(dict);
754+
return NULL;
750755
}
751756

752757
Py_DECREF(objval);
753758
}
754-
out:
759+
755760
return dict;
756-
free_dict:
757-
goto out;
758-
dict = NULL;
759761
}
760762

761763
#define struct_desc_create_dict(table, values) \

tests/parse_ifconfig.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ def __init__(self, stdout=None, debug=False):
113113
curdev._parse_rest_of_first_line_old(mo.group(2))
114114
continue
115115

116+
# If we don't have current device yet, doesn't make sense to
117+
# read the rest of output lines
118+
if curdev is None:
119+
continue
120+
116121
if self.oldFormat:
117122
curdev._parse_line_old(line)
118123
else:
@@ -540,8 +545,6 @@ def _parse_line_old(self, line):
540545

541546
return
542547

543-
raise ValueError('parser could not handle line: %r' % line)
544-
545548
# ifconfig = IfConfig()
546549
# for dev in ifconfig.devices:
547550
# print(dev)

0 commit comments

Comments
 (0)