I have a small char array (let's say 10 bytes) that I want to pass into sprintf(). What happens if sprintf() pushes too much onto that array? Here's an example of how to reproduce this:
1 | #include <iostream> |
2 | |
3 | int main() |
4 | { |
5 | int a = 1234; |
6 | char buffer[10] = {0}; |
7 | |
8 | // This should be "10" |
9 | std::cerr << "The size of buffer is: " << sizeof(buffer); |
10 | |
11 | // This should be "0" |
12 | std::cerr << ", and the string length of buffer: " << strlen(buffer); |
13 | std::cerr << std::endl; |
14 | |
15 | // stuff buffer[] with more than 10 chars |
16 | sprintf(buffer, "I will stuff 'buffer' with lots of text and numbers (like %d)!\n", a); |
17 | |
18 | // This should still be "10" |
19 | std::cerr << "Now the size of buffer is: " << sizeof(buffer); |
20 | |
21 | // This results in "65" |
22 | std::cerr << ", and the string length of buffer is: " << strlen(buffer); |
23 | std::cerr << std::endl; |
24 | |
25 | return 0; |
26 | } |
This will crash with a SEGFAULT (so if you run it, do it through a debugger). Apparently, sprintf() is unsafe and "keeps going until it's done" but doesn't do any bounds checking.
Is there a "safe" sprintf() function, like sprintnf() that I can use to ensure that I don't exceed a specified size?
Apparently, sprintf() is unsafe and "keeps going until it's done" but doesn't do any bounds checking.
Of course, because you don't tell it how big its bounds are (it's not psychic).
Is there a "safe" sprintf() function, like sprintnf() that I can use to ensure that I don't exceed a specified size?
snprintf
Or using that other OS that doesn't like to play by everyone else's standards: _snprintf or sprintf_s.
Perfect. Thanks, KC!
I personally like using the return value of snprintf, and passing NULL to the string arg so it just calculates the total length, and then allocating a properly sized buffer to do it again.
Another option is to use GCC's asprintf. It allocates the buffer for you.
Uhh... you're using iostream and sprintf? Why???
Use #include <strstream>
I personally like using the return value of snprintf, and passing NULL to the string arg so it just calculates the total length, and then allocating a properly sized buffer to do it again.
O_o
pretty much, though I'd do it like so:
Just to be obvious about the NULL bit.
Stringstream = better.
Is there a sprintf()-style function available for std::string? If so, you wouldn't need to worry about memory allocation/deallocation or string length.
Does std::stringstream allow you to do this?
Stringstream = better.
Though you get problems trying to use it in C.
OnlineCop: yes.