[PATCH 3/3] handle return values of read, write and fgets
Manuel Zerpies
manuel.f.zerpies at ww.stud.uni-erlangen.de
Wed Sep 26 10:32:50 UTC 2012
bump.
On Tue, Jul 31, 2012 at 03:15:51PM +0200, Manuel Zerpies wrote:
> From: Manuel Zerpies <manuel.f.zerpies at ww.stud.uni-erlangen.de>
>
> Check the return values of read(), write() and fgets().
>
> Signed-off-by: Manuel Zerpies <manuel.f.zerpies at ww.stud.uni-erlangen.de>
> ---
> applets/applet_tables.c | 5 ++++-
> applets/usage.c | 7 +++++--
> libbb/lineedit.c | 4 +++-
> libbb/xfuncs_printf.c | 4 +++-
> 4 files changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/applets/applet_tables.c b/applets/applet_tables.c
> index 152d5f4..ffd4a82 100644
> --- a/applets/applet_tables.c
> +++ b/applets/applet_tables.c
> @@ -141,8 +141,11 @@ int main(int argc, char **argv)
> line_old[0] = 0;
> fp = fopen(argv[2], "r");
> if (fp) {
> - fgets(line_old, sizeof(line_old), fp);
> + char *c = fgets(line_old, sizeof(line_old), fp);
> fclose(fp);
> + if (c == NULL) {
> + return 1;
> + }
> }
> sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
> if (strcmp(line_old, line_new) != 0) {
> diff --git a/applets/usage.c b/applets/usage.c
> index 94520ff..faa6b27 100644
> --- a/applets/usage.c
> +++ b/applets/usage.c
> @@ -48,8 +48,11 @@ int main(void)
> qsort(usage_array,
> num_messages, sizeof(usage_array[0]),
> compare_func);
> - for (i = 0; i < num_messages; i++)
> - write(STDOUT_FILENO, usage_array[i].usage, strlen(usage_array[i].usage) + 1);
> + for (i = 0; i < num_messages; i++) {
> + ssize_t r = write(STDOUT_FILENO, usage_array[i].usage, strlen(usage_array[i].usage) + 1);
> + if (r < 0)
> + return 1;
> + }
>
> return 0;
> }
> diff --git a/libbb/lineedit.c b/libbb/lineedit.c
> index b89748a..356c7f1 100644
> --- a/libbb/lineedit.c
> +++ b/libbb/lineedit.c
> @@ -2729,7 +2729,9 @@ int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize)
> {
> fputs(prompt, stdout);
> fflush_all();
> - fgets(command, maxsize, stdin);
> + char *c = fgets(command, maxsize, stdin);
> + if (c == NULL)
> + return 0;
> return strlen(command);
> }
>
> diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
> index 29c963f..9c76fbb 100644
> --- a/libbb/xfuncs_printf.c
> +++ b/libbb/xfuncs_printf.c
> @@ -590,8 +590,10 @@ void FAST_FUNC generate_uuid(uint8_t *buf)
>
> i = open("/dev/urandom", O_RDONLY);
> if (i >= 0) {
> - read(i, buf, 16);
> + ssize_t r = read(i, buf, 16);
> close(i);
> + if (r < 0)
> + return;
> }
> /* Paranoia. /dev/urandom may be missing.
> * rand() is guaranteed to generate at least [0, 2^15) range,
> --
> 1.7.0.4
>
More information about the busybox
mailing list