diff options
author | Tuukka Tikkanen <tuukka.tikkanen@linaro.org> | 2014-12-03 07:44:11 +0200 |
---|---|---|
committer | Tuukka Tikkanen <tuukka.tikkanen@linaro.org> | 2014-12-05 09:52:38 +0200 |
commit | c7ba025bae5fd8a324dd3210c8c78c5991f67fc7 (patch) | |
tree | 1fbff5a4cd2c3d8da7c05ae6686ba8e1b3f911c9 | |
parent | 8f4a5b446ec48ce6fb54af2e3d37f0558fee0483 (diff) |
utils: Fix return value handling
Before this patch,
read_int() ignores the return value of fscanf(), file_read_value()
checks for only EOF instead of all failures of a fscanf call and
check_window_size() ignores the return value of an ioctl call.
This patch fixes the error checking for all of the above.
Signed-off-by: Tuukka Tikkanen <tuukka.tikkanen@linaro.org>
Reviewed-by: Amit Kucheria <amit.kucheria@linaro.org>
-rw-r--r-- | utils.c | 15 |
1 files changed, 11 insertions, 4 deletions
@@ -57,6 +57,7 @@ int write_int(const char *path, int val) int read_int(const char *path, int *val) { FILE *f; + int ret; f = fopen(path, "r"); @@ -65,10 +66,15 @@ int read_int(const char *path, int *val) return -1; } - fscanf(f, "%d", val); - + ret = fscanf(f, "%d", val); fclose(f); + if (ret != 1) { + fprintf(stderr, + "Warning: failed to parse integer from %s\n", path); + return -1; + } + return 0; } @@ -113,7 +119,7 @@ int file_read_value(const char *path, const char *name, goto out_free; } - ret = fscanf(file, format, value) == EOF ? -1 : 0; + ret = fscanf(file, format, value) != 1 ? -1 : 0; fclose(file); out_free: @@ -191,7 +197,8 @@ int check_window_size(void) return 0; /* Get terminal window size */ - ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize); + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) == -1) + return -1; if (winsize.ws_col >= 80) return 0; |