...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> /* Get username and password from user, return -1 on error */ extern int do_auth(void); enum { BUFFERSIZE = 24 }; void report_error(const char *msg) { const char *error_log; char buffer[BUFFERSIZE]; sprintf(buffer, "Error: %s\n", error_log); puts(printf("%s\n", buffer); } int main(void) { if (do_auth() == -1) { report_error("Unable to login"); } return 0; } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h> enum { BUFFERSIZE = 24 }; void report_error(const char *msg) { const char *error_log = msg; char buffer[BUFFERSIZE]; sprintf(buffer, "Error: %s\n", error_log); puts(printf("%s\n", buffer); } |
This example remains problematic because a buffer overflow will occur if the null-terminated byte string referenced by msg
is greater than 17 characters, including the null terminator. (See STR31-C. Guarantee that storage for strings has sufficient space for character data and the null terminator for more information.)
...