#include #include #include #include using namespace std; /* This program makes a mistake, and does not allocate enough memory for the sprintf() call. In particular, buf2 is only eight bytes, and the sprintf() call writes at least 10 bytes, and maybe more. Fortunately, snprintf makes sure that we do not corrupt memory that's not part of the 8 bytes of buf2 */ int main() { char buf1[8]; char buf2[8]; char buf3[8]; int i; buf1[0] = '\0'; buf2[0] = '\0'; buf3[0] = '\0'; cin >> i; printf("Before:\n"); printf("buf1: %s\n", buf1); printf("buf2: %s\n", buf2); printf("buf3: %s\n", buf3); /* Here is where snprintf() overruns the bytes allocated for buf2. */ snprintf(buf2, 8, "%d %d %d %d %d", i, i+1, i+2, i+3, i+4); printf("After:\n"); printf("buf1: %s\n", buf1); printf("buf2: %s\n", buf2); printf("buf3: %s\n", buf3); return 0; }