frang75 / nappgui_src

SDK for building cross-platform desktop apps in ANSI-C
https://www.nappgui.com
MIT License
442 stars 43 forks source link

Bug in str_repl ? #127

Closed SamSandq closed 3 weeks ago

SamSandq commented 1 month ago

I had problems (bad access crash) with str_repl and tried your example from https://nappgui.com/en/core/string.html#f13

String *str = str_repl("const Product **pr;", "const", "", "*", "", " ", "", 0);

and it crashes too. I'm using v1.4.1 - r4927, if it helps.

Please check.

ADDED:

If I exclude the last pair of strings, i.e.:

String *str = str_repl("const Product **pr;", "const", "", "*", "", 0);

then it does NOT crash!

ADDED2:

Some more testing revealed that the issue might be in the number of pairs; 2 pairs is OK, but 3 or more causes a crash.

frang75 commented 1 month ago

It's a very curious case. It doesn't crash in Windows/Linux, but in macOS does. The last 0 is not detected and a buffer overflow occurs. The only explanation I can find is how va_arg() is interpreted on macOS. If we change the last parameter from 0 to (const char_t*)NULL then no crash.

// Crash (only on macOS)
String *str = str_repl("const Product **pr;", "const", "", "*", "", " ", "", 0); 

// No crash
String *str = str_repl("const Product **pr;", "const", "", "*", "", " ", "", (const char_t*)NULL); 

I have updated the doc. Will be visible in next website update.

frang75 commented 3 weeks ago

Updated documentation. Just change 0 by NULL in last parameter https://nappgui.com/en/core/string.html#f13