From f0c19df495aecebde4042dc3b4fbd8ee63b8e467 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 27 Jan 2024 12:41:46 +0100 Subject: container.c: Add minor fixes --- src/container/src/container.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/container/src/container.c b/src/container/src/container.c index a6b8733..1567357 100644 --- a/src/container/src/container.c +++ b/src/container/src/container.c @@ -41,13 +41,19 @@ static int get_file_size(size_t *const sz, FILE *const f) return -1; errno = 0; - const unsigned long val = strtoul(szstr, NULL, 10); + char *end; + const unsigned long val = strtoul(szstr, &end, 10); if (errno) { fprintf(stderr, "%s: strtoul(3): %s\n", __func__, strerror(errno)); return -1; } + else if (*end) + { + fprintf(stderr, "%s: invalid number %s\n", __func__, szstr); + return -1; + } *sz = val; return 0; @@ -123,29 +129,24 @@ static const struct container *find_element(const struct container *const list, static int read_element(const struct container *const list, const size_t n, FILE *const f, bool *const done) { - int ret = -1; long init_off; const struct container *el = NULL; size_t sz; - if (!(el = find_element(list, n, f))) - goto end; - else if (get_file_size(&sz, f)) - goto end; + if (!(el = find_element(list, n, f)) + || get_file_size(&sz, f)) + return -1; else if ((init_off = ftell(f)) < 0) { fprintf(stderr, "%s:%d: fseek failed: %s\n", __func__, __LINE__, strerror(errno)); - goto end; + return -1; } else if (read_file_contents(el, f, init_off, sz)) - goto end; + return -1; done[el - list] = true; - ret = 0; - -end: - return ret; + return 0; } static int read_all_elements(const struct container *const list, const size_t n, -- cgit v1.2.3