This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Ensure mktime sets errno on error (bug 23789)
Hi Zack,
On Thu, 25 Oct 2018 09:37:08 -0400, Zack Weinberg <zackw@panix.com>
wrote :
> On Thu, Oct 25, 2018 at 8:59 AM Albert ARIBAUD <albert.aribaud@3adev.fr> wrote:
> > >
> > > This patch is not correct since -1 is a valid time_t value, and a result of -1
> > > does not necessarily indicate time_t overflow.
> >
> > While a do agree that -1 is a valid time_t value in general terms, in
> > the specific case of the mktime() return values, -1 is the value
> > returned on error
>
> Yes, but it is *also* a value that can be returned as the result of a
> *successful* call to mktime. Specifically, I believe this program is
> required to print
>
> tv=-1 errno=0
>
> on a system conforming to POSIX, when invoked with the time zone set to UTC:
>
> #include <stdio.h>
> #include <time.h>
> #include <errno.h>
>
> int main(void)
> {
> struct tm tm;
> tm.tm_sec = 59;
> tm.tm_min = 59;
> tm.tm_hour = 23;
> tm.tm_mday = 31;
> tm.tm_mon = 11;
> tm.tm_year = 69;
> tm.tm_wday = 0;
> tm.tm_yday = 0;
> tm.tm_isdst = 0;
>
> errno = 0;
> time_t tv = mktime(&tm);
> int err = errno;
>
> printf("tv=%lld errno=%d\n", (long long)tv, err);
> return 0;
> }
>
> This is similar to how strtol can return LONG_MAX either because the
> input string was exactly the decimal representation of LONG_MAX, or
> because the input string was the representation of a number too large
> for "long"; errno is required *not* to be set to ERANGE in the first
> case.
Hmm... Posix defines the value returned by mktime to be "Seconds since
the Epoch" thus: <http://pubs.opengroup.org/onlinepubs/9699919799/>.
According to this definition, a year below 1970 makes the corresponding
seconds since the epoch value undefined, so I wonder whether the struct
tm above is not outside the allowed limits for mktime().
Cordialement,
Albert ARIBAUD
3ADEV