Bug 24549 – std.process.environment.get(null) segfaults

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-05-13T12:38:02Z
Last change time
2024-05-27T12:02:25Z
Keywords
pull
Assigned to
No Owner
Creator
kinke

Comments

Comment #0 by kinke — 2024-05-13T12:38:02Z
It's of course an invalid input, but a segfault isn't really expected. Tested with Phobos v2.108.1 on Linux.
Comment #1 by schveiguy — 2024-05-13T22:19:07Z
The underlying issue is that `std.internal.cstring.tempCString` returns a `null` pointer if the input string is an empty string with a null pointer. This is surprising, and seems to have been done as a hack to work around issue 14980 I would not expect to get a null pointer when passing in an empty string, I'd expect an empty string. This has a seemingly "unforseen" effect on code that is accepting of a string, and blindly passes to `tempCString` for calling a C lib -- if the library function doesn't handle null pointers, or handles them differently than non-null empty strings, then it may crash or do something unexpected. The Linux `getenv` function is crashing on the null pointer (BTW, this does not happen on MacOS).
Comment #2 by dlang-bot — 2024-05-14T01:13:48Z
@schveiguy updated dlang/phobos pull request #9002 "Fix issue 24549" fixing this issue: - Fix bugzilla issue 24549 -- environment.get(null) segfaults on Linux https://github.com/dlang/phobos/pull/9002
Comment #3 by dlang-bot — 2024-05-16T18:09:12Z
dlang/phobos pull request #9002 "Fix issue 24549 - std.process.environment.get(null) segfaults" was merged into stable: - 6ea12cf3a4b2fb6e3e962a87e1954b949f349a79 by Steven Schveighoffer: Fix bugzilla issue 24549 -- environment.get(null) segfaults on Linux https://github.com/dlang/phobos/pull/9002
Comment #4 by dlang-bot — 2024-05-27T12:02:25Z
dlang/phobos pull request #9006 "merge stable" was merged into master: - 82e8e32b2ff546a86eb3683b1af4ecc5939f633e by Steven Schveighoffer: Fix bugzilla issue 24549 -- environment.get(null) segfaults on Linux https://github.com/dlang/phobos/pull/9006