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.
# include <limits.h>
# include <stdbool.h>
+# include <intprops.h>
+
/* Name of the witness file. */
#define TEMPFILE BASE "nap.tmp"
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.