From 668da3fc0ad7db1c9ef6206f83bf16249bfce091 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Sep 28 2018 08:37:05 +0000 Subject: Explicitly check the return value of getgrgid_r/getgrnam_r Merges: https://pagure.io/libuser/issue/21 An explicit return value check makes the job of static code analyzers easier. --- diff --git a/lib/config.c b/lib/config.c index 29e7120..97221f7 100644 --- a/lib/config.c +++ b/lib/config.c @@ -672,6 +672,7 @@ handle_default_useradd_key(gpointer xkey, gpointer xvalue, gpointer xconfig) char buf[LINE_MAX * 4]; intmax_t val; char *p; + int rv; errno = 0; val = strtoimax(value, &p, 10); @@ -679,8 +680,8 @@ handle_default_useradd_key(gpointer xkey, gpointer xvalue, gpointer xconfig) || (gid_t)val != val) { struct group grp, *g; - getgrnam_r(value, &grp, buf, sizeof(buf), &g); - if (g != NULL) + rv = getgrnam_r(value, &grp, buf, sizeof(buf), &g); + if (rv == 0 && g != NULL) value = g->gr_name; /* else ignore the entry */ } diff --git a/lib/user.c b/lib/user.c index ad2bb09..8aa03b9 100644 --- a/lib/user.c +++ b/lib/user.c @@ -2060,13 +2060,14 @@ lu_get_first_unused_id(struct lu_context *ctx, struct lu_error *error = NULL; do { struct group grp, *err; + int rv; /* There may be read-only sources of user information * on the system, and we want to avoid allocating an ID * that's already in use by a service we can't write * to, so check with NSS first. */ - getgrgid_r(id, &grp, buf, sizeof(buf), &err); - if (err == &grp) { + rv = getgrgid_r(id, &grp, buf, sizeof(buf), &err); + if (rv == 0 && err == &grp) { id++; continue; }