Repository navigation
Parse the HTTP Last-Modified header without strptime - #111619
Conversation
RFC 7231 IMF-fixdate always ends in the literal "GMT", but the parser matched it with strptime's %Z. That is not portable: musl's %Z only knows the names of the currently configured local time zone, so a zero-length tzname match consumes nothing, "GMT" is left unparsed, the full-consumption check fails, and the _time virtual column of the url table function is always NULL (04104_url_time_virtual_column failed on every musl configuration). If the time zone database has not been initialized yet, musl's %Z even dereferences a NULL tzname[0]. Match the literal instead; verified to parse identically on musl (with and without tzset) and glibc.
|
Workflow [PR], commit [40ad37e] Summary: ❌
AI ReviewSummaryThis PR replaces Final Verdict✅ No blocking or major issues found. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 189/191 (98.95%) · Uncovered code |
Replace `strptime` in `parseFileInfo` with `tryParseHTTPDate`. `strptime` is a
C function whose `%a`, `%b` and `%Z` specifiers depend on the locale and on the
time zone database, which is what made the `Last-Modified` header unparseable
on musl.
Every form of an `HTTP-date` has a fixed layout and a fixed set of
case-sensitive day and month names, so the new parser checks the layout
directly and builds the timestamp with `DateLUTImpl::tryToMakeDateTime` on the
UTC time zone. Neither `strptime` nor `timegm` is needed.
RFC 9110, 5.6.7 requires a recipient to accept all three forms, so the obsolete
RFC 850 and `asctime` forms are now parsed as well, not only `IMF-fixdate`:
Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate
Sunday, 06-Nov-94 08:49:37 GMT ; obsolete RFC 850 format
Sun Nov 6 08:49:37 1994 ; ANSI C's `asctime` format
The two-digit year of the RFC 850 form is resolved against the current time as
the same section requires: a timestamp that would appear to be more than 50
years in the future denotes the most recent past year with the same last two
digits. `tryParseHTTPDate` therefore has an overload taking an explicit
reference time, so that tests do not depend on the wall clock.
The new integration test `test_storage_url_last_modified` serves a one-row CSV
under a path per form and checks that `_time` is populated from `Last-Modified`
in each of them, and that an absent or malformed header leaves `_time` as
`NULL`.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
RFC 9110, 5.6.7 defines the rule on the whole timestamp: a value that appears to be more than 50 years in the future denotes the most recent past year with the same last two digits. `resolveTwoDigitYear` compared years instead, so a date inside the reference year but past the cutoff day stayed a century ahead. With a reference time of 2026-01-01, `Friday, 02-Jan-76 00:00:00 GMT` resolved to 2076 rather than 1976, which made `_time` wrong by a century for such a `Last-Modified` header. Build the candidate timestamp first and compare it against the reference time plus 50 years, subtracting a century only when it is actually past the cutoff. `ParseHTTPDate.RFC850TwoDigitYear` asserted the year-granularity result for `06-Nov-76`, so it is corrected, and `ParseHTTPDate.RFC850TwoDigitYearCutoff` now pins both sides of the cutoff at 2076-01-01. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`makeTimestamp` bounded the day of month by 31 and left the rest to `DateLUTImpl::tryToMakeDateTime`, which only checks the same bound before indexing the month-start table. A day past the end of its month was therefore folded into the next month instead of being rejected: `Mon, 29 Feb 2021 00:00:00 GMT` parsed as 2021-03-01 and `Sun, 31 Apr 2026 00:00:00 GMT` as 2026-05-01, in all three forms of an `HTTP-date`. That contradicts what this parser promises for a malformed `Last-Modified`: the `_time` virtual column and the URL schema and count caches would take an impossible date for a real modification time. `strptime` with `timegm` behaved the same way before, so this tightens the long-standing behavior. Bound the day by `DateLUTImpl::daysInMonth` for the parsed month and year instead. The century a two-digit RFC 850 year resolves to therefore decides whether 29 February exists, which `ParseHTTPDate.InvalidCalendarDate` pins along with a non-leap 29 February, 31 April, and the last day of every month. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The candidate year was anchored to the century of the reference time and could only ever be moved 100 years into the past, so near the end of a century a small two-digit year resolved a century too early: seen from 1999-12-31, `01-Jan-00` denoted 1900 instead of 2000. Pick the candidate whose full timestamp falls into the (reference - 50 years, reference + 50 years] window instead; the candidates are 100 years apart, so at most one matches. This also chooses the year before validating the day of month, so a `29-Feb-00` seen from 1950 resolves to 2000-02-29 even though 1900 had no 29 February. Co-Authored-By: Claude Fable 5 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 185/186 (99.46%) · Uncovered code |
RFC 9110, 5.6.7 defines `HTTP-date` as case sensitive, but RFC 9111, 4.2 relaxes that for cache recipients: "Although all date formats are specified to be case-sensitive, a cache recipient SHOULD match the field value case-insensitively." `ReadWriteBufferFromHTTP` is such a recipient - `Last-Modified` decides whether a cached schema or row count for a `URL` table is still valid - and the previous `strptime` call folded case for `%a` and `%b`, so a header like `sun, 06 nov 1994 08:49:37 gmt` used to parse and now did not. Match the weekday, the month and the `GMT` literal with `equalsCaseInsensitive`.
strptime
Each of the three forms has a fixed width, checked against a literal sample date. Give those samples names instead of repeating them inline.
|
🕵 CI status of the latest run (commit 40ad37e): the only failures are unrelated to this PR.
No unresolved review threads; the AI review verdict is ✅. The branch is merge-clean against |
ReadWriteBufferFromHTTP::parseFileInfoparsed the HTTPLast-Modifiedheader withstrptimeand matched the trailing literalGMTwith%Z, which is not portable. musl's%Zonly knows the names of the currently configured local time zone, so it consumes nothing, the full-consumption check fails, and the_timevirtual column of theurl/urlClustertable functions is alwaysNULL. Before the time zone database is initialized, musl's%Zcan also dereference a NULLtzname[0]. This is why04104_url_time_virtual_columnfailed on every musl configuration.Replace
strptimewith a dedicated parser,tryParseHTTPDate. It accepts all three forms RFC 9110, 5.6.7 requires a recipient to handle -IMF-fixdate,rfc850-dateandasctime-date- resolves therfc850-datetwo-digit year against the 50-year sliding window, rejects calendar dates that do not exist instead of folding them into the next month, and matches the weekday, month andGMTtokens case-insensitively as RFC 9111, 4.2 asks of cache recipients. An absent or unparsable header leaves_timeasNULL.Covered by the existing
04104_url_time_virtual_column, the newsrc/IO/tests/gtest_parse_http_date.cpp, and the new integration testtest_storage_url_last_modified.Bugfix validation (unit tests)isERROR(inconclusive), not a refutation, and it cannot be made to pass here. That job overlays every changedsrc/**/tests/*.cppfile onto the merge-base and buildsunit_tests_dbmswithout the fix;gtest_parse_http_date.cppteststryParseHTTPDate, which does not exist at the merge-base, so the before-binary cannot compile. A unit test of a newly added function can never build against the merge-base, so the runtime reproduction is delegated to the integration testtest_storage_url_last_modified, which drives the realurltable function against a server emitting eachHTTP-dateform.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the
_timevirtual column of theurl/urlClustertable functions always beingNULLon musl-based builds, caused by non-portable parsing of the HTTPLast-Modifiedheader.Version info
26.8.1.1193(included in26.8and later)