www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - 2 bugs: format / printf

reply bug d.com writes:
I suppose format and printf accept the same format string.  If you comment out
the line marked "here", there's a runtime error with format.

$ cat fmt.d
-----------------------------
import std.string;

int main(char[][] args)
{
byte c = -1;
char ch;
printf(       "0x%02hhX\n", c );  // OK
printf(format("0x%02hhX\n", c));  // runtime Error: std.format formatArg
printf("here"~ch);                // compiler crash
return 0;
}

-----------------------------

$ dmd fmt.d
gdc -c fmt.d -o fmt.o
fmt.d: In function `main':
fmt.d:9: internal compiler error: in rawArray, at d/d-codegen.cc:981
Please submit a full bug report,
with preprocessed source if appropriate.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
Aug 09 2005
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
bug d.com wrote:
 I suppose format and printf accept the same format string.
Not exactly, actually.
 $ cat fmt.d
 -----------------------------
 import std.string;
 
 int main(char[][] args)
 {
 byte c = -1;
 char ch;
 printf(       "0x%02hhX\n", c );  // OK
 printf(format("0x%02hhX\n", c));  // runtime Error: std.format formatArg
 printf("here"~ch);                // compiler crash
 return 0;
 }
 -----------------------------
I'm not surprised you got errors... you are passing D-style strings and slices to printf, which expects C-style strings. For instance you could change this line: to this one: And change this line: to this one: Or, better yet, just import 'std.stdio' and use writef/writefln in place of printf. Then you have no problems, and can even scrap your call to format in this case. So your code becomes: -- Chris Sauls
Aug 09 2005
parent reply bug d.com writes:
Maybe my choose of printf is misleading.  I only want to write a small example
to show the bug.  

For the 2nd bug: at any rate the compile can report errors, but should not
crash.

Let's see the new example:

$ cat fmt.d
-----------------------------
import std.string;

void doNothing(char[] s) {}

int main(char[][] args)
{
int i;
byte c = -1;
char ch;

doNothing(format("0x%02hhX\n", c));  // Error: std.format formatArg
doNothing("here"~ch);  // compiler crash fmt.d:13: internal compiler error: in
rawArray, at d/d-codegen.cc:981
doNothing(format("// %d: %.*s\n", i, "there")); //Error: std.format int argument
expected

return 0;
}
-----------------------------


In article <ddb00l$9u0$1 digitaldaemon.com>, Chris Sauls says...
bug d.com wrote:
 I suppose format and printf accept the same format string.
Not exactly, actually.
 $ cat fmt.d
 -----------------------------
 import std.string;
 
 int main(char[][] args)
 {
 byte c = -1;
 char ch;
 printf(       "0x%02hhX\n", c );  // OK
 printf(format("0x%02hhX\n", c));  // runtime Error: std.format formatArg
 printf("here"~ch);                // compiler crash
 return 0;
 }
 -----------------------------
I'm not surprised you got errors... you are passing D-style strings and slices to printf, which expects C-style strings. For instance you could change this line: to this one: And change this line: to this one: Or, better yet, just import 'std.stdio' and use writef/writefln in place of printf. Then you have no problems, and can even scrap your call to format in this case. So your code becomes: -- Chris Sauls
Aug 09 2005
parent reply bug d.com writes:
Sorry, looks like format(...) do not accept format string e.g. "%d..." at all.
So the 1st and 3rd are not bug.  

Only the 2nd compiler crash is a real bug.


In article <ddb81r$ihe$1 digitaldaemon.com>, bug d.com says...
Maybe my choose of printf is misleading.  I only want to write a small example
to show the bug.  

For the 2nd bug: at any rate the compile can report errors, but should not
crash.

Let's see the new example:

$ cat fmt.d
-----------------------------
import std.string;

void doNothing(char[] s) {}

int main(char[][] args)
{
int i;
byte c = -1;
char ch;

doNothing(format("0x%02hhX\n", c));  // Error: std.format formatArg
doNothing("here"~ch);  // compiler crash fmt.d:13: internal compiler error: in
rawArray, at d/d-codegen.cc:981
doNothing(format("// %d: %.*s\n", i, "there")); //Error: std.format int argument
expected

return 0;
}
-----------------------------


In article <ddb00l$9u0$1 digitaldaemon.com>, Chris Sauls says...
bug d.com wrote:
 I suppose format and printf accept the same format string.
Not exactly, actually.
 $ cat fmt.d
 -----------------------------
 import std.string;
 
 int main(char[][] args)
 {
 byte c = -1;
 char ch;
 printf(       "0x%02hhX\n", c );  // OK
 printf(format("0x%02hhX\n", c));  // runtime Error: std.format formatArg
 printf("here"~ch);                // compiler crash
 return 0;
 }
 -----------------------------
I'm not surprised you got errors... you are passing D-style strings and slices to printf, which expects C-style strings. For instance you could change this line: to this one: And change this line: to this one: Or, better yet, just import 'std.stdio' and use writef/writefln in place of printf. Then you have no problems, and can even scrap your call to format in this case. So your code becomes: -- Chris Sauls
Aug 09 2005
parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
bug d.com wrote:
 Only the 2nd compiler crash is a real bug.
Both the snippets you posted compile fine with DMD 0.129 under Windows.
Aug 10 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
bug d.com wrote:

 I suppose format and printf accept the same format string.  If you comment out
 the line marked "here", there's a runtime error with format.
<snip> No, format and writef accept the same format string. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on on the 'group where everyone may benefit.
Aug 10 2005