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

Skip to content

Commit 6ae6d46

Browse files
[3.11] gh-116545: Fix error handling in mkpwent in pwdmodule (GH-116548) (#116594)
gh-116545: Fix error handling in `mkpwent` in `pwdmodule` (GH-116548) (cherry picked from commit ffd79be) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent d781179 commit 6ae6d46

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
@@ -63,53 +63,52 @@ static struct PyModuleDef pwdmodule;
6363

6464
#define DEFAULT_BUFFER_SIZE 1024
6565

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

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

89-
SETS(setIndex++, p->pw_name);
88+
SET_STRING(p->pw_name);
9089
#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
91-
SETS(setIndex++, p->pw_passwd);
90+
SET_STRING(p->pw_passwd);
9291
#else
93-
SETS(setIndex++, "");
92+
SET_STRING("");
9493
#endif
95-
PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
96-
PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
94+
SET_RESULT(_PyLong_FromUid(p->pw_uid));
95+
SET_RESULT(_PyLong_FromGid(p->pw_gid));
9796
#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
98-
SETS(setIndex++, p->pw_gecos);
97+
SET_STRING(p->pw_gecos);
9998
#else
100-
SETS(setIndex++, "");
99+
SET_STRING("");
101100
#endif
102-
SETS(setIndex++, p->pw_dir);
103-
SETS(setIndex++, p->pw_shell);
104-
105-
#undef SETS
101+
SET_STRING(p->pw_dir);
102+
SET_STRING(p->pw_shell);
106103

107-
if (PyErr_Occurred()) {
108-
Py_XDECREF(v);
109-
return NULL;
110-
}
104+
#undef SET_STRING
105+
#undef SET_RESULT
111106

112107
return v;
108+
109+
error:
110+
Py_DECREF(v);
111+
return NULL;
113112
}
114113

115114
/*[clinic input]

0 commit comments

Comments
 (0)