Bug 22877 – importC: wrong sizeof for string literal
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-03-11T19:06:56Z
Last change time
2023-04-02T01:48:23Z
Keywords
ImportC, pull
Assigned to
No Owner
Creator
duser
Comments
Comment #0 by duser — 2022-03-11T19:06:56Z
int printf(char *, ...);
int main()
{
printf("%d\n", (int)sizeof("a"));
}
this should print 2 (length of the string plus one), but right now it prints the size of a pointer regardless of the string length
Comment #1 by dlang-bot — 2022-03-15T06:03:11Z
@WalterBright created dlang/dmd pull request #13824 "fix Issue 22877 - importC: wrong sizeof for string literal" fixing this issue:
- fix Issue 22877 - importC: wrong sizeof for string literal
https://github.com/dlang/dmd/pull/13824
Comment #2 by dlang-bot — 2022-03-15T08:00:04Z
dlang/dmd pull request #13824 "fix Issue 22877 - importC: wrong sizeof for string literal" was merged into master:
- 53850e9d5ec8cb276308edf1d652c1442d00b8de by Walter Bright:
fix Issue 22877 - importC: wrong sizeof for string literal
https://github.com/dlang/dmd/pull/13824
Comment #3 by me — 2022-04-26T08:22:22Z
With this fix sizeof an address of a string literal is also the length of the string:
```
const int s1 = sizeof("aa");
_Static_assert(s1 == 3, "");
const int s2 = sizeof(&"aa");
_Static_assert(s2 == sizeof(void*), ""); // fails
```
Additionally, the following invalid code is now accepted:
```
char(**s1)[3] = &(&"aa");
char(***s2)[3] = &(&(&"aa"));
// and so on
```
Comment #4 by me — 2022-04-26T08:45:17Z
(In reply to Steven Dwy from comment #3)
> Additionally, the following invalid code is now accepted:
> ```
> char(**s1)[3] = &(&"aa");
> char(***s2)[3] = &(&(&"aa"));
> // and so on
> ```
My bad, seems to actually be from the fix for issue 22070.
Comment #5 by dlang-bot — 2022-04-26T15:53:44Z
@Yoplitein created dlang/dmd pull request #14027 "Amend prior fix for issue 22877" mentioning this issue:
- Amend prior fix for issue 22877
Aligns ImportC's treatment of string literals with C11 by typing them as static arrays
Also fixes `typeof(&"")` returning string length instead of pointer size
https://github.com/dlang/dmd/pull/14027
Comment #6 by dlang-bot — 2022-04-26T23:59:44Z
dlang/dmd pull request #14027 "Amend prior fix for issue 22877" was merged into master:
- ede0d4a4c5d7df7f6f54d20af0c06b221a79f875 by Steven Dwy:
Amend prior fix for issue 22877
Aligns ImportC's treatment of string literals with C11 by typing them as static arrays
Also fixes `typeof(&"")` returning string length instead of pointer size
https://github.com/dlang/dmd/pull/14027
Comment #7 by dlang-bot — 2022-05-01T11:39:51Z
@WalterBright updated dlang/dmd pull request #14049 "fix Issue 23057 - importC: dmd segfault on invalid syntax" mentioning this issue:
- Amend prior fix for issue 22877
Aligns ImportC's treatment of string literals with C11 by typing them as static arrays
Also fixes `typeof(&"")` returning string length instead of pointer size
https://github.com/dlang/dmd/pull/14049
Comment #8 by dlang-bot — 2022-05-02T03:12:02Z
@ljmf00 updated dlang/dmd pull request #14058 "fix(backend): define XMM load/store codegen for short type variants" mentioning this issue:
- Amend prior fix for issue 22877
Aligns ImportC's treatment of string literals with C11 by typing them as static arrays
Also fixes `typeof(&"")` returning string length instead of pointer size
https://github.com/dlang/dmd/pull/14058
Comment #9 by dlang-bot — 2022-05-09T00:51:28Z
@WalterBright updated dlang/dmd pull request #14044 "fix Issue 23058 - importC: cannot take address inside multi-dimension…" mentioning this issue:
- Amend prior fix for issue 22877
Aligns ImportC's treatment of string literals with C11 by typing them as static arrays
Also fixes `typeof(&"")` returning string length instead of pointer size
https://github.com/dlang/dmd/pull/14044
Comment #10 by bugzilla — 2022-09-16T08:05:13Z
(In reply to Steven Dwy from comment #3)
> With this fix sizeof an address of a string literal is also the length of
> the string:
Please open new bugs for new problems.
Comment #11 by bugzilla — 2023-04-02T01:47:28Z
I wish people would open new bugs for new problems. With this one, open closed and reopened multiple times, I have no idea what the actual problem is.