Page MenuHomeFreeBSD

strptime: fix %C, %y, and %Y permutations
ClosedPublic

Authored by yuripv on Oct 13 2018, 9:20 PM.
Tags
None
Referenced Files
Unknown Object (File)
Oct 18 2024, 7:00 AM
Unknown Object (File)
Oct 1 2024, 4:22 PM
Unknown Object (File)
Sep 26 2024, 4:30 AM
Unknown Object (File)
Sep 17 2024, 5:53 PM
Unknown Object (File)
Sep 2 2024, 2:26 PM
Unknown Object (File)
Aug 14 2024, 2:52 PM
Unknown Object (File)
Aug 7 2024, 8:12 PM
Unknown Object (File)
Jul 29 2024, 4:15 PM
Subscribers

Details

Summary

Found by tests/lib/libc/time (not shipped yet).

We fail the following century/year test cases:

strptime("x2084y", "x%C%yy", tm): incorrect tm_year: expected: 184, but got: 84
strptime("x8420y", "x%y%Cy", tm): incorrect tm_year: expected: 184, but got: 100
strptime("%20845", "%%%C%y5", tm): incorrect tm_year: expected: 184, but got: 84

The problem here is that we don't check if we have %C conversion in the format string when processing %y, and as %C can come *after* %y, fix this by calculating tm_year after we have finished processing the format string.

Also do NOT fail if resulting tm_year is negative based on the APPLICATION USAGE in http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/time.h.html (glibc does the same):

tm_year is a signed value; therefore, years before 1900 may be represented.

Konstantin, it's not clear to me who else can/should be added to reviewers, need an advise here (hackers@?).

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

So we apparently have Y2K bug ?

lib/libc/stdtime/strptime.c
98 ↗(On Diff #49112)

style(9) requests that initialization is not done in declarations.

609 ↗(On Diff #49112)

... and use flags instead of the comparision with -1, in style of other places ?

yuripv added inline comments.
lib/libc/stdtime/strptime.c
609 ↗(On Diff #49112)

There's a problem with using flags -- they are set by e.g. %c, which does NOT modify century/year by itself, and rather uses recursion.

yuripv added inline comments.
lib/libc/stdtime/strptime.c
98 ↗(On Diff #49112)

Will change, it's just that surroundings not following the rule made me forget about it :-)

In D17550#374655, @kib wrote:

So we apparently have Y2K bug ?

If you mean the negative values for tm_year, then it likely should be called Y1.9K bug. Otherwise, we seem to have followed the standard, just didn't care about %C and %y both appearing in the format string in arbitrary order.

In D17550#374655, @kib wrote:

So we apparently have Y2K bug ?

If you mean the negative values for tm_year, then it likely should be called Y1.9K bug. Otherwise, we seem to have followed the standard, just didn't care about %C and %y both appearing in the format string in arbitrary order.

I mean that %y%C leads to invalid century.

In D17550#374675, @kib wrote:
In D17550#374655, @kib wrote:

So we apparently have Y2K bug ?

If you mean the negative values for tm_year, then it likely should be called Y1.9K bug. Otherwise, we seem to have followed the standard, just didn't care about %C and %y both appearing in the format string in arbitrary order.

I mean that %y%C leads to invalid century.

Ah, it certainly looks like that, yes, but really, we only get the value from whichever conversion comes last, %C or %y.

This revision is now accepted and ready to land.Oct 14 2018, 6:52 PM
This revision was automatically updated to reflect the committed changes.