Thread overview
2 bugs: format / printf
Aug 09, 2005
bug
Aug 09, 2005
Chris Sauls
3 bugs Re: 2 bugs: format
Aug 09, 2005
bug
Aug 09, 2005
bug
Aug 10, 2005
Deewiant
Aug 10, 2005
Stewart Gordon
August 09, 2005
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.


August 09, 2005
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:
# printf(format("0x%02hhX\n", c));

to this one:
# printf(toStringz(format("0x%02hhX\n", c)));

And change this line:
# printf("here"~ch);

to this one:
# printf("%.*s", "here"~ch); //.*s is compatable with D-style strings/slices

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:

# import std.stdio ;
#
# int main (in char[][] args) {
#   byte c  = -1 ;
#   char ch      ;
#
#   writefln("0x%02hhX", c);
#   writefln("here" ~ ch);
#   return 0;
# }

-- Chris Sauls
August 09, 2005
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:
># printf(format("0x%02hhX\n", c));
>
>to this one:
># printf(toStringz(format("0x%02hhX\n", c)));
>
>And change this line:
># printf("here"~ch);
>
>to this one:
># printf("%.*s", "here"~ch); //.*s is compatable with D-style strings/slices
>
>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:
>
># import std.stdio ;
>#
># int main (in char[][] args) {
>#   byte c  = -1 ;
>#   char ch      ;
>#
>#   writefln("0x%02hhX", c);
>#   writefln("here" ~ ch);
>#   return 0;
># }
>
>-- Chris Sauls


August 09, 2005
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:
>># printf(format("0x%02hhX\n", c));
>>
>>to this one:
>># printf(toStringz(format("0x%02hhX\n", c)));
>>
>>And change this line:
>># printf("here"~ch);
>>
>>to this one:
>># printf("%.*s", "here"~ch); //.*s is compatable with D-style strings/slices
>>
>>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:
>>
>># import std.stdio ;
>>#
>># int main (in char[][] args) {
>>#   byte c  = -1 ;
>>#   char ch      ;
>>#
>>#   writefln("0x%02hhX", c);
>>#   writefln("here" ~ ch);
>>#   return 0;
>># }
>>
>>-- Chris Sauls
>
>


August 10, 2005
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.
August 10, 2005
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.