D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7872 - dmd should warn if `printf` is used on D strings
Summary: dmd should warn if `printf` is used on D strings
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-04-09 08:19 UTC by Jonas H.
Modified: 2020-03-21 03:56 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jonas H. 2012-04-09 08:19:32 UTC
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 bearophile_hugs 2012-04-09 09:36:00 UTC
(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 Jonas H. 2012-04-09 11:26:57 UTC
Yeah but I guess it's a common mistake for someone coming from C/C++ so there should be some guidance anyway.
Comment 3 Maxim Fomin 2012-04-09 11:35:19 UTC
(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 Jonas H. 2012-04-09 11:40:53 UTC
For the record, some discussion on this is happening in the forum too. http://forum.dlang.org/thread/ytoebhnapmcixfdtaoqd@forum.dlang.org
Comment 5 Stewart Gordon 2012-04-11 16:10:42 UTC
DMD should warn if printf is used full stop. :)
Comment 6 Maxim Fomin 2012-04-14 22:18:48 UTC
(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 basile-z 2020-02-20 09:15:51 UTC
it is documented that string literals are zero terminated. In addition warnings are not in the tradition of the compiler.