modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo
Other
21.92k stars 2.53k forks source link

[stdlib] Add more safeguards in the `String` constructors #2691

Closed gabrieldemarmiesse closed 2 weeks ago

gabrieldemarmiesse commented 4 weeks ago

Related to https://github.com/modularml/mojo/issues/2687

Fix https://github.com/modularml/mojo/issues/2392

Also related to https://github.com/modularml/mojo/issues/2678

Description of the problem:

sddefault

EDIT: Please note that now, an empty string will allocate one byte on the heap for the null terminator. This will degrade the performance of some programs, the tradeoff is more safety. This performance penalty will go away with SSO, as the null terminator will be on the stack, which I hope can be implemented soon-ish when https://github.com/modularml/mojo/issues/2637 is fixed

gabrieldemarmiesse commented 2 weeks ago

I'll close this PR in favor of the SSO work. We can always see after SSO if this is still relevant.