Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffd79be

Browse files
authored
pythongh-116545: Fix error handling in mkpwent in pwdmodule (python#116548)
1 parent 1cc02ca commit ffd79be

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

Modules/pwdmodule.c

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,53 +64,52 @@ static struct PyModuleDef pwdmodule;
6464

6565
#define DEFAULT_BUFFER_SIZE 1024
6666

67-
static void
68-
sets(PyObject *v, int i, const char* val)
69-
{
70-
if (val) {
71-
PyObject *o = PyUnicode_DecodeFSDefault(val);
72-
PyStructSequence_SET_ITEM(v, i, o);
73-
}
74-
else {
75-
PyStructSequence_SET_ITEM(v, i, Py_None);
76-
Py_INCREF(Py_None);
77-
}
78-
}
79-
8067
static PyObject *
8168
mkpwent(PyObject *module, struct passwd *p)
8269
{
83-
int setIndex = 0;
8470
PyObject *v = PyStructSequence_New(get_pwd_state(module)->StructPwdType);
85-
if (v == NULL)
71+
if (v == NULL) {
8672
return NULL;
73+
}
74+
75+
int setIndex = 0;
76+
77+
#define SET_STRING(VAL) \
78+
SET_RESULT((VAL) ? PyUnicode_DecodeFSDefault((VAL)) : Py_NewRef(Py_None))
8779

88-
#define SETS(i,val) sets(v, i, val)
80+
#define SET_RESULT(CALL) \
81+
do { \
82+
PyObject *item = (CALL); \
83+
if (item == NULL) { \
84+
goto error; \
85+
} \
86+
PyStructSequence_SET_ITEM(v, setIndex++, item); \
87+
} while(0)
8988

90-
SETS(setIndex++, p->pw_name);
89+
SET_STRING(p->pw_name);
9190
#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
92-
SETS(setIndex++, p->pw_passwd);
91+
SET_STRING(p->pw_passwd);
9392
#else
94-
SETS(setIndex++, "");
93+
SET_STRING("");
9594
#endif
96-
PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
97-
PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
95+
SET_RESULT(_PyLong_FromUid(p->pw_uid));
96+
SET_RESULT(_PyLong_FromGid(p->pw_gid));
9897
#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
99-
SETS(setIndex++, p->pw_gecos);
98+
SET_STRING(p->pw_gecos);
10099
#else
101-
SETS(setIndex++, "");
100+
SET_STRING("");
102101
#endif
103-
SETS(setIndex++, p->pw_dir);
104-
SETS(setIndex++, p->pw_shell);
105-
106-
#undef SETS
102+
SET_STRING(p->pw_dir);
103+
SET_STRING(p->pw_shell);
107104

108-
if (PyErr_Occurred()) {
109-
Py_XDECREF(v);
110-
return NULL;
111-
}
105+
#undef SET_STRING
106+
#undef SET_RESULT
112107

113108
return v;
109+
110+
error:
111+
Py_DECREF(v);
112+
return NULL;
114113
}
115114

116115
/*[clinic input]

0 commit comments

Comments
 (0)