diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-01-27 12:41:46 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2024-01-27 17:38:11 +0100 |
| commit | f0c19df495aecebde4042dc3b4fbd8ee63b8e467 (patch) | |
| tree | 2920649fe7fd03c562ff7216acae8a1f1c789aee | |
| parent | 6120d66dd038ea9f3c9b19ce05b772ad8b407d44 (diff) | |
container.c: Add minor fixes
| -rw-r--r-- | src/container/src/container.c | 25 |
1 files 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, |
