changeset 38864:3f2980c4d3f2

maint: fix overflow checking in nap.h * modules/chown-tests: * modules/fchownat-tests, modules/fdutimensat-tests: * modules/futimens-tests, modules/lchown-tests: * modules/stat-time-tests, modules/utime-tests: * modules/utimens-tests, modules/utimensat-tests: Depend on intprops. * tests/nap.h: Include intprops.h. (diff_timespec): Handle overflow properly.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 25 Sep 2017 18:20:44 -0700
parents 25c874f73ccb
children f7f068168823
files ChangeLog modules/chown-tests modules/fchownat-tests modules/fdutimensat-tests modules/futimens-tests modules/lchown-tests modules/stat-time-tests modules/utime-tests modules/utimens-tests modules/utimensat-tests tests/nap.h
diffstat 11 files changed, 32 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Sep 25 18:14:01 2017 -0700
+++ b/ChangeLog	Mon Sep 25 18:20:44 2017 -0700
@@ -1,5 +1,15 @@
 2017-09-25  Paul Eggert  <eggert@cs.ucla.edu>
 
+	maint: fix overflow checking in nap.h
+	* modules/chown-tests:
+	* modules/fchownat-tests, modules/fdutimensat-tests:
+	* modules/futimens-tests, modules/lchown-tests:
+	* modules/stat-time-tests, modules/utime-tests:
+	* modules/utimens-tests, modules/utimensat-tests:
+	Depend on intprops.
+	* tests/nap.h: Include intprops.h.
+	(diff_timespec): Handle overflow properly.
+
 	sys_types: update URL
 	* m4/sys_types_h.m4: Use https: URL.
 
--- a/modules/chown-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/chown-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -7,6 +7,7 @@
 
 Depends-on:
 ignore-value
+intprops
 lstat
 mgetgroups
 nanosleep
--- a/modules/fchownat-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/fchownat-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -8,6 +8,7 @@
 
 Depends-on:
 ignore-value
+intprops
 mgetgroups
 nanosleep
 openat-h
--- a/modules/fdutimensat-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/fdutimensat-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -10,6 +10,7 @@
 Depends-on:
 fcntl-h
 ignore-value
+intprops
 nanosleep
 openat
 timespec
--- a/modules/futimens-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/futimens-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -10,6 +10,7 @@
 gettext-h
 fcntl-h
 ignore-value
+intprops
 nanosleep
 timespec
 dup
--- a/modules/lchown-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/lchown-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -7,6 +7,7 @@
 
 Depends-on:
 ignore-value
+intprops
 mgetgroups
 nanosleep
 stat-time
--- a/modules/stat-time-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/stat-time-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -4,6 +4,7 @@
 tests/nap.h
 
 Depends-on:
+intprops
 nanosleep
 time
 
--- a/modules/utime-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/utime-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -8,6 +8,7 @@
 dup
 gettext-h
 ignore-value
+intprops
 nanosleep
 symlink
 timespec
--- a/modules/utimens-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/utimens-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -11,6 +11,7 @@
 dup
 gettext-h
 ignore-value
+intprops
 nanosleep
 symlink
 timespec
--- a/modules/utimensat-tests	Mon Sep 25 18:14:01 2017 -0700
+++ b/modules/utimensat-tests	Mon Sep 25 18:20:44 2017 -0700
@@ -9,6 +9,7 @@
 
 Depends-on:
 ignore-value
+intprops
 nanosleep
 timespec
 utimecmp
--- a/tests/nap.h	Mon Sep 25 18:14:01 2017 -0700
+++ b/tests/nap.h	Mon Sep 25 18:20:44 2017 -0700
@@ -22,6 +22,8 @@
 # include <limits.h>
 # include <stdbool.h>
 
+# include <intprops.h>
+
 /* Name of the witness file.  */
 #define TEMPFILE BASE "nap.tmp"
 
@@ -38,17 +40,20 @@
   time_t bs = b.tv_sec;
   int ans = a.tv_nsec;
   int bns = b.tv_nsec;
+  int sdiff;
+
+  ASSERT (0 <= ans && ans < 2000000000);
+  ASSERT (0 <= bns && bns < 2000000000);
 
   if (! (bs < as || (bs == as && bns < ans)))
     return 0;
-  if (as - bs <= INT_MAX / 1000000000)
-    {
-      int sdiff = (as - bs) * 1000000000;
-      int usdiff = ans - bns;
-      if (usdiff < INT_MAX - sdiff)
-        return sdiff + usdiff;
-    }
-  return INT_MAX;
+
+  if (INT_SUBTRACT_WRAPV (as, bs, &sdiff)
+      || INT_MULTIPLY_WRAPV (sdiff, 1000000000, &sdiff)
+      || INT_ADD_WRAPV (sdiff, ans - bns, &sdiff))
+    return INT_MAX;
+
+  return sdiff;
 }
 
 /* If DO_WRITE, bump the modification time of the file designated by NAP_FD.