chiark / gitweb /
sysusers: make sure to reset the returned value when EOF is reached in fget*ent_sane...
authorFranck Bui <fbui@suse.com>
Wed, 18 Apr 2018 16:32:21 +0000 (18:32 +0200)
committerSven Eden <yamakuzure@gmx.net>
Fri, 24 Aug 2018 14:47:08 +0000 (16:47 +0200)
To indicate that the there're no more entries, these wrappers return false but
did leave the passed pointed unmodified.

However EOF is not an error and is a very common case so initialize the output
argument to NULL even in this case so callers don't need to do that.

Fixes: #8721
src/basic/user-util.c

index 905d34b7f51171b6e02d91b4e8496c03c66894df..64cc447212544e3f3c3ee65bf1decbda45e74c2c 100644 (file)
@@ -783,14 +783,11 @@ int fgetpwent_sane(FILE *stream, struct passwd **pw) {
 
         errno = 0;
         p = fgetpwent(stream);
-        if (p == NULL) {
-                if (errno == ENOENT)
-                        return false;
+        if (p == NULL && errno != ENOENT)
                 return errno > 0 ? -errno : -EIO;
-        }
 
         *pw = p;
-        return true;
+        return p != NULL;
 }
 
 int fgetspent_sane(FILE *stream, struct spwd **sp) {
@@ -801,14 +798,11 @@ int fgetspent_sane(FILE *stream, struct spwd **sp) {
 
         errno = 0;
         s = fgetspent(stream);
-        if (s == NULL) {
-                if (errno == ENOENT)
-                        return false;
+        if (s == NULL && errno != ENOENT)
                 return errno > 0 ? -errno : -EIO;
-        }
 
         *sp = s;
-        return true;
+        return s != NULL;
 }
 
 int fgetgrent_sane(FILE *stream, struct group **gr) {
@@ -819,14 +813,11 @@ int fgetgrent_sane(FILE *stream, struct group **gr) {
 
         errno = 0;
         g = fgetgrent(stream);
-        if (g == NULL) {
-                if (errno == ENOENT)
-                        return false;
+        if (g == NULL && errno != ENOENT)
                 return errno > 0 ? -errno : -EIO;
-        }
 
         *gr = g;
-        return true;
+        return g != NULL;
 }
 
 #if ENABLE_GSHADOW
@@ -838,13 +829,10 @@ int fgetsgent_sane(FILE *stream, struct sgrp **sg) {
 
         errno = 0;
         s = fgetsgent(stream);
-        if (s == NULL) {
-                if (errno == ENOENT)
-                        return false;
+        if (s == NULL && errno != ENOENT)
                 return errno > 0 ? -errno : -EIO;
-        }
 
         *sg = s;
-        return true;
+        return s != NULL;
 }
 #endif