godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.46k stars 21.07k forks source link

Empty string in a HashMap results in a crash #85388

Closed darksylinc closed 10 months ago

darksylinc commented 11 months ago

Godot version

4.2.x master [5df98679672c12d96b4ac4d96ee17f1559207401]

System information

Linux - GCC g++ 9.4.0 - Ubuntu 20.04 LTS

Issue description

I somehow managed to get my Windows system into a broken state were the driver reports 145 extensions, but extension #144 is an empty string.

Therefore Godot will crash while initializing Vulkan and looking for requested device extensions.

Steps to reproduce

Execute the following code:

HashMap<CharString, bool> requested_device_extensions;

CharString extension_name("");
CharString extension_name2("Hello");

requested_device_extensions.insert("HashMap must not be empty", true);

if (requested_device_extensions.has(extension_name2))
    printf("Has Hello\n");
if (requested_device_extensions.has(extension_name))
    printf("Has <empty>\n");

It will crash before . Callstack:

1 hash_djb2                                                                                                                                                   hashfuncs.h        64   0x4a135d6 
2 HashMapHasherDefault::hash                                                                                                                                  hashfuncs.h        313  0x4a1decd 
3 HashMap<CharString, bool, HashMapHasherDefault, HashMapComparatorDefault<CharString>, DefaultTypedAllocator<HashMapElement<CharString, bool>>>::_hash       hash_map.h         85   0x4a1dbd9 
4 HashMap<CharString, bool, HashMapHasherDefault, HashMapComparatorDefault<CharString>, DefaultTypedAllocator<HashMapElement<CharString, bool>>>::_lookup_pos hash_map.h         106  0x4a1d8d7 
5 HashMap<CharString, bool, HashMapHasherDefault, HashMapComparatorDefault<CharString>, DefaultTypedAllocator<HashMapElement<CharString, bool>>>::has         hash_map.h         310  0x4a14788 
6 Main::setup                                                                                                                                                 main.cpp           2115 0x4a088d3 
7 main                                                                                                                                                        godot_linuxbsd.cpp 62   0x497f058 

The reason is that CharString decided to not create a pointer at all to save memory. Hence when hash_djb2 tries to read the first byte, it crashes.

Minimal reproduction project

N / A

AThousandShips commented 11 months ago

Got a fix for this, will open a PR in a bit