...
Code Block |
---|
#define BUF_SIZE 150 void runit(void) { char buf[50]; char *ptr; memset(buf,0,50); strcpy(buf,ptr); } void logit(char *str) { char buffer[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buffer[i] = '\0'; strcpy(buffer, str); printf("The message: %s\nhas been logged\n",buffer); } int main(int argc, char *argv[]) { char buf[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buf[i] = '\0'; strcpy(buf, argv[1]); logit(buf)); runit(); } |
Compliant Solution 2
...
Code Block |
---|
#define BUF_SIZE 150 void runit(void) { char buf[50]; char *ptr = 0NULL; /* initialize ptr to 0 */ memset(buf,0,50); strcpy(buf,ptr); } void logit(char *str) { char buffer[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buffer[i] = '\0'; strcpy(buffer, str); printf("The message: %s\nhas been logged\n",buffer); } int main(int argc, char *argv[]) { char buf[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buf[i] = '\0'; strcpy(buf, argv[1]); logit(buf); runit(); } |
...