summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2016-10-04 16:39:41 (GMT)
committerPaul Eggert <eggert@cs.ucla.edu>2016-10-04 16:42:54 (GMT)
commit04771cbf71ced408965f3da4e8c80c67ad3cd936 (patch)
treed2e6056b1fb5a3fb1e082ab127e801234b1c63f7
parent56bad7471db9e57232c6853a5c5e0a6385e17905 (diff)
downloadgrep-04771cbf71ced408965f3da4e8c80c67ad3cd936.zip
grep-04771cbf71ced408965f3da4e8c80c67ad3cd936.tar.gz
grep-04771cbf71ced408965f3da4e8c80c67ad3cd936.tar.bz2
grep: die more systematically
* src/die.h: New file. * src/dfasearch.c, src/grep.c, src/pcresearch.c: Include die.h. * src/dfasearch.c (dfaerror): * src/grep.c (context_length_arg, add_count, prline, setmatcher, main): * src/pcresearch.c (jit_exec, Pcompile, Pexecute): Use 'die' instead of 'error' when exiting. * src/pcresearch.c: Do not include verify.h. (die): Remove; now in die.h. * src/search.h: Do not include error.h here, since this file does not use anything defined in error.h. Instead, dfasearch.c, which uses error.h's symbols, now includes error.h directly.
-rw-r--r--src/dfasearch.c8
-rw-r--r--src/die.h29
-rw-r--r--src/grep.c25
-rw-r--r--src/pcresearch.c46
-rw-r--r--src/search.h1
5 files changed, 61 insertions, 48 deletions
diff --git a/src/dfasearch.c b/src/dfasearch.c
index 96be58f..d41b6fd 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -21,6 +21,8 @@
#include <config.h>
#include "intprops.h"
#include "search.h"
+#include "die.h"
+#include <error.h>
struct localeinfo localeinfo;
@@ -53,11 +55,7 @@ static bool begline;
void
dfaerror (char const *mesg)
{
- error (EXIT_TROUBLE, 0, "%s", mesg);
-
- /* notreached */
- /* Tell static analyzers that this function does not return. */
- abort ();
+ die (EXIT_TROUBLE, 0, "%s", mesg);
}
/* For now, the sole dfawarn-eliciting condition (use of a regexp
diff --git a/src/die.h b/src/die.h
new file mode 100644
index 0000000..b476c98
--- /dev/null
+++ b/src/die.h
@@ -0,0 +1,29 @@
+/* Report an error and exit.
+ Copyright 2016 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+#ifndef DIE_H
+
+#include <error.h>
+#include <verify.h>
+
+/* Like 'error (STATUS, ...)', except STATUS must be a nonzero constant.
+ This may pacify the compiler or help it generate better code. */
+#define die(status, ...) \
+ verify_expr (status, (error (status, __VA_ARGS__), assume (false)))
+
+#endif /* DIE_H */
diff --git a/src/grep.c b/src/grep.c
index 3b85561..f7cadfb 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -32,6 +32,7 @@
#include "c-ctype.h"
#include "closeout.h"
#include "colorize.h"
+#include "die.h"
#include "error.h"
#include "exclude.h"
#include "exitfail.h"
@@ -763,8 +764,8 @@ context_length_arg (char const *str, intmax_t *out)
break;
/* Fall through. */
default:
- error (EXIT_TROUBLE, 0, "%s: %s", str,
- _("invalid context length argument"));
+ die (EXIT_TROUBLE, 0, "%s: %s", str,
+ _("invalid context length argument"));
}
}
@@ -828,7 +829,7 @@ add_count (uintmax_t a, uintmax_t b)
{
uintmax_t sum = a + b;
if (sum < a)
- error (EXIT_TROUBLE, 0, _("input is too large to count"));
+ die (EXIT_TROUBLE, 0, _("input is too large to count"));
return sum;
}
@@ -1309,7 +1310,7 @@ prline (char *beg, char *lim, char sep)
fflush_errno ();
if (stdout_errno)
- error (EXIT_TROUBLE, stdout_errno, _("write error"));
+ die (EXIT_TROUBLE, stdout_errno, _("write error"));
lastout = lim;
}
@@ -2055,7 +2056,7 @@ setmatcher (char const *m)
struct matcher const *p;
if (matcher && !STREQ (matcher, m))
- error (EXIT_TROUBLE, 0, _("conflicting matchers specified"));
+ die (EXIT_TROUBLE, 0, _("conflicting matchers specified"));
for (p = matchers; p->compile; p++)
if (STREQ (m, p->name))
@@ -2066,7 +2067,7 @@ setmatcher (char const *m)
return;
}
- error (EXIT_TROUBLE, 0, _("invalid matcher %s"), m);
+ die (EXIT_TROUBLE, 0, _("invalid matcher %s"), m);
}
/* Find the white-space-separated options specified by OPTIONS, and
@@ -2420,7 +2421,7 @@ main (int argc, char **argv)
else if (STREQ (optarg, "skip"))
devices = SKIP_DEVICES;
else
- error (EXIT_TROUBLE, 0, _("unknown devices method"));
+ die (EXIT_TROUBLE, 0, _("unknown devices method"));
break;
case 'E':
@@ -2499,7 +2500,7 @@ main (int argc, char **argv)
case 'f':
fp = STREQ (optarg, "-") ? stdin : fopen (optarg, O_TEXT ? "rt" : "r");
if (!fp)
- error (EXIT_TROUBLE, errno, "%s", optarg);
+ die (EXIT_TROUBLE, errno, "%s", optarg);
for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
;
keys = xrealloc (keys, keyalloc);
@@ -2512,7 +2513,7 @@ main (int argc, char **argv)
}
fread_errno = errno;
if (ferror (fp))
- error (EXIT_TROUBLE, fread_errno, "%s", optarg);
+ die (EXIT_TROUBLE, fread_errno, "%s", optarg);
if (fp != stdin)
fclose (fp);
/* Append final newline if file ended in non-newline. */
@@ -2549,7 +2550,7 @@ main (int argc, char **argv)
break;
default:
- error (EXIT_TROUBLE, 0, _("invalid max count"));
+ die (EXIT_TROUBLE, 0, _("invalid max count"));
}
break;
@@ -2606,7 +2607,7 @@ main (int argc, char **argv)
else if (STREQ (optarg, "without-match"))
binary_files = WITHOUT_MATCH_BINARY_FILES;
else
- error (EXIT_TROUBLE, 0, _("unknown binary-files type"));
+ die (EXIT_TROUBLE, 0, _("unknown binary-files type"));
break;
case COLOR_OPTION:
@@ -2647,7 +2648,7 @@ main (int argc, char **argv)
if (add_exclude_file (add_exclude, excluded_patterns[cmd],
optarg, exclude_options (cmd), '\n')
!= 0)
- error (EXIT_TROUBLE, errno, "%s", optarg);
+ die (EXIT_TROUBLE, errno, "%s", optarg);
}
break;
diff --git a/src/pcresearch.c b/src/pcresearch.c
index 6e192e3..928c22c 100644
--- a/src/pcresearch.c
+++ b/src/pcresearch.c
@@ -20,19 +20,7 @@
#include <config.h>
#include "search.h"
-#include "verify.h"
-
-/* Wrap a fatal 3-argument use of "error" (with literal nonzero exit
- status), so that static analyzers like clang-analyzer and GCC 7's
- -Wimplicit-fallthrough know this "function" does not return. */
-#define die(st, fmt, arg) \
- do \
- { \
- verify ((st) != 0); \
- error (0, fmt, arg); \
- exit (st); \
- } \
- while (0)
+#include "die.h"
#if HAVE_LIBPCRE
# include <pcre.h>
@@ -80,8 +68,8 @@ jit_exec (char const *subject, int search_bytes, int search_offset,
pcre_jit_stack_free (jit_stack);
jit_stack = pcre_jit_stack_alloc (old_size, new_size);
if (!jit_stack)
- error (EXIT_TROUBLE, 0,
- _("failed to allocate memory for the PCRE JIT stack"));
+ die (EXIT_TROUBLE, 0,
+ _("failed to allocate memory for the PCRE JIT stack"));
pcre_assign_jit_stack (extra, NULL, jit_stack);
continue;
}
@@ -105,9 +93,9 @@ void
Pcompile (char const *pattern, size_t size)
{
#if !HAVE_LIBPCRE
- error (EXIT_TROUBLE, 0, "%s",
- _("support for the -P option is not compiled into "
- "this --disable-perl-regexp binary"));
+ die (EXIT_TROUBLE, 0,
+ _("support for the -P option is not compiled into "
+ "this --disable-perl-regexp binary"));
#else
int e;
char const *ep;
@@ -128,15 +116,14 @@ Pcompile (char const *pattern, size_t size)
if (1 < MB_CUR_MAX)
{
if (! localeinfo.using_utf8)
- error (EXIT_TROUBLE, 0,
- _("-P supports only unibyte and UTF-8 locales"));
+ die (EXIT_TROUBLE, 0, _("-P supports only unibyte and UTF-8 locales"));
multibyte_locale = true;
flags |= PCRE_UTF8;
}
/* FIXME: Remove these restrictions. */
if (memchr (pattern, '\n', size))
- error (EXIT_TROUBLE, 0, _("the -P option only supports a single pattern"));
+ die (EXIT_TROUBLE, 0, _("the -P option only supports a single pattern"));
if (! eolbyte)
{
bool escaped = false;
@@ -147,8 +134,8 @@ Pcompile (char const *pattern, size_t size)
else
{
if (*p == '$' || (*p == '^' && !after_unescaped_left_bracket))
- error (EXIT_TROUBLE, 0,
- _("unescaped ^ or $ not supported with -Pz"));
+ die (EXIT_TROUBLE, 0,
+ _("unescaped ^ or $ not supported with -Pz"));
escaped = *p == '\\';
after_unescaped_left_bracket = *p == '[';
}
@@ -186,15 +173,15 @@ Pcompile (char const *pattern, size_t size)
cre = pcre_compile (re, flags, &ep, &e, pcre_maketables ());
if (!cre)
- error (EXIT_TROUBLE, 0, "%s", ep);
+ die (EXIT_TROUBLE, 0, "%s", ep);
extra = pcre_study (cre, PCRE_STUDY_JIT_COMPILE, &ep);
if (ep)
- error (EXIT_TROUBLE, 0, "%s", ep);
+ die (EXIT_TROUBLE, 0, "%s", ep);
# if PCRE_STUDY_JIT_COMPILE
if (pcre_fullinfo (cre, extra, PCRE_INFO_JIT, &e))
- error (EXIT_TROUBLE, 0, _("internal error (should never happen)"));
+ die (EXIT_TROUBLE, 0, _("internal error (should never happen)"));
/* The PCRE documentation says that a 32 KiB stack is the default. */
if (e)
@@ -216,8 +203,7 @@ Pexecute (char *buf, size_t size, size_t *match_size,
{
#if !HAVE_LIBPCRE
/* We can't get here, because Pcompile would have been called earlier. */
- error (EXIT_TROUBLE, 0, _("internal error"));
- return -1;
+ die (EXIT_TROUBLE, 0, _("internal error"));
#else
int sub[NSUB];
char const *p = start_ptr ? start_ptr : buf;
@@ -260,7 +246,7 @@ Pexecute (char *buf, size_t size, size_t *match_size,
}
if (too_big)
- error (EXIT_TROUBLE, 0, _("exceeded PCRE's line length limit"));
+ die (EXIT_TROUBLE, 0, _("exceeded PCRE's line length limit"));
for (;;)
{
@@ -365,7 +351,7 @@ Pexecute (char *buf, size_t size, size_t *match_size,
If anyone cares to provide sample grep usage that can trigger
particular PCRE errors, we can add to the list (above) of more
detailed diagnostics. */
- error (EXIT_TROUBLE, 0, _("internal PCRE error: %d"), e);
+ die (EXIT_TROUBLE, 0, _("internal PCRE error: %d"), e);
}
return -1;
diff --git a/src/search.h b/src/search.h
index fb4e5c8..b6c1945 100644
--- a/src/search.h
+++ b/src/search.h
@@ -28,7 +28,6 @@
#include <regex.h>
#include "system.h"
-#include "error.h"
#include "grep.h"
#include "dfa.h"
#include "kwset.h"