www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - printf bug ??!

reply "lanael" <no mail.never> writes:
void main() {
ulong u =0b100000000000000000000000000000000000000000000000; // 1 and 
47 zeros
ulong u1=0b1000000000000000000000000000000000000000000000000; // 1 and 
48 zeros
printf("u  = \t %llx \t %llb\n",u,u);
printf("u1 = \t %llx \t %llb\n",u1,u1);
}

output :
u  =     800000000000    
100000000000000000000000000000000000000000000000
u1 =     1000000000000    
000000000000000000000000000000000000000000000000

it looks like a buffer overflow...
Mar 16 2006
next sibling parent Thomas Kuehne <thomas-dloop kuehne.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

lanael schrieb am 2006-03-16:
 void main() {
 ulong u =0b100000000000000000000000000000000000000000000000; // 1 and 
 47 zeros
 ulong u1=0b1000000000000000000000000000000000000000000000000; // 1 and 
 48 zeros
 printf("u  = \t %llx \t %llb\n",u,u);
 printf("u1 = \t %llx \t %llb\n",u1,u1);
 }

 output :
 u  =     800000000000    
 100000000000000000000000000000000000000000000000
 u1 =     1000000000000    
 000000000000000000000000000000000000000000000000

 it looks like a buffer overflow...
The %llb notation isn't part of the standard printf implementation. (see C99, ISO 9899 7.19.6.1.8) I'm not saying that this is a bug or not, but the code isn't portable:
 #include <stdio.h>

 int main(){
	unsigned long long i = 9;
	printf("%llb\n", i);
	return 0;
 }
prints "%b" under Linux Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFEGcuj3w+/yD4P9tIRAqu7AKCpEp/rD8aVQO8quVn6KH5rp7zxvACdGelJ SWSf/QenPQsqubs6ftznCOg= =70Te -----END PGP SIGNATURE-----
Mar 16 2006
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"lanael" <no mail.never> wrote in message 
news:mn.84927d63e30b23b4.35838 mail.never...
 printf("u  = \t %llx \t %llb\n",u,u);
 printf("u1 = \t %llx \t %llb\n",u1,u1);
Don't. Use. printf. writefln("u = \t %x \t %b", u, u); writefln("u1 = \t %x \t %b", u1, u1);
Mar 16 2006
parent reply lanael <lanael_member pathlink.com> writes:
In article <dvd11t$2a4h$1 digitaldaemon.com>, Jarrett Billingsley says...

Don't.
Use.
printf.
Ok! ok! ok! : I'll add this to my mantra list ! ;) Anyway, I was thinking that writef() was a just a "bridge" to printf, but I now see that I was totally wrong. writefx() looks like a complete rewrite of printf type functions in pure D...
Mar 17 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"lanael" <lanael_member pathlink.com> wrote in message 
news:dvdssk$jed$1 digitaldaemon.com...
 Ok! ok! ok! : I'll add this to my mantra list ! ;)
Hehe :)
 Anyway, I was thinking that writef() was a just a "bridge" to printf, but 
 I now
 see that I was totally wrong. writefx() looks like a complete rewrite of 
 printf
 type functions in pure D...
Absolutely. printf is a C function, and as such, has no concept of type safety. The std.format based functions (like the writef family) are written in D, and use D's variable argument system, making them much more type-safe as well as being D-aware.
Mar 17 2006