summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-03-11 11:55:09 (GMT)
committerGitHub <noreply@github.com>2024-03-11 11:55:09 (GMT)
commitd130cb49834573fff6a0a63da00e04d0a0cca818 (patch)
tree2ff35f1c3a9ac9770bd2667976f3361cfae78e6d
parent1ddd2f9036fd855f80fe9fe7ccafb9fc38c417f2 (diff)
downloadcpython-d130cb49834573fff6a0a63da00e04d0a0cca818.zip
cpython-d130cb49834573fff6a0a63da00e04d0a0cca818.tar.gz
cpython-d130cb49834573fff6a0a63da00e04d0a0cca818.tar.bz2
[3.12] gh-116545: Fix error handling in `mkpwent` in `pwdmodule` (GH-116548) (#116593)
gh-116545: Fix error handling in `mkpwent` in `pwdmodule` (GH-116548) (cherry picked from commit ffd79bea0f032df5a2e7f75e8c823a09cdc7c7a2) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
-rw-r--r--Modules/pwdmodule.c61
1 files changed, 30 insertions, 31 deletions
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
index cc2e2a4..920259a 100644
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -63,53 +63,52 @@ static struct PyModuleDef pwdmodule;
#define DEFAULT_BUFFER_SIZE 1024
-static void
-sets(PyObject *v, int i, const char* val)
-{
- if (val) {
- PyObject *o = PyUnicode_DecodeFSDefault(val);
- PyStructSequence_SET_ITEM(v, i, o);
- }
- else {
- PyStructSequence_SET_ITEM(v, i, Py_None);
- Py_INCREF(Py_None);
- }
-}
-
static PyObject *
mkpwent(PyObject *module, struct passwd *p)
{
- int setIndex = 0;
PyObject *v = PyStructSequence_New(get_pwd_state(module)->StructPwdType);
- if (v == NULL)
+ if (v == NULL) {
return NULL;
+ }
+
+ int setIndex = 0;
+
+#define SET_STRING(VAL) \
+ SET_RESULT((VAL) ? PyUnicode_DecodeFSDefault((VAL)) : Py_NewRef(Py_None))
-#define SETS(i,val) sets(v, i, val)
+#define SET_RESULT(CALL) \
+ do { \
+ PyObject *item = (CALL); \
+ if (item == NULL) { \
+ goto error; \
+ } \
+ PyStructSequence_SET_ITEM(v, setIndex++, item); \
+ } while(0)
- SETS(setIndex++, p->pw_name);
+ SET_STRING(p->pw_name);
#if defined(HAVE_STRUCT_PASSWD_PW_PASSWD) && !defined(__ANDROID__)
- SETS(setIndex++, p->pw_passwd);
+ SET_STRING(p->pw_passwd);
#else
- SETS(setIndex++, "");
+ SET_STRING("");
#endif
- PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromUid(p->pw_uid));
- PyStructSequence_SET_ITEM(v, setIndex++, _PyLong_FromGid(p->pw_gid));
+ SET_RESULT(_PyLong_FromUid(p->pw_uid));
+ SET_RESULT(_PyLong_FromGid(p->pw_gid));
#if defined(HAVE_STRUCT_PASSWD_PW_GECOS)
- SETS(setIndex++, p->pw_gecos);
+ SET_STRING(p->pw_gecos);
#else
- SETS(setIndex++, "");
+ SET_STRING("");
#endif
- SETS(setIndex++, p->pw_dir);
- SETS(setIndex++, p->pw_shell);
-
-#undef SETS
+ SET_STRING(p->pw_dir);
+ SET_STRING(p->pw_shell);
- if (PyErr_Occurred()) {
- Py_XDECREF(v);
- return NULL;
- }
+#undef SET_STRING
+#undef SET_RESULT
return v;
+
+error:
+ Py_DECREF(v);
+ return NULL;
}
/*[clinic input]