]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: port some code over to safe_fgetc()
authorLennart Poettering <lennart@poettering.net>
Mon, 17 Dec 2018 10:22:38 +0000 (11:22 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 18 Dec 2018 14:03:00 +0000 (15:03 +0100)
src/basic/fileio.c
src/basic/terminal-util.c
src/machine/machine.c

index b07f4f92a9f531a76fd6a32826ed0bbe2eeb753e..a119e831b472509c1f776d96087649b56a5ffce5 100644 (file)
@@ -731,6 +731,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
 int read_line(FILE *f, size_t limit, char **ret) {
         size_t n = 0, allocated = 0, count = 0;
         _cleanup_free_ char *buffer = NULL;
+        int r;
 
         assert(f);
 
@@ -770,7 +771,7 @@ int read_line(FILE *f, size_t limit, char **ret) {
 
                 for (;;) {
                         EndOfLineMarker eol;
-                        int c;
+                        char c;
 
                         if (n >= limit)
                                 return -ENOBUFS;
@@ -778,16 +779,11 @@ int read_line(FILE *f, size_t limit, char **ret) {
                         if (count >= INT_MAX) /* We couldn't return the counter anymore as "int", hence refuse this */
                                 return -ENOBUFS;
 
-                        errno = 0;
-                        c = fgetc_unlocked(f);
-                        if (c == EOF) {
-                                /* if we read an error, and have no data to return, then propagate the error */
-                                if (ferror_unlocked(f) && n == 0)
-                                        return errno > 0 ? -errno : -EIO;
-
-                                /* EOF is line ending too. */
+                        r = safe_fgetc(f, &c);
+                        if (r < 0)
+                                return r;
+                        if (r == 0)
                                 break;
-                        }
 
                         count++;
 
@@ -813,7 +809,7 @@ int read_line(FILE *f, size_t limit, char **ret) {
                                 if (!GREEDY_REALLOC(buffer, allocated, n + 2))
                                         return -ENOMEM;
 
-                                buffer[n] = (char) c;
+                                buffer[n] = c;
                         }
 
                         n++;
index ceba7d0ff85b8431fc0a92d77fda257bb6741815..0f3812072938f556c6c9c7c7512fcf97d860a844 100644 (file)
@@ -96,7 +96,7 @@ int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
                 new_termios.c_cc[VTIME] = 0;
 
                 if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
-                        int c;
+                        char c;
 
                         if (t != USEC_INFINITY) {
                                 if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) {
@@ -105,17 +105,12 @@ int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
                                 }
                         }
 
-                        errno = 0;
-                        c = fgetc(f);
-                        if (c == EOF)
-                                r = ferror(f) && errno > 0 ? -errno : -EIO;
-                        else
-                                r = 0;
-
+                        r = safe_fgetc(f, &c);
                         (void) tcsetattr(fileno(f), TCSADRAIN, &old_termios);
-
                         if (r < 0)
                                 return r;
+                        if (r == 0)
+                                return -EIO;
 
                         if (need_nl)
                                 *need_nl = c != '\n';
index beb5b3566a92d9d59e7541c4015ececa9a94503a..4f89ac026dbb5bc5406a4cafb1fac0d881328cd5 100644 (file)
@@ -594,7 +594,7 @@ int machine_get_uid_shift(Machine *m, uid_t *ret) {
         uid_t uid_base, uid_shift, uid_range;
         gid_t gid_base, gid_shift, gid_range;
         _cleanup_fclose_ FILE *f = NULL;
-        int k;
+        int k, r;
 
         assert(m);
         assert(ret);
@@ -643,7 +643,10 @@ int machine_get_uid_shift(Machine *m, uid_t *ret) {
                 return -ENXIO;
 
         /* If there's more than one line, then we don't support this mapping. */
-        if (fgetc(f) != EOF)
+        r = safe_fgetc(f, NULL);
+        if (r < 0)
+                return r;
+        if (r != 0) /* Insist on EOF */
                 return -ENXIO;
 
         fclose(f);
@@ -664,7 +667,10 @@ int machine_get_uid_shift(Machine *m, uid_t *ret) {
         }
 
         /* If there's more than one line, then we don't support this file. */
-        if (fgetc(f) != EOF)
+        r = safe_fgetc(f, NULL);
+        if (r < 0)
+                return r;
+        if (r != 0) /* Insist on EOF */
                 return -ENXIO;
 
         /* If the UID and GID mapping doesn't match, we don't support this mapping. */