[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
snprintf fails but claims success with large sizes when printing %#x
From: |
Eric Albert |
Subject: |
snprintf fails but claims success with large sizes when printing %#x |
Date: |
Sat, 5 Oct 2002 15:52:13 -0700 |
Using glibc 2.2.5-11.2 on Debian 3.0 and the 2.4.18 kernel, and
compiling with GCC 3.2, it appears that snprintf fails when printing
%#x when it is given a large value for 'n', but it returns a value that
claims that it has succeeded. The following program shows the problem.
On my computer, it prints this:
-----
Buffer is <0x123456>, result is 8
Buffer is <>, result is 8
-----
It should print out 0x123456 as the buffer for the second line of
output.
-----
#include <stdio.h>
int main() {
char buf[256];
int result;
result = snprintf(buf, 0x4000040c, "%#x", 0x123456);
printf("Buffer is <%s>, result is %d\n", buf, result);
result = snprintf(buf, 0x4000040d, "%#x", 0x123456);
printf("Buffer is <%s>, result is %d\n", buf, result);
return 0;
}
-----
Interestingly, the above code fails in both cases with the GCC 2.95.4
(prerelease) build that is distributed by default with Debian 3.0.
This version shows the boundary for 'n' to cause failure in GCC 2.95.4:
-----
#include <stdio.h>
int main() {
char buf[256];
int result;
result = snprintf(buf, 0x400003e4, "%#x", 0x123456);
printf("Buffer is <%s>, result is %d\n", buf, result);
result = snprintf(buf, 0x400003e5, "%#x", 0x123456);
printf("Buffer is <%s>, result is %d\n", buf, result);
return 0;
}
-----
Thanks,
Eric
--
Eric Albert address@hidden
http://rescomp.stanford.edu/~ejalbert/
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- snprintf fails but claims success with large sizes when printing %#x,
Eric Albert <=