On Tue, Jul 31, 2012 at 3:15 PM,
<[email protected]> wrote:
> From: Manuel Zerpies <[email protected]>
>
> Check the return values of read(), write() and fgets().
>
> Signed-off-by: Manuel Zerpies <[email protected]>
> ---
>  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;
> +       }

You can also add short write handling.
And btw, we have tons of printf's all over the place w/o result check,
want to fix them all? :)


> 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;

Should return -1 (see libbb.h comment).

>         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;

Well, paranoia code below takes care of the possibility
of read error.

>         }
>         /* Paranoia. /dev/urandom may be missing.
>          * rand() is guaranteed to generate at least [0, 2^15) range,



commit b2320370be14811459718b9fe418efed75ea3615
Author: Denys Vlasenko <[email protected]>
Date:   Thu Sep 27 16:03:49 2012 +0200

    lineedit: in !EDITING config, return -1 on fgets error

    Signed-off-by: Denys Vlasenko <[email protected]>

diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index 92bea85..dbe6164 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -2729,7 +2729,8 @@ int FAST_FUNC read_line_input(const char*
prompt, char* command, int maxsize)
 {
        fputs(prompt, stdout);
        fflush_all();
-       fgets(command, maxsize, stdin);
+       if (!fgets(command, maxsize, stdin))
+               return -1;
        return strlen(command);
 }
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to