From: Luca Boccassi Date: Fri, 9 Apr 2021 16:55:57 +0000 (+0100) Subject: scsi_id: use read_line instead of fgets X-Git-Tag: v249-rc1~433 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3f66ded568d7bd06c0c2794766eb5929847f62f4;p=thirdparty%2Fsystemd.git scsi_id: use read_line instead of fgets LGTM warns about it: "Call to fgets() is potentially dangerous. Use read_line() instead." --- diff --git a/src/udev/scsi_id/scsi_id.c b/src/udev/scsi_id/scsi_id.c index 3ac76072b09..2f07a2d99fa 100644 --- a/src/udev/scsi_id/scsi_id.c +++ b/src/udev/scsi_id/scsi_id.c @@ -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++;