Skip to content

Commit 0e46609

Browse files
committed
Handle a whole lot of failures from PyString_FromInternedString().
Should fix most of Klocwork 234-272.
1 parent 8ae7305 commit 0e46609

1 file changed

Lines changed: 101 additions & 25 deletions

File tree

Objects/classobject.c

Lines changed: 101 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ PyClass_New(PyObject *bases, PyObject *dict, PyObject *name)
103103
op->cl_name = name;
104104
if (getattrstr == NULL) {
105105
getattrstr = PyString_InternFromString("__getattr__");
106+
if (getattrstr == NULL)
107+
return NULL;
106108
setattrstr = PyString_InternFromString("__setattr__");
109+
if (setattrstr == NULL)
110+
return NULL;
107111
delattrstr = PyString_InternFromString("__delattr__");
112+
if (delattrstr == NULL)
113+
return NULL;
108114
}
109115
op->cl_getattr = class_lookup(op, getattrstr, &dummy);
110116
op->cl_setattr = class_lookup(op, setattrstr, &dummy);
@@ -522,11 +528,14 @@ PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw)
522528
PyObject *init;
523529
static PyObject *initstr;
524530

531+
if (initstr == NULL) {
532+
initstr = PyString_InternFromString("__init__");
533+
if (initstr == NULL)
534+
return NULL;
535+
}
525536
inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);
526537
if (inst == NULL)
527538
return NULL;
528-
if (initstr == NULL)
529-
initstr = PyString_InternFromString("__init__");
530539
init = instance_getattr2(inst, initstr);
531540
if (init == NULL) {
532541
if (PyErr_Occurred()) {
@@ -612,8 +621,11 @@ instance_dealloc(register PyInstanceObject *inst)
612621
/* Save the current exception, if any. */
613622
PyErr_Fetch(&error_type, &error_value, &error_traceback);
614623
/* Execute __del__ method, if any. */
615-
if (delstr == NULL)
624+
if (delstr == NULL) {
616625
delstr = PyString_InternFromString("__del__");
626+
if (delstr == NULL)
627+
return NULL;
628+
}
617629
if ((del = instance_getattr2(inst, delstr)) != NULL) {
618630
PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
619631
if (res == NULL)
@@ -840,8 +852,11 @@ instance_repr(PyInstanceObject *inst)
840852
PyObject *res;
841853
static PyObject *reprstr;
842854

843-
if (reprstr == NULL)
855+
if (reprstr == NULL) {
844856
reprstr = PyString_InternFromString("__repr__");
857+
if (reprstr == NULL)
858+
return NULL;
859+
}
845860
func = instance_getattr(inst, reprstr);
846861
if (func == NULL) {
847862
PyObject *classname, *mod;
@@ -876,8 +891,11 @@ instance_str(PyInstanceObject *inst)
876891
PyObject *res;
877892
static PyObject *strstr;
878893

879-
if (strstr == NULL)
894+
if (strstr == NULL) {
880895
strstr = PyString_InternFromString("__str__");
896+
if (strstr == NULL)
897+
return NULL;
898+
}
881899
func = instance_getattr(inst, strstr);
882900
if (func == NULL) {
883901
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -898,8 +916,11 @@ instance_hash(PyInstanceObject *inst)
898916
long outcome;
899917
static PyObject *hashstr, *eqstr, *cmpstr;
900918

901-
if (hashstr == NULL)
919+
if (hashstr == NULL) {
902920
hashstr = PyString_InternFromString("__hash__");
921+
if (hashstr == NULL)
922+
return -1;
923+
}
903924
func = instance_getattr(inst, hashstr);
904925
if (func == NULL) {
905926
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
@@ -908,15 +929,21 @@ instance_hash(PyInstanceObject *inst)
908929
/* If there is no __eq__ and no __cmp__ method, we hash on the
909930
address. If an __eq__ or __cmp__ method exists, there must
910931
be a __hash__. */
911-
if (eqstr == NULL)
932+
if (eqstr == NULL) {
912933
eqstr = PyString_InternFromString("__eq__");
934+
if (eqstr == NULL)
935+
return -1;
936+
}
913937
func = instance_getattr(inst, eqstr);
914938
if (func == NULL) {
915939
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
916940
return -1;
917941
PyErr_Clear();
918-
if (cmpstr == NULL)
942+
if (cmpstr == NULL) {
919943
cmpstr = PyString_InternFromString("__cmp__");
944+
if (cmpstr == NULL)
945+
return -1;
946+
}
920947
func = instance_getattr(inst, cmpstr);
921948
if (func == NULL) {
922949
if (!PyErr_ExceptionMatches(
@@ -964,8 +991,11 @@ instance_length(PyInstanceObject *inst)
964991
PyObject *res;
965992
Py_ssize_t outcome;
966993

967-
if (lenstr == NULL)
994+
if (lenstr == NULL) {
968995
lenstr = PyString_InternFromString("__len__");
996+
if (lenstr == NULL)
997+
return -1;
998+
}
969999
func = instance_getattr(inst, lenstr);
9701000
if (func == NULL)
9711001
return -1;
@@ -1010,8 +1040,11 @@ instance_subscript(PyInstanceObject *inst, PyObject *key)
10101040
PyObject *arg;
10111041
PyObject *res;
10121042

1013-
if (getitemstr == NULL)
1043+
if (getitemstr == NULL) {
10141044
getitemstr = PyString_InternFromString("__getitem__");
1045+
if (getitemstr == NULL)
1046+
return NULL;
1047+
}
10151048
func = instance_getattr(inst, getitemstr);
10161049
if (func == NULL)
10171050
return NULL;
@@ -1034,13 +1067,19 @@ instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
10341067
PyObject *res;
10351068

10361069
if (value == NULL) {
1037-
if (delitemstr == NULL)
1070+
if (delitemstr == NULL) {
10381071
delitemstr = PyString_InternFromString("__delitem__");
1072+
if (delitemstr == NULL)
1073+
return -1;
1074+
}
10391075
func = instance_getattr(inst, delitemstr);
10401076
}
10411077
else {
1042-
if (setitemstr == NULL)
1078+
if (setitemstr == NULL) {
10431079
setitemstr = PyString_InternFromString("__setitem__");
1080+
if (setitemstr == NULL)
1081+
return -1;
1082+
}
10441083
func = instance_getattr(inst, setitemstr);
10451084
}
10461085
if (func == NULL)
@@ -1073,8 +1112,11 @@ instance_item(PyInstanceObject *inst, Py_ssize_t i)
10731112
{
10741113
PyObject *func, *res;
10751114

1076-
if (getitemstr == NULL)
1115+
if (getitemstr == NULL) {
10771116
getitemstr = PyString_InternFromString("__getitem__");
1117+
if (getitemstr == NULL)
1118+
return NULL;
1119+
}
10781120
func = instance_getattr(inst, getitemstr);
10791121
if (func == NULL)
10801122
return NULL;
@@ -1089,17 +1131,23 @@ instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
10891131
PyObject *func, *arg, *res;
10901132
static PyObject *getslicestr;
10911133

1092-
if (getslicestr == NULL)
1134+
if (getslicestr == NULL) {
10931135
getslicestr = PyString_InternFromString("__getslice__");
1136+
if (getslicestr == NULL)
1137+
return NULL;
1138+
}
10941139
func = instance_getattr(inst, getslicestr);
10951140

10961141
if (func == NULL) {
10971142
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
10981143
return NULL;
10991144
PyErr_Clear();
11001145

1101-
if (getitemstr == NULL)
1146+
if (getitemstr == NULL) {
11021147
getitemstr = PyString_InternFromString("__getitem__");
1148+
if (getitemstr == NULL)
1149+
return NULL;
1150+
}
11031151
func = instance_getattr(inst, getitemstr);
11041152
if (func == NULL)
11051153
return NULL;
@@ -1123,13 +1171,19 @@ instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item)
11231171
PyObject *func, *arg, *res;
11241172

11251173
if (item == NULL) {
1126-
if (delitemstr == NULL)
1174+
if (delitemstr == NULL) {
11271175
delitemstr = PyString_InternFromString("__delitem__");
1176+
if (delitemstr == NULL)
1177+
return -1;
1178+
}
11281179
func = instance_getattr(inst, delitemstr);
11291180
}
11301181
else {
1131-
if (setitemstr == NULL)
1182+
if (setitemstr == NULL) {
11321183
setitemstr = PyString_InternFromString("__setitem__");
1184+
if (setitemstr == NULL)
1185+
return -1;
1186+
}
11331187
func = instance_getattr(inst, setitemstr);
11341188
}
11351189
if (func == NULL)
@@ -1158,17 +1212,23 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject
11581212
static PyObject *setslicestr, *delslicestr;
11591213

11601214
if (value == NULL) {
1161-
if (delslicestr == NULL)
1215+
if (delslicestr == NULL) {
11621216
delslicestr =
11631217
PyString_InternFromString("__delslice__");
1218+
if (delslicestr == NULL)
1219+
return -1;
1220+
}
11641221
func = instance_getattr(inst, delslicestr);
11651222
if (func == NULL) {
11661223
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
11671224
return -1;
11681225
PyErr_Clear();
1169-
if (delitemstr == NULL)
1226+
if (delitemstr == NULL) {
11701227
delitemstr =
11711228
PyString_InternFromString("__delitem__");
1229+
if (delitemstr == NULL)
1230+
return -1;
1231+
}
11721232
func = instance_getattr(inst, delitemstr);
11731233
if (func == NULL)
11741234
return -1;
@@ -1179,17 +1239,23 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject
11791239
arg = Py_BuildValue("(nn)", i, j);
11801240
}
11811241
else {
1182-
if (setslicestr == NULL)
1242+
if (setslicestr == NULL) {
11831243
setslicestr =
11841244
PyString_InternFromString("__setslice__");
1245+
if (setslicestr == NULL)
1246+
return -1;
1247+
}
11851248
func = instance_getattr(inst, setslicestr);
11861249
if (func == NULL) {
11871250
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
11881251
return -1;
11891252
PyErr_Clear();
1190-
if (setitemstr == NULL)
1253+
if (setitemstr == NULL) {
11911254
setitemstr =
11921255
PyString_InternFromString("__setitem__");
1256+
if (setitemstr == NULL)
1257+
return -1;
1258+
}
11931259
func = instance_getattr(inst, setitemstr);
11941260
if (func == NULL)
11951261
return -1;
@@ -1462,7 +1528,8 @@ instance_coerce(PyObject **pv, PyObject **pw)
14621528
#define UNARY(funcname, methodname) \
14631529
static PyObject *funcname(PyInstanceObject *self) { \
14641530
static PyObject *o; \
1465-
if (o == NULL) o = PyString_InternFromString(methodname); \
1531+
if (o == NULL) { o = PyString_InternFromString(methodname); \
1532+
if (o == NULL) return NULL; } \
14661533
return generic_unary_op(self, o); \
14671534
}
14681535

@@ -1634,14 +1701,20 @@ instance_nonzero(PyInstanceObject *self)
16341701
long outcome;
16351702
static PyObject *nonzerostr;
16361703

1637-
if (nonzerostr == NULL)
1704+
if (nonzerostr == NULL) {
16381705
nonzerostr = PyString_InternFromString("__nonzero__");
1706+
if (nonzerostr == NULL)
1707+
return -1;
1708+
}
16391709
if ((func = instance_getattr(self, nonzerostr)) == NULL) {
16401710
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
16411711
return -1;
16421712
PyErr_Clear();
1643-
if (lenstr == NULL)
1713+
if (lenstr == NULL) {
16441714
lenstr = PyString_InternFromString("__len__");
1715+
if (lenstr == NULL)
1716+
return -1;
1717+
}
16451718
if ((func = instance_getattr(self, lenstr)) == NULL) {
16461719
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
16471720
return -1;
@@ -1923,8 +1996,11 @@ instance_iternext(PyInstanceObject *self)
19231996
{
19241997
PyObject *func;
19251998

1926-
if (nextstr == NULL)
1999+
if (nextstr == NULL) {
19272000
nextstr = PyString_InternFromString("next");
2001+
if (nextstr == NULL)
2002+
return NULL;
2003+
}
19282004

19292005
if ((func = instance_getattr(self, nextstr)) != NULL) {
19302006
PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);

0 commit comments

Comments
 (0)