On Thursday 11 January 2007 03:00, Felix von Leitner wrote:
$ cat t.c
#include stdio.h
int main() {
printf(%d\n,snprintf(0,0,%*d %*d,0x4000,1,0x4000,1));
}
$ gcc -o t t.c
$ ./t
-2147483647
./t 17.02s user 0.03s system 99% cpu 17.161 total
$
the second
Hello Felix.
Am Donnerstag, 11. Januar 2007 02:00 schrieb Felix von Leitner:
This is about two issues. First: abs within vasprintf.
I just read some gnupg source code and stumbled upon their
vasprintf implementation.
Did you told them about it?
Second issue: int overflow in *printf:
Thus spake Pierre Habouzit ([EMAIL PROTECTED]):
But that got me thinking. *printf return an int, and it's supposed to
be the number of chars written. So a typical idiom is
size_t memory_needed=snprintf(NULL,0,format_string,...);
char* ptr=malloc(memory_needed+1);
Thus spake Thomas ([EMAIL PROTECTED]):
I just read some gnupg source code and stumbled upon their
vasprintf implementation.
Did you told them about it?
I'm, uh, still working on that. :-)
But that got me thinking. *printf return an int, and it's supposed to
be the number of chars
But that got me thinking. *printf return an int, and it's supposed to
be the number of chars written. So a typical idiom is
size_t memory_needed=snprintf(NULL,0,format_string,...);
char* ptr=malloc(memory_needed+1);
sprintf(ptr,format_string,...);
This is nothing new.
This is about two issues. First: abs within vasprintf.
I just read some gnupg source code and stumbled upon their
vasprintf implementation. Basically they make one pass over the format
string to find out how much memory to malloc, and then they call sprintf
on the malloced buffer.
Here is an