D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20766 - empty string literals passed as optional parameter should not be 0 terminated
Summary: empty string literals passed as optional parameter should not be 0 terminated
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-04-25 07:22 UTC by basile-z
Modified: 2020-04-29 05:53 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description basile-z 2020-04-25 07:22:47 UTC
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 RazvanN 2020-04-29 03:06:52 UTC
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 Simen Kjaeraas 2020-04-29 05:53:16 UTC
Of course the else branch can be executed, just call test(null). Like RazvanN said, "" is not the same as null.