From c91e947dbd48907342b1fbffa4f76bdaa3334f0d Mon Sep 17 00:00:00 2001 From: Jacques Vidrine Date: Mon, 17 Nov 2003 04:19:15 +0000 Subject: [PATCH] Detect range errors when using the %s specifier. Previously, LONG_MAX was rejected as a range error, while any values less than LONG_MIN were silently substituted with LONG_MIN. Furthermore, on some platforms `time_t' has less range than `long' (e.g. alpha), which may give incorrect results when parsing some strings. --- lib/libc/stdtime/strptime.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/libc/stdtime/strptime.c b/lib/libc/stdtime/strptime.c index f69847a85be..ddf6b57e38f 100644 --- a/lib/libc/stdtime/strptime.c +++ b/lib/libc/stdtime/strptime.c @@ -64,7 +64,7 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include -#include +#include #include #include #include @@ -444,11 +444,18 @@ label: case 's': { char *cp; + int sverrno; + long n; time_t t; - t = strtol(buf, &cp, 10); - if (t == LONG_MAX) + sverrno = errno; + errno = 0; + n = strtol(buf, &cp, 10); + if (errno == ERANGE || (long)(t = n) != n) { + errno = sverrno; return 0; + } + errno = sverrno; buf = cp; gmtime_r(&t, tm); *GMTp = 1;