]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
scsi_id: use read_line instead of fgets
authorLuca Boccassi <luca.boccassi@microsoft.com>
Fri, 9 Apr 2021 16:55:57 +0000 (17:55 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sun, 11 Apr 2021 14:45:06 +0000 (15:45 +0100)
LGTM warns about it:

"Call to fgets() is potentially dangerous. Use read_line() instead."

src/udev/scsi_id/scsi_id.c

index 3ac76072b09fb1bd015bf81a010bb271f12e192f..2f07a2d99fa4a19e109300b548fc6751206b6ccf 100644 (file)
@@ -21,6 +21,7 @@
 #include "device-nodes.h"
 #include "extract-word.h"
 #include "fd-util.h"
+#include "fileio.h"
 #include "scsi_id.h"
 #include "string-util.h"
 #include "strv.h"
@@ -103,11 +104,9 @@ static void set_type(const char *from, char *to, size_t len) {
  */
 static int get_file_options(const char *vendor, const char *model,
                             int *argc, char ***newargv) {
-        _cleanup_free_ char *buffer = NULL,
-                *vendor_in = NULL, *model_in = NULL, *options_in = NULL; /* read in from file */
+        _cleanup_free_ char *vendor_in = NULL, *model_in = NULL, *options_in = NULL; /* read in from file */
         _cleanup_strv_free_ char **options_argv = NULL;
         _cleanup_fclose_ FILE *f;
-        const char *buf;
         int lineno, r;
 
         f = fopen(config_file, "re");
@@ -120,28 +119,21 @@ static int get_file_options(const char *vendor, const char *model,
                 }
         }
 
-        /*
-         * Allocate a buffer rather than put it on the stack so we can
-         * keep it around to parse any options (any allocated newargv
-         * points into this buffer for its strings).
-         */
-        buffer = malloc(MAX_BUFFER_LEN);
-        if (!buffer)
-                return log_oom();
-
         *newargv = NULL;
         lineno = 0;
         for (;;) {
-                _cleanup_free_ char *key = NULL, *value = NULL;
+                _cleanup_free_ char *buffer = NULL, *key = NULL, *value = NULL;
+                const char *buf;
 
                 vendor_in = model_in = options_in = NULL;
 
-                buf = fgets(buffer, MAX_BUFFER_LEN, f);
-                if (!buf)
+                r = read_line(f, MAX_BUFFER_LEN, &buffer);
+                if (r < 0)
+                        return log_error_errno(r, "read_line() on line %d of %s failed: %m", lineno, config_file);
+                if (r == 0)
                         break;
+                buf = buffer;
                 lineno++;
-                if (buf[strlen(buffer) - 1] != '\n')
-                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Config file line %d too long", lineno);
 
                 while (isspace(*buf))
                         buf++;