Skip to content

[libc] Handle overflow in localtime and gmtime#27187

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:mktime_fix
Jun 27, 2026
Merged

[libc] Handle overflow in localtime and gmtime#27187
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:mktime_fix

Conversation

@sbc100

@sbc100 sbc100 commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

Modify _localtime_js and _gmtime_js to return success/failure status, and update __localtime_r and __gmtime_r to return NULL when they fail.

Previously, these JS helper functions returned void and would write invalid/NaN values to the tm struct when the input time was out of range of the JS Date object. This could trigger assertions or result in garbage data.

This completes the work started in #26704 (which fixed mktime but left localtime/gmtime unhandled).

Added tests to verify:

  • localtime and gmtime return NULL on overflow.
  • localtime_r does not modify the tm struct on failure.

See: #26704

@sbc100 sbc100 requested a review from kripken June 26, 2026 16:46
@sbc100 sbc100 force-pushed the mktime_fix branch 4 times, most recently from d54f0a9 to fe6a831 Compare June 26, 2026 19:31
Modify `_localtime_js` and `_gmtime_js` to return success/failure
status, and update `__localtime_r` and `__gmtime_r` to return NULL
when they fail.

Previously, these JS helper functions returned `void` and would write
invalid/NaN values to the `tm` struct when the input time was out of
range of the JS `Date` object. This could trigger assertions or result
in garbage data.

This completes the work started in 722a1b7 (which fixed `mktime`
but left `localtime`/`gmtime` unhandled).

Added tests to verify:
- `localtime` and `gmtime` return NULL on overflow.
- `localtime_r` does not modify the `tm` struct on failure.

See: emscripten-core#26704
@sbc100 sbc100 enabled auto-merge (squash) June 26, 2026 23:29
@sbc100 sbc100 merged commit 2f6b893 into emscripten-core:main Jun 27, 2026
39 checks passed
@sbc100 sbc100 deleted the mktime_fix branch June 27, 2026 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants