emscripten-core / emscripten-core/emscripten

_malloc(NaN) is hard to debug

Open
#17,356 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

The following code is hard to debug:

```js
var ptr = _malloc(NaN)
use_pointer_as_if_it_is_valid_memory(ptr)
_free(ptr)
```

(Obviously I didn't have an actual `NaN` in my code. It was more like `_malloc(someTypo * 10)` which turned into `_malloc(NaN)`.)

The code fails with segmentation fault on `_free`.

None of the memory safety tools (`SAFE_HEAP`, `-fsanitize=address`, `-fsanitize=undefined`) pointed me to the fact that `_malloc()` call was wrong.

Could it be improved somehow?

**Version of emscripten/emsdk:**
```
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.14 (4343cbec72b7db283ea3bda1adc6cb1811ae9a73)
clang version 15.0.0 (https://github.com/llvm/llvm-project 7effcbda49ba32991b8955821b8fdbd4f8f303e2)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: /Users/shamrin/src/emsdk/upstream/bin
```

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.