Bug 20766 – empty string literals passed as optional parameter should not be 0 terminated

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-04-25T07:22:47Z
Last change time
2020-04-29T05:53:16Z
Assigned to
No Owner
Creator
Basile-z

Comments

Comment #0 by b2.temp — 2020-04-25T07:22:47Z
concrete illustration: --- void main() { test(); } void test(string s = "") { if (s) {} else { assert(false); } s ? {} : assert(false); } --- `""` is actually a pointer to 0x00 and a length of 0. Since `s.ptr` will never be null the else branch (or the last exp of a CondExpr) will never be executed, even when calling test() without argument.
Comment #1 by razvan.nitu1305 — 2020-04-29T03:06:52Z
I don't understand why this is a problem. I think that this is the intendend behavior; an empty string is not a null pointer.
Comment #2 by simen.kjaras — 2020-04-29T05:53:16Z
Of course the else branch can be executed, just call test(null). Like RazvanN said, "" is not the same as null.