Bug 7872 – dmd should warn if `printf` is used on D strings

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-04-09T08:19:32Z
Last change time
2020-03-21T03:56:40Z
Assigned to
No Owner
Creator
Jonas H.

Comments

Comment #0 by jonas — 2012-04-09T08:19:32Z
string foo = "john"; printf("hello %s\n", foo); doesn't work because `printf` expects a zero-terminated string. The compiler should really yield a warning here.
Comment #1 by bearophile_hugs — 2012-04-09T09:36:00Z
(In reply to comment #0) > string foo = "john"; > printf("hello %s\n", foo); > > doesn't work because `printf` expects a zero-terminated string. > > The compiler should really yield a warning here. In D string literals are zero-terminated. So this run correctly: import core.stdc.stdio; void main() { string foo = "john"; printf("hello %s\n", foo.ptr); }
Comment #2 by jonas — 2012-04-09T11:26:57Z
Yeah but I guess it's a common mistake for someone coming from C/C++ so there should be some guidance anyway.
Comment #3 by maxim — 2012-04-09T11:35:19Z
(In reply to comment #1) > In D string literals are zero-terminated. So this run correctly: > If documentation (http://www.prowiki.org/wiki4d/wiki.cgi?action=browse&id=DanielKeep/TextInD&oldid=StringsInD) is not mistaken string literals are zero-terminated in DMD, thus it is compiler-specific feature.
Comment #4 by jonas — 2012-04-09T11:40:53Z
For the record, some discussion on this is happening in the forum too. http://forum.dlang.org/thread/[email protected]
Comment #5 by smjg — 2012-04-11T16:10:42Z
DMD should warn if printf is used full stop. :)
Comment #6 by maxim — 2012-04-14T22:18:48Z
(In reply to comment #3) > (In reply to comment #1) > > In D string literals are zero-terminated. So this run correctly: > > > > If documentation > (http://www.prowiki.org/wiki4d/wiki.cgi?action=browse&id=DanielKeep/TextInD&oldid=StringsInD) > is not mistaken string literals are zero-terminated in DMD, thus it is > compiler-specific feature. I just found that here (http://dlang.org/interfaceToC.html) is written that they actually are zero-terminated in D.
Comment #7 by b2.temp — 2020-02-20T09:15:51Z
it is documented that string literals are zero terminated. In addition warnings are not in the tradition of the compiler.