]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
touch: placate static analyzers: no NULL-deref is possible
authorJim Meyering <meyering@redhat.com>
Tue, 24 May 2011 18:33:27 +0000 (20:33 +0200)
committerJim Meyering <meyering@redhat.com>
Wed, 25 May 2011 06:25:54 +0000 (08:25 +0200)
* src/touch.c (main): Avoid even the hint of possibility that
we'd dereference NULL upon localtime failure.  Coverity reported
the potential, but it appears not to be possible, since posixtime
rejects any time for which the subsequent localtime would return NULL.
See http://thread.gmane.org/gmane.comp.gnu.coreutils.general/1253

src/touch.c

index 49be961e4878fa4084c91128560205edd7a0411a..18f81c8aa80bb1d76471d8c468cedb3d668c5e4d 100644 (file)
@@ -405,12 +405,18 @@ main (int argc, char **argv)
       if (! getenv ("POSIXLY_CORRECT"))
         {
           struct tm const *tm = localtime (&newtime[0].tv_sec);
-          error (0, 0,
-                 _("warning: `touch %s' is obsolete; use "
-                   "`touch -t %04ld%02d%02d%02d%02d.%02d'"),
-                 argv[optind],
-                 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
-                 tm->tm_hour, tm->tm_min, tm->tm_sec);
+
+          /* Technically, it appears that even a deliberate attempt to cause
+             the above localtime to return NULL will always fail because our
+             posixtime implementation rejects all dates for which localtime
+             would fail.  However, skip the warning if it ever fails.  */
+          if (tm)
+            error (0, 0,
+                   _("warning: `touch %s' is obsolete; use "
+                     "`touch -t %04ld%02d%02d%02d%02d.%02d'"),
+                   argv[optind],
+                   tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
+                   tm->tm_hour, tm->tm_min, tm->tm_sec);
         }
 
       optind++;