From 383bd1c5033b466ffcc1a0be766d8a8b003c73e9 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Thu, 6 Dec 2001 08:49:08 +0000 Subject: [PATCH] Update. 2001-12-06 Ulrich Drepper * libio/vasprintf.c (_IO_vasprintf): Free buffer on failure. * assert/assert.c: Check result of __asprintf call and don't use string if it failed. * assert/assert-perr.c: Likewise. * inet/rcmd.c: Likewise. * locale/programs/localedef.c (main): Check result of construct_output_path and exit if it failed. (construct_output_path): Check result of asprintf and mkdir calls and fail if they failed. * posix/getopt.c: Check result of __asprintf calls and fail if they failed. Patch by Dmitry V. Levin . --- ChangeLog | 15 ++++ assert/assert-perr.c | 36 ++++---- assert/assert.c | 36 ++++---- inet/rcmd.c | 105 ++++++++++++++---------- libio/vasprintf.c | 5 +- locale/programs/localedef.c | 8 +- posix/getopt.c | 195 ++++++++++++++++++++++++-------------------- 7 files changed, 236 insertions(+), 164 deletions(-) diff --git a/ChangeLog b/ChangeLog index 222b69e..7b263dc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,18 @@ +2001-12-06 Ulrich Drepper + + * libio/vasprintf.c (_IO_vasprintf): Free buffer on failure. + * assert/assert.c: Check result of __asprintf call and don't use + string if it failed. + * assert/assert-perr.c: Likewise. + * inet/rcmd.c: Likewise. + * locale/programs/localedef.c (main): Check result of + construct_output_path and exit if it failed. + (construct_output_path): Check result of asprintf and mkdir calls and + fail if they failed. + * posix/getopt.c: Check result of __asprintf calls and fail if + they failed. + Patch by Dmitry V. Levin . + 2001-12-05 Ulrich Drepper * sysdeps/generic/strcasecmp.c (__strcasecmp): Little performance diff --git a/assert/assert-perr.c b/assert/assert-perr.c index 597ac5e..1342207 100644 --- a/assert/assert-perr.c +++ b/assert/assert-perr.c @@ -22,6 +22,7 @@ #include #include #include +#include extern const char *__progname; @@ -53,25 +54,30 @@ __assert_perror_fail (int errnum, FATAL_PREPARE; #endif - (void) __asprintf (&buf, _("%s%s%s:%u: %s%sUnexpected error: %s.\n"), - __progname, __progname[0] ? ": " : "", - file, line, - function ? function : "", function ? ": " : "", - __strerror_r (errnum, errbuf, sizeof errbuf)); - - /* Print the message. */ + if (__asprintf (&buf, _("%s%s%s:%u: %s%sUnexpected error: %s.\n"), + __progname, __progname[0] ? ": " : "", + file, line, + function ? function : "", function ? ": " : "", + __strerror_r (errnum, errbuf, sizeof errbuf)) >= 0) + { + /* Print the message. */ #ifdef USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - (void) __fwprintf (stderr, L"%s", buf); - else + if (_IO_fwide (stderr, 0) > 0) + (void) __fwprintf (stderr, L"%s", buf); + else #endif - (void) fputs (buf, stderr); + (void) fputs (buf, stderr); - (void) fflush (stderr); + (void) fflush (stderr); - /* We have to free the buffer since the appplication might catch the - SIGABRT. */ - free (buf); + /* We have to free the buffer since the appplication might catch the + SIGABRT. */ + free (buf); + } + else + /* At least print a minimal message. */ +#define STR_N_LEN(str) str, sizeof (str) - 1 + __libc_write (STDERR_FILENO, STR_N_LEN ("Unexpected error.\n")); abort (); } diff --git a/assert/assert.c b/assert/assert.c index df38245..6a9c4de 100644 --- a/assert/assert.c +++ b/assert/assert.c @@ -21,6 +21,7 @@ #include #include #include +#include extern const char *__progname; @@ -51,25 +52,30 @@ __assert_fail (const char *assertion, const char *file, unsigned int line, FATAL_PREPARE; #endif - (void) __asprintf (&buf, _("%s%s%s:%u: %s%sAssertion `%s' failed.\n"), - __progname, __progname[0] ? ": " : "", - file, line, - function ? function : "", function ? ": " : "", - assertion); - - /* Print the message. */ + if (__asprintf (&buf, _("%s%s%s:%u: %s%sAssertion `%s' failed.\n"), + __progname, __progname[0] ? ": " : "", + file, line, + function ? function : "", function ? ": " : "", + assertion) >= 0) + { + /* Print the message. */ #ifdef USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - (void) __fwprintf (stderr, L"%s", buf); - else + if (_IO_fwide (stderr, 0) > 0) + (void) __fwprintf (stderr, L"%s", buf); + else #endif - (void) fputs (buf, stderr); + (void) fputs (buf, stderr); - (void) fflush (stderr); + (void) fflush (stderr); - /* We have to free the buffer since the appplication might catch the - SIGABRT. */ - free (buf); + /* We have to free the buffer since the appplication might catch the + SIGABRT. */ + free (buf); + } + else + /* At least print a minimal message. */ +#define STR_N_LEN(str) str, sizeof (str) - 1 + __libc_write (STDERR_FILENO, STR_N_LEN ("Unexpected error.\n")); abort (); } diff --git a/inet/rcmd.c b/inet/rcmd.c index 2c0a34d..4010bac 100644 --- a/inet/rcmd.c +++ b/inet/rcmd.c @@ -206,13 +206,17 @@ rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) NULL, 0, NI_NUMERICHOST); - __asprintf (&buf, _("connect to address %s: "), paddr); + if (__asprintf (&buf, _("connect to address %s: "), + paddr) >= 0) + { #ifdef USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf(stderr, L"%s", buf); - else + if (_IO_fwide (stderr, 0) > 0) + __fwprintf(stderr, L"%s", buf); + else #endif - fputs (buf, stderr); + fputs (buf, stderr); + free (buf); + } __set_errno (oerrno); perror(0); ai = ai->ai_next; @@ -220,14 +224,16 @@ rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) paddr, sizeof(paddr), NULL, 0, NI_NUMERICHOST); - __asprintf (&buf, _("Trying %s...\n"), paddr); + if (__asprintf (&buf, _("Trying %s...\n"), paddr) >= 0) + { #ifdef USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else #endif - fputs (buf, stderr); - free (buf); + fputs (buf, stderr); + free (buf); + } continue; } if (refused && timo <= 16) { @@ -267,15 +273,17 @@ rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) if (__write(s, num, strlen(num)+1) != (ssize_t)strlen(num)+1) { char *buf = NULL; - __asprintf (&buf, _("\ -rcmd: write (setting up stderr): %m\n")); + if (__asprintf (&buf, _("\ +rcmd: write (setting up stderr): %m\n")) >= 0) + { #ifdef USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf(stderr, L"%s", buf); - else + if (_IO_fwide (stderr, 0) > 0) + __fwprintf(stderr, L"%s", buf); + else #endif - fputs (buf, stderr); - free (buf); + fputs (buf, stderr); + free (buf); + } (void)__close(s2); goto bad; } @@ -285,19 +293,21 @@ rcmd: write (setting up stderr): %m\n")); if (__poll (pfd, 2, -1) < 1 || (pfd[1].revents & POLLIN) == 0){ char *buf = NULL; - if (errno != 0) - __asprintf(&buf, - _("rcmd: poll (setting up stderr): %m\n")); - else - __asprintf(&buf, - _("poll: protocol failure in circuit setup\n")); + if ((errno != 0 + && __asprintf(&buf, _("\ +rcmd: poll (setting up stderr): %m\n")) >= 0) + || (errno == 0 + && __asprintf(&buf, _("\ +poll: protocol failure in circuit setup\n")) >= 0)) + { #ifdef USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else #endif - fputs (buf, stderr); - free (buf); + fputs (buf, stderr); + free (buf); + } (void)__close(s2); goto bad; } @@ -331,15 +341,17 @@ rcmd: write (setting up stderr): %m\n")); if (rport >= IPPORT_RESERVED || rport < IPPORT_RESERVED / 2){ char *buf = NULL; - __asprintf(&buf, - _("socket: protocol failure in circuit setup\n")); + if (__asprintf(&buf, _("\ +socket: protocol failure in circuit setup\n")) >= 0) + { #ifdef USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else #endif - fputs (buf, stderr); - free (buf); + fputs (buf, stderr); + free (buf); + } goto bad2; } } @@ -350,17 +362,20 @@ rcmd: write (setting up stderr): %m\n")); if (n != 1) { char *buf = NULL; - if (n == 0) - __asprintf(&buf, _("rcmd: %s: short read"), *ahost); - else - __asprintf(&buf, "rcmd: %s: %m\n", *ahost); + if ((n == 0 + && __asprintf(&buf, _("rcmd: %s: short read"), + *ahost) >= 0) + || (n != 0 + && __asprintf(&buf, "rcmd: %s: %m\n", *ahost) >= 0)) + { #ifdef USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else #endif - fputs (buf, stderr); - free (buf); + fputs (buf, stderr); + free (buf); + } goto bad2; } if (c != 0) { diff --git a/libio/vasprintf.c b/libio/vasprintf.c index 1cab485..2e27e82 100644 --- a/libio/vasprintf.c +++ b/libio/vasprintf.c @@ -60,7 +60,10 @@ _IO_vasprintf (result_ptr, format, args) sf._s._free_buffer = (_IO_free_type) free; ret = _IO_vfprintf (&sf._sbf._f, format, args); if (ret < 0) - return ret; + { + free (sf._sbf._f._IO_buf_base); + return ret; + } /* Only use realloc if the size we need is of the same order of magnitude then the memory we allocated. */ needed = sf._sbf._f._IO_write_ptr - sf._sbf._f._IO_write_base + 1; diff --git a/locale/programs/localedef.c b/locale/programs/localedef.c index 180e82c..62dbc1a 100644 --- a/locale/programs/localedef.c +++ b/locale/programs/localedef.c @@ -177,6 +177,8 @@ main (int argc, char *argv[]) /* The parameter describes the output path of the constructed files. If the described files cannot be written return a NULL pointer. */ output_path = construct_output_path (argv[remaining]); + if (output_path == NULL) + error (4, errno, _("cannot create directory for output files")); cannot_write_why = errno; /* Now that the parameters are processed we have to reset the local @@ -374,6 +376,9 @@ construct_output_path (char *path) output_prefix ?: "", LOCALEDIR, (int) (startp - path), path, normal, endp, '\0'); + if (n < 0) + return NULL; + endp = result + n - 1; } else @@ -392,7 +397,8 @@ construct_output_path (char *path) if (errno == ENOENT) { errno = 0; - mkdir (result, 0777); + if (mkdir (result, 0777) < 0) + return NULL; } *endp++ = '/'; diff --git a/posix/getopt.c b/posix/getopt.c index 58ba104..2e929b7 100644 --- a/posix/getopt.c +++ b/posix/getopt.c @@ -685,15 +685,17 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) #if defined _LIBC && defined USE_IN_LIBIO char *buf; - __asprintf (&buf, _("%s: option `%s' is ambiguous\n"), - argv[0], argv[optind]); + if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), + argv[0], argv[optind]) >= 0) + { - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); - free (buf); + free (buf); + } #else fprintf (stderr, _("%s: option `%s' is ambiguous\n"), argv[0], argv[optind]); @@ -721,15 +723,16 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; + int n; #endif if (argv[optind - 1][1] == '-') { /* --option */ #if defined _LIBC && defined USE_IN_LIBIO - __asprintf (&buf, _("\ + n = __asprintf (&buf, _("\ %s: option `--%s' doesn't allow an argument\n"), - argv[0], pfound->name); + argv[0], pfound->name); #else fprintf (stderr, _("\ %s: option `--%s' doesn't allow an argument\n"), @@ -740,10 +743,10 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) { /* +option or -option */ #if defined _LIBC && defined USE_IN_LIBIO - __asprintf (&buf, _("\ + n = __asprintf (&buf, _("\ %s: option `%c%s' doesn't allow an argument\n"), - argv[0], argv[optind - 1][0], - pfound->name); + argv[0], argv[optind - 1][0], + pfound->name); #else fprintf (stderr, _("\ %s: option `%c%s' doesn't allow an argument\n"), @@ -752,12 +755,15 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) } #if defined _LIBC && defined USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); + if (n >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); - free (buf); + free (buf); + } #endif } @@ -778,16 +784,17 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) #if defined _LIBC && defined USE_IN_LIBIO char *buf; - __asprintf (&buf, - _("%s: option `%s' requires an argument\n"), - argv[0], argv[optind - 1]); - - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); + if (__asprintf (&buf, _("\ +%s: option `%s' requires an argument\n"), + argv[0], argv[optind - 1]) >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); - free (buf); + free (buf); + } #else fprintf (stderr, _("%s: option `%s' requires an argument\n"), @@ -821,14 +828,15 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; + int n; #endif if (argv[optind][1] == '-') { /* --option */ #if defined _LIBC && defined USE_IN_LIBIO - __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), - argv[0], nextchar); + n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), + argv[0], nextchar); #else fprintf (stderr, _("%s: unrecognized option `--%s'\n"), argv[0], nextchar); @@ -838,8 +846,8 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) { /* +option or -option */ #if defined _LIBC && defined USE_IN_LIBIO - __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), - argv[0], argv[optind][0], nextchar); + n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), + argv[0], argv[optind][0], nextchar); #else fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), argv[0], argv[optind][0], nextchar); @@ -847,12 +855,15 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) } #if defined _LIBC && defined USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); + if (n >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); - free (buf); + free (buf); + } #endif } nextchar = (char *) ""; @@ -878,14 +889,15 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) { #if defined _LIBC && defined USE_IN_LIBIO char *buf; + int n; #endif if (posixly_correct) { /* 1003.2 specifies the format of this message. */ #if defined _LIBC && defined USE_IN_LIBIO - __asprintf (&buf, _("%s: illegal option -- %c\n"), - argv[0], c); + n = __asprintf (&buf, _("%s: illegal option -- %c\n"), + argv[0], c); #else fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); #endif @@ -893,20 +905,23 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) else { #if defined _LIBC && defined USE_IN_LIBIO - __asprintf (&buf, _("%s: invalid option -- %c\n"), - argv[0], c); + n = __asprintf (&buf, _("%s: invalid option -- %c\n"), + argv[0], c); #else fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); #endif } #if defined _LIBC && defined USE_IN_LIBIO - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); + if (n >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); - free (buf); + free (buf); + } #endif } optopt = c; @@ -939,15 +954,17 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) #if defined _LIBC && defined USE_IN_LIBIO char *buf; - __asprintf (&buf, _("%s: option requires an argument -- %c\n"), - argv[0], c); - - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); + if (__asprintf (&buf, + _("%s: option requires an argument -- %c\n"), + argv[0], c) >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); - free (buf); + free (buf); + } #else fprintf (stderr, _("%s: option requires an argument -- %c\n"), argv[0], c); @@ -1001,15 +1018,16 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) #if defined _LIBC && defined USE_IN_LIBIO char *buf; - __asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), - argv[0], argv[optind]); - - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); + if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), + argv[0], argv[optind]) >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); - free (buf); + free (buf); + } #else fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), argv[0], argv[optind]); @@ -1035,16 +1053,17 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) #if defined _LIBC && defined USE_IN_LIBIO char *buf; - __asprintf (&buf, _("\ + if (__asprintf (&buf, _("\ %s: option `-W %s' doesn't allow an argument\n"), - argv[0], pfound->name); - - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); + argv[0], pfound->name) >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); + + free (buf); + } #else fprintf (stderr, _("\ %s: option `-W %s' doesn't allow an argument\n"), @@ -1067,16 +1086,17 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) #if defined _LIBC && defined USE_IN_LIBIO char *buf; - __asprintf (&buf, _("\ + if (__asprintf (&buf, _("\ %s: option `%s' requires an argument\n"), - argv[0], argv[optind - 1]); - - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); - - free (buf); + argv[0], argv[optind - 1]) >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); + + free (buf); + } #else fprintf (stderr, _("%s: option `%s' requires an argument\n"), @@ -1132,16 +1152,17 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) #if defined _LIBC && defined USE_IN_LIBIO char *buf; - __asprintf (&buf, - _("%s: option requires an argument -- %c\n"), - argv[0], c); - - if (_IO_fwide (stderr, 0) > 0) - __fwprintf (stderr, L"%s", buf); - else - fputs (buf, stderr); + if (__asprintf (&buf, _("\ +%s: option requires an argument -- %c\n"), + argv[0], c) >= 0) + { + if (_IO_fwide (stderr, 0) > 0) + __fwprintf (stderr, L"%s", buf); + else + fputs (buf, stderr); - free (buf); + free (buf); + } #else fprintf (stderr, _("%s: option requires an argument -- %c\n"), -- 2.7.4