glob: fix heap buffer overflow
[gnulib.git] / lib / verror.c
blobe4b68cb78f0954c6147a4a940c7db843e048244f
1 /* va_list error handler for noninteractive utilities
2 Copyright (C) 2006-2007, 2009-2017 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake. */
19 #include <config.h>
21 #include "verror.h"
22 #include "xvasprintf.h"
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
28 #if ENABLE_NLS
29 # include "gettext.h"
30 # define _(msgid) gettext (msgid)
31 #endif
33 #ifndef _
34 # define _(String) String
35 #endif
37 /* Print a message with 'vfprintf (stderr, FORMAT, ARGS)';
38 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
39 If STATUS is nonzero, terminate the program with 'exit (STATUS)'.
40 Use the globals error_print_progname and error_message_count similarly
41 to error(). */
42 void
43 verror (int status, int errnum, const char *format, va_list args)
45 verror_at_line (status, errnum, NULL, 0, format, args);
48 /* Print a message with 'vfprintf (stderr, FORMAT, ARGS)';
49 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
50 If STATUS is nonzero, terminate the program with 'exit (STATUS)'.
51 If FNAME is not NULL, prepend the message with "FNAME:LINENO:".
52 Use the globals error_print_progname, error_message_count, and
53 error_one_per_line similarly to error_at_line(). */
54 void
55 verror_at_line (int status, int errnum, const char *file,
56 unsigned int line_number, const char *format, va_list args)
58 char *message = xvasprintf (format, args);
59 if (message)
61 /* Until https://sourceware.org/bugzilla/show_bug.cgi?id=2997 is fixed,
62 glibc violates GNU Coding Standards when the file argument to
63 error_at_line is NULL. */
64 if (file)
65 error_at_line (status, errnum, file, line_number, "%s", message);
66 else
67 error (status, errnum, "%s", message);
69 else
71 /* EOVERFLOW, EINVAL, and EILSEQ from xvasprintf are signs of
72 serious programmer errors. */
73 error (0, errno, _("unable to display error message"));
74 abort ();
76 free (message);