Thread overview
[Issue 7872] New: dmd should warn if `printf` is used on D strings
Apr 09, 2012
Jonas H.
Apr 09, 2012
Jonas H.
Apr 09, 2012
Maxim Fomin
Apr 09, 2012
Jonas H.
Apr 10, 2012
Walter Bright
Apr 11, 2012
Stewart Gordon
Apr 15, 2012
Maxim Fomin
April 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7872

           Summary: dmd should warn if `printf` is used on D strings
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: jonas@lophus.org


--- Comment #0 from Jonas H. <jonas@lophus.org> 2012-04-09 08:19:32 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7872


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2012-04-09 09:36:00 PDT ---
(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);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7872



--- Comment #2 from Jonas H. <jonas@lophus.org> 2012-04-09 11:26:57 PDT ---
Yeah but I guess it's a common mistake for someone coming from C/C++ so there should be some guidance anyway.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7872


Maxim Fomin <maxim@maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim@maxim-fomin.ru


--- Comment #3 from Maxim Fomin <maxim@maxim-fomin.ru> 2012-04-09 11:35:19 PDT ---
(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&amp;id=DanielKeep/TextInD&amp;oldid=StringsInD)
is not mistaken string literals are zero-terminated in DMD, thus it is
compiler-specific feature.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7872



--- Comment #4 from Jonas H. <jonas@lophus.org> 2012-04-09 11:40:53 PDT ---
For the record, some discussion on this is happening in the forum too. http://forum.dlang.org/thread/ytoebhnapmcixfdtaoqd@forum.dlang.org

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7872


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com
           Severity|normal                      |enhancement


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 11, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7872


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com


--- Comment #5 from Stewart Gordon <smjg@iname.com> 2012-04-11 16:10:42 PDT ---
DMD should warn if printf is used full stop. :)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7872



--- Comment #6 from Maxim Fomin <maxim@maxim-fomin.ru> 2012-04-14 22:18:48 PDT ---
(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&amp;id=DanielKeep/TextInD&amp;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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------