emscripten-core / emscripten-core/emscripten

Behavioral inconsistency of `strptime` between emscripten and glibc/musl

Open
#21,024 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

In short, `strptime("2012/2/30", "%Y/%m/%d", &date)` yields `2012/3/1` in emscripten while `gcc a.c` and `musl-gcc a.c` both give `2012/2/30`. (Possibly UB though.)

**Version of emscripten/emsdk:**
```console
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.51-git (c0c2ca1314672a25699846b4663701bcb6f69cca)
clang version 18.0.0git (/startdir/llvm-project f2464ca317bfeeedddb7cbdea3c2c8ec487890bb)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /opt/emscripten-llvm/bin
```

**Failing command line in full:**
```c
// a.c
#include
#include

int main() {
struct tm date;
strptime("2012/2/30", "%Y/%m/%d", &date);
printf("%d/%d/%d\n", date.tm_year + 1900, date.tm_mon + 1, date.tm_mday);
return 0;
}
```
```console
$ emcc a.c
$ node a.out.js
2012/3/1
```
In comparison, `gcc a.c` and `musl-gcc a.c` both give `2012/2/30`.

**Spec:**
The [POSIX spec](https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html) does not seem to specify what to do when it encounters an invalid number that does not fit into the range, so this is possibly not a but but an undefined behavior instead? (But even if it is a UB, it really helps if emscripten follows the behavior of glibc/musl.)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.