Skip to content

[libc] Handle overflow in localtime and gmtime#27187

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:mktime_fix
Open

[libc] Handle overflow in localtime and gmtime#27187
sbc100 wants to merge 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

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
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.

1 participant