Skip to content

Commit 5cf2fb9

Browse files
committed
[Backport r50683 | neal.norwitz]
Stop INCREFing name, then checking if it's NULL. name (f_name) should never be NULL so assert it. Fix one place where we could have passed NULL. Reported by Klocwork python#66.
1 parent 9169792 commit 5cf2fb9

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

‎Objects/fileobject.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static PyObject *
9898
fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode,
9999
int (*close)(FILE *))
100100
{
101+
assert(name != NULL);
101102
assert(f != NULL);
102103
assert(PyFile_Check(f));
103104
assert(f->f_fp == NULL);
@@ -106,7 +107,7 @@ fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode,
106107
Py_DECREF(f->f_mode);
107108
Py_DECREF(f->f_encoding);
108109

109-
Py_INCREF (name);
110+
Py_INCREF(name);
110111
f->f_name = name;
111112

112113
f->f_mode = PyString_FromString(mode);
@@ -121,7 +122,7 @@ fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode,
121122
Py_INCREF(Py_None);
122123
f->f_encoding = Py_None;
123124

124-
if (f->f_name == NULL || f->f_mode == NULL)
125+
if (f->f_mode == NULL)
125126
return NULL;
126127
f->f_fp = fp;
127128
f = dircheck(f);
@@ -207,7 +208,9 @@ PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
207208
PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
208209
NULL, NULL);
209210
if (f != NULL) {
210-
PyObject *o_name = PyString_FromString(name);
211+
PyObject *o_name = PyString_FromString(name);
212+
if (o_name == NULL)
213+
return NULL;
211214
if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
212215
Py_DECREF(f);
213216
f = NULL;

0 commit comments

Comments
 (0)