[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/2] maint: add syntax-check rule to help avoid misuse of EXIT_FA
From: |
Jim Meyering |
Subject: |
[PATCH 2/2] maint: add syntax-check rule to help avoid misuse of EXIT_FAILURE |
Date: |
Tue, 03 Jul 2012 20:19:17 +0200 |
Inspired by the EXIT_FAILURE problem Pádraig spotted
and that Paul just fixed, I wrote a new syntax-check rule
to help avoid that sort of problem. The following programs
must avoid most new uses of EXIT_FAILURE:
chroot.c
env.c
expr.c
ls.c
nice.c
nohup.c
printenv.c
setuidgid.c
stdbuf.c
system.h
timeout.c
tty.c
This new syntax-check rule caught a minor problem in
our helper setuidgid (not installed) and I fixed another
minor problem in the vicinity (dead code on error path).
Here are the two patches:
>From fc895b0772c80dfcb099325788478b965bae2196 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Tue, 3 Jul 2012 20:13:09 +0200
Subject: [PATCH 1/2] maint: fix minor bugs in helper program, setuidgid
* src/setuidgid.c (main): Fix two error-before-usage calls not to exit.
Exit with status SETUIDGID_FAILURE (not EXIT_FAILURE) consistently.
---
src/setuidgid.c | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
diff --git a/src/setuidgid.c b/src/setuidgid.c
index 39044ce..4dc055b 100644
--- a/src/setuidgid.c
+++ b/src/setuidgid.c
@@ -107,7 +107,7 @@ main (int argc, char **argv)
{
if (! (xstrtoul (gr, &ptr, 10, &tmp_ul, NULL) == LONGINT_OK
&& tmp_ul <= GID_T_MAX))
- error (EXIT_FAILURE, 0, _("invalid group %s"),
+ error (SETUIDGID_FAILURE, 0, _("invalid group %s"),
quote (gr));
if (n_gids == n_gids_allocated)
gids = X2NREALLOC (gids, &n_gids_allocated);
@@ -159,8 +159,7 @@ main (int argc, char **argv)
pwd = getpwnam (user);
if (pwd == NULL)
{
- error (SETUIDGID_FAILURE, errno,
- _("unknown user-ID: %s"), quote (user));
+ error (0, errno, _("unknown user-ID: %s"), quote (user));
usage (SETUIDGID_FAILURE);
}
uid = pwd->pw_uid;
@@ -170,7 +169,7 @@ main (int argc, char **argv)
pwd = getpwuid (uid);
if (pwd == NULL)
{
- error (SETUIDGID_FAILURE, errno,
+ error (0, errno,
_("to use user-ID %s you need to use -g too"), quote
(user));
usage (SETUIDGID_FAILURE);
}
@@ -181,8 +180,8 @@ main (int argc, char **argv)
{
int n = xgetgroups (pwd->pw_name, pwd->pw_gid, &gids);
if (n <= 0)
- error (EXIT_FAILURE, errno, _("failed to get groups for user %s"),
- quote (pwd->pw_name));
+ error (SETUIDGID_FAILURE, errno,
+ _("failed to get groups for user %s"), quote (pwd->pw_name));
n_gids = n;
}
--
1.7.11.1.104.ge7b44f1
>From 74427c76d7be2a5522a8d24e29bfa63c299dbbce Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Tue, 3 Jul 2012 20:14:56 +0200
Subject: [PATCH 2/2] maint: add syntax-check rule to help avoid misuse of
EXIT_FAILURE
* cfg.mk (sc_some_programs_must_avoid_exit_failure): New rule,
to help us avoid using EXIT_FAILURE in programs like sort, ls, nohup,
timeout, env, etc. that use different exit codes in many cases.
---
cfg.mk | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/cfg.mk b/cfg.mk
index ef8a2c8..6620eaf 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -319,6 +319,16 @@ sc_prohibit_test_backticks:
halt='use $$(...), not `...` in tests/' \
$(_sc_search_regexp)
+# Programs like sort, ls, expr use PROG_FAILURE in place of EXIT_FAILURE.
+# Others, use the EXIT_CANCELED, EXIT_ENOENT, etc. macros defined in system.h.
+# In those programs, ensure that EXIT_FAILURE is not used by mistake.
+sc_some_programs_must_avoid_exit_failure:
+ @grep -nw EXIT_FAILURE \
+ $$(git grep -El '[^T]_FAILURE|EXIT_CANCELED' src) \
+ | grep -vE '= EXIT_FAILURE|exit \(.* \?' | grep . \
+ && { echo '$(ME): do not use EXIT_FAILURE in the above' \
+ 1>&2; exit 1; } || :
+
# Exempt the contents of any usage function from the following.
_continued_string_col_1 = \
s/^usage .*?\n}//ms;/\\\n\w/ and print ("$$ARGV\n"),$$e=1;END{$$e||=0;exit $$e}
--
1.7.11.1.104.ge7b44f1
- [PATCH 2/2] maint: add syntax-check rule to help avoid misuse of EXIT_FAILURE,
Jim Meyering <=