Skip to content

time-util: fix operator precedence in format_timestamp_style() size check - #44676

Merged
bluca merged 1 commit into
systemd:mainfrom
b84330808:fix/time-util-timestamp-size-check
Nov 10, 2026
Merged

bluca merged 1 commit into
systemd:mainfrom
b84330808:fix/time-util-timestamp-size-check

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

The minimum buffer size check in format_timestamp_style() was missing parentheses around the ?: expression, so it was parsed as (3 + 1 + 10 + style == TIMESTAMP_DATE) ? 0 : ..., which never counted the weekday and the date. The check required 12 to 21 bytes instead of 15 to 35.

For normal timestamps the later strftime() calls still catch a short buffer, but for timestamps too large to format the "--- XXXX-XX-XX ..." placeholder is copied with only an assert() guarding the size, so a buffer of 12 to 23 bytes hits the assertion instead of getting NULL back (and would overflow the buffer with NDEBUG). All in-tree callers pass FORMAT_TIMESTAMP_MAX sized buffers, so this is not triggered in practice. I also grepped src/ for the same a + b == C ? x : y pattern; this is the only instance.

The new test calls format_timestamp_style() with every buffer size from 0 up to the result length for each style, checks it returns NULL and does not write past the given size, and checks the full-size result. After rebasing onto current main, meson test test-time-util passes, as do test-calendarspec and test-date.

Found by reading the code; there is no upstream issue for this. Prepared with AI assistance, as in #44652.

…heck

The minimum buffer size check in format_timestamp_style() lacked
parentheses around the conditional expression. Since + binds more
tightly than == and ?:, it was parsed as

    (3 + 1 + 10 + style == TIMESTAMP_DATE) ? 0 : (1 + 8 + ... + 1)

i.e. the weekday and date were never accounted for, and the check only
required 12 to 21 bytes instead of 15 to 35. Usually the strftime()
calls catch a buffer that is too short later on. However, for timestamps
that are too large to be formatted, the "--- XXXX-XX-XX ..." placeholder
is copied with only an assert() guarding the buffer size, so e.g. a 20
byte buffer with TIMESTAMP_PRETTY resulted in an assertion failure
instead of NULL being returned (or in a buffer overflow with NDEBUG).

All callers in the tree use FORMAT_TIMESTAMP_MAX sized buffers, hence
this is not triggered in practice, but let's fix the check and add a
test.
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Reviewed bd48cc1 (architecture, correctness, style; failed: lifetimes, security, tests).

Suggestions

  • src/basic/time-util.c:385 With the fixed minimum, the assert() before the placeholder strcpy() has zero slack for UTC/US_UTC/DATE. PRETTY/US placeholders that fit in 24–25 or 31–32 bytes are also now refused. Replacing the assert with if (l < strlen(xxx[style]) + 1) return NULL;, placed before the generic minimum check, fixes both. (likely fixed)
  • src/test/test-time-util.c:716 Success is only checked at sizeof(buf), so a minimum that is too strict goes unnoticed. Example: PRETTY/US placeholders are now refused at 24/31 bytes. Check success at strlen(result) + 1, ideally inside test_format_timestamp_one(), so the cases in format_timestamp_range are reused rather than listed twice. (likely fixed)

Nits

The review is incomplete, so the next push reviews the full diff again.

Workflow run

@github-actions github-actions Bot added tests util-lib please-review PR is ready for (re-)review by a maintainer labels Nov 5, 2026
Comment thread src/basic/time-util.c Resolved
Comment thread src/test/test-time-util.c Resolved
Comment thread src/test/test-time-util.c Resolved
@poettering poettering added good-to-merge/with-minor-suggestions and removed please-review PR is ready for (re-)review by a maintainer labels Nov 6, 2026
@yuwata

yuwata commented Nov 10, 2026

Copy link
Copy Markdown
Member

Please address the review comments by Claude.

Comment thread src/test/test-time-util.c Resolved
@b84330808
b84330808 force-pushed the fix/time-util-timestamp-size-check branch from 844988f to bd48cc1 Compare November 10, 2026 08:01
@b84330808

Copy link
Copy Markdown
Contributor Author

rebased to main

Validation: test-time-util passes. With only the test changes applied, the new assertion fails for the PRETTY placeholder, confirming the regression test catches the original issue.

@yuwata yuwata added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed and removed good-to-merge/with-minor-suggestions labels Nov 10, 2026
@bluca
bluca merged commit 23c99f4 into systemd:main Nov 10, 2026
93 of 96 checks passed
@github-actions github-actions Bot removed the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants