From ec8c425866a164c3e73144751a5071aaf6660664 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 16 Oct 2016 17:11:33 -0400 Subject: Check for errors in -print and friends. --- color.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++------------------ color.h | 9 +++++--- eval.c | 22 ++++++++++++++---- 3 files changed, 81 insertions(+), 29 deletions(-) diff --git a/color.c b/color.c index 6e0902b..de2a1fa 100644 --- a/color.c +++ b/color.c @@ -317,69 +317,104 @@ static const char *file_color(const struct colors *colors, const char *filename, return color; } -static void print_esc(const char *esc, FILE *file) { - fputs("\033[", file); - fputs(esc, file); - fputs("m", file); +static int print_esc(const char *esc, FILE *file) { + if (fputs("\033[", file) == EOF) { + return -1; + } + if (fputs(esc, file) == EOF) { + return -1; + } + if (fputs("m", file) == EOF) { + return -1; + } + + return 0; } -void pretty_print(const struct colors *colors, const struct BFTW *ftwbuf) { +int pretty_print(const struct colors *colors, const struct BFTW *ftwbuf) { const char *path = ftwbuf->path; if (!colors) { - puts(path); - return; + return puts(path) == EOF ? -1 : 0; } const char *filename = path + ftwbuf->nameoff; if (colors->dir) { - print_esc(colors->dir, stdout); + if (print_esc(colors->dir, stdout) != 0) { + return -1; + } + } + if (fwrite(path, 1, ftwbuf->nameoff, stdout) != ftwbuf->nameoff) { + return -1; } - fwrite(path, 1, ftwbuf->nameoff, stdout); if (colors->dir) { - print_esc(colors->reset, stdout); + if (print_esc(colors->reset, stdout) != 0) { + return -1; + } } const char *color = file_color(colors, filename, ftwbuf); if (color) { - print_esc(color, stdout); + if (print_esc(color, stdout) != 0) { + return -1; + } + } + if (fputs(filename, stdout) == EOF) { + return -1; } - fputs(filename, stdout); if (color) { - print_esc(colors->reset, stdout); + if (print_esc(colors->reset, stdout) != 0) { + return -1; + } + } + if (fputs("\n", stdout) == EOF) { + return -1; } - fputs("\n", stdout); + + return 0; } -static void pretty_format(const struct colors *colors, const char *color, const char *format, va_list args) { +static int pretty_format(const struct colors *colors, const char *color, const char *format, va_list args) { if (color) { - print_esc(color, stderr); + if (print_esc(color, stderr) != 0) { + return -1; + } } - vfprintf(stderr, format, args); + if (vfprintf(stderr, format, args) < 0) { + return -1; + } if (color) { - print_esc(colors->reset, stderr); + if (print_esc(colors->reset, stderr) != 0) { + return -1; + } } + + return 0; } -void pretty_warning(const struct colors *colors, const char *format, ...) { +int pretty_warning(const struct colors *colors, const char *format, ...) { va_list args; va_start(args, format); - pretty_format(colors, colors ? colors->warning : NULL, format, args); + int ret = pretty_format(colors, colors ? colors->warning : NULL, format, args); va_end(args); + + return ret; } -void pretty_error(const struct colors *colors, const char *format, ...) { +int pretty_error(const struct colors *colors, const char *format, ...) { va_list args; va_start(args, format); - pretty_format(colors, colors ? colors->error : NULL, format, args); + int ret = pretty_format(colors, colors ? colors->error : NULL, format, args); va_end(args); + + return ret; } void free_colors(struct colors *colors) { diff --git a/color.h b/color.h index 1fd8e19..2ddc838 100644 --- a/color.h +++ b/color.h @@ -35,8 +35,9 @@ struct colors *parse_colors(const char *ls_colors); * The color table to use. * @param ftwbuf * The bftw() data for the current path. + * @return 0 on success, -1 on failure. */ -void pretty_print(const struct colors *colors, const struct BFTW *ftwbuf); +int pretty_print(const struct colors *colors, const struct BFTW *ftwbuf); #if __GNUC__ # define BFS_PRINTF_ATTRIBUTE(f, v) __attribute__((format(printf, f, v))) @@ -53,8 +54,9 @@ void pretty_print(const struct colors *colors, const struct BFTW *ftwbuf); * The format string. * @param ... * The format string's arguments. + * @return 0 on success, -1 on failure. */ -void pretty_warning(const struct colors *colors, const char *format, ...) BFS_PRINTF_ATTRIBUTE(2, 3); +int pretty_warning(const struct colors *colors, const char *format, ...) BFS_PRINTF_ATTRIBUTE(2, 3); /** * Pretty-print an error message. @@ -65,8 +67,9 @@ void pretty_warning(const struct colors *colors, const char *format, ...) BFS_PR * The format string. * @param ... * The format string's arguments. + * @return 0 on success, -1 on failure. */ -void pretty_error(const struct colors *colors, const char *format, ...) BFS_PRINTF_ATTRIBUTE(2, 3); +int pretty_error(const struct colors *colors, const char *format, ...) BFS_PRINTF_ATTRIBUTE(2, 3); /** * Free a color table. diff --git a/eval.c b/eval.c index 65a9186..d66c9f6 100644 --- a/eval.c +++ b/eval.c @@ -633,7 +633,10 @@ bool eval_print(const struct expr *expr, struct eval_state *state) { fill_statbuf(state); } - pretty_print(colors, state->ftwbuf); + if (pretty_print(colors, state->ftwbuf) != 0) { + eval_error(state); + } + return true; } @@ -642,8 +645,16 @@ bool eval_print(const struct expr *expr, struct eval_state *state) { */ bool eval_fprint(const struct expr *expr, struct eval_state *state) { const char *path = state->ftwbuf->path; - fputs(path, expr->file); - fputc('\n', expr->file); + if (fputs(path, expr->file) == EOF) { + goto error; + } + if (fputc('\n', expr->file) == EOF) { + goto error; + } + return true; + +error: + eval_error(state); return true; } @@ -652,7 +663,10 @@ bool eval_fprint(const struct expr *expr, struct eval_state *state) { */ bool eval_print0(const struct expr *expr, struct eval_state *state) { const char *path = state->ftwbuf->path; - fwrite(path, 1, strlen(path) + 1, expr->file); + size_t length = strlen(path) + 1; + if (fwrite(path, 1, length, expr->file) != length) { + eval_error(state); + } return true; } -- cgit v1.2.3