...
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, int size) { char buffer[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buffer[i] = '\0'; strncpystrcpy(buffer, str, size); 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'; strncpystrcpy(buf, argv[1], BUF_SIZE-1); logit(buf,strlen(buf)); runit(); } |
Compliant Solution 2
...
Code Block |
---|
#define BUF_SIZE 150 void runit(void) { char buf[50]; char *ptr = 0; /* initialize ptr to 0 */ memset(buf,0,50); strcpy(buf,ptr); } void logit(char *str, int size) { char buffer[BUF_SIZE]; int i; for (i=0; i < BUF_SIZE; ++i) buffer[i] = '\0'; strncpystrcpy(buffer, str, size); 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'; strncpystrcpy(buf, argv[1], BUF_SIZE-1); logit(buf,strlen(buf)); runit(); } |
References
- ISO/IEC 9899-1999 Section 6.7.8 Initialization