emscripten-core / emscripten-core/emscripten

mis-sized arguments in printf family results in format specifier corruption

Open
#19,111 1 comment 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

If a 64-bit int variable is passed to a `%d` specifier, the format specifier may in certain cases get corrupted. In the below example, the specifier snippet `%04d-%02d-%02d` effectively gets corrupted into `%04d-%04d-%02d`, when preceded by `%s` where the argument corresponding to the first `%04d` is oversized.

Not sure this is a bug given that it only occurs when a mis-sized argument is passed, but it seems that error should not corrupt the specifier and could indicate some operation that should be bounded but isn't.

**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.20 (5d878c99921ec247d34fb26a20b5a13d60d69e93)
clang version 16.0.0 (https://github.com/llvm/llvm-project 75767a0f9a926641edbef08e31ec2148ff45da67)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/matt/install/emsdk/upstream/bin

To reproduce:

```c
#include
#include

int main(int argc, char* argv[]) {
int64_t y = 2023;
int m = 3;
int d = 31;

fprintf(stderr, "correct: %04d-%02d-%02d\n", (int)y, m, d);
fprintf(stderr, "incorrect, but expected: %04d-%02d-%02d\n", y, m, d);
fprintf(stderr, "%s: %04d-%02d-%02d\n", "actual", y, m, d);

return 0;
}
```

```sh
> gcc44 -m32 main.c && ./a.out
correct: 2023-03-31
incorrect, but expected: 2023-00-03
actual: 2023-00-03
```

```sh
> emcc main.c && node a.out.js
# ... 2 warnings generated.

correct: 2023-03-31
incorrect, but expected: 2023-00-03
actual: 0000-2023-00

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.