HaxeFoundation / HaxeFoundation/haxe

Map.toString stackoverflow

Open
#6,728 1 comment 0 reactions 0 assignees View on GitHub
standard library
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

```haxe
class Test {
static function main() {

var map = new Map();
map['100']=map;
trace(map);
}
}
```
results in ` Uncaught RangeError: Maximum call stack size exceeded` on js.
I did not test on other platforms but looked at source code: and it should fail on most of them.

I believe Std.string() should never fail. For example because it's being called if cast was unsuccessful. So instead of clear invalid cast exception user gets stack overflow (or as in my case hanged program without any error message, because object was huge and browser failed to detect stack overflow before running out of memory.)

I've found somewhat similar issue https://github.com/HaxeFoundation/haxe/issues/4398 And it was not resolved. But I disagree that this cannot be fixed. This is how this can be fixed:

```haxe
var isBeingStringified: Bool = false;

public function toString() : String {

if(isBeingStringified)
return "<...>";
isBeingStringified = true;

var s = new StringBuf();
s.add("{");
var it = keys();
for( i in it ) {
s.add(i);
s.add(" => ");
s.add(Std.string(get(i)));
if( it.hasNext() )
s.add(", ");
}
s.add("}");

isBeingStringified = false;

return s.toString();
}
```

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.