coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] maint: env.c: remove unnecessary use of strchr


From: Jim Meyering
Subject: [PATCH] maint: env.c: remove unnecessary use of strchr
Date: Thu, 26 May 2011 18:10:52 +0200

coverity complained that there was a risk of NULL dereference.
That turned out to be a false positive, but did highlight
the opportunity for improvement:

>From 998f34e8de9f9498d4897e763cbfc06e92b008bc Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Wed, 25 May 2011 22:27:53 +0200
Subject: [PATCH] maint: env.c: remove unnecessary use of strchr

* src/env.c (main): Remove excess (and confusing to static analyzers)
use of strchr.
---
 src/env.c |   18 +++++++++++-------
 1 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/env.c b/src/env.c
index 1eb39a1..9f7a852 100644
--- a/src/env.c
+++ b/src/env.c
@@ -123,13 +123,17 @@ main (int argc, char **argv)
   if (optind < argc && STREQ (argv[optind], "-"))
     ++optind;

-  while (optind < argc && strchr (argv[optind], '='))
-    if (putenv (argv[optind++]))
-      {
-        char *name = argv[optind - 1];
-        *(strchr (name, '=')) = '\0';
-        error (EXIT_CANCELED, errno, _("cannot set %s"), quote (name));
-      }
+  char *eq;
+  while (optind < argc && (eq = strchr (argv[optind], '=')))
+    {
+      if (putenv (argv[optind]))
+        {
+          *eq = '\0';
+          error (EXIT_CANCELED, errno, _("cannot set %s"),
+                 quote (argv[optind]));
+        }
+      optind++;
+    }

   /* If no program is specified, print the environment and exit. */
   if (argc <= optind)
--
1.7.5.2.660.g9f46c



reply via email to

[Prev in Thread] Current Thread [Next in Thread]