...
Code Block | ||
---|---|---|
| ||
void complain(char const *msg) { errno_t err; static char const prefix[] = "Error: "; static char const suffix[] = "\n"; char buf[BUFSIZ]; if ((err = strcpy_s(buf, sizeof(buf), prefix)); if (err != 0) { /* handle error */ } if ((err = strcat_s(buf, sizeof(buf), msg)); if (err != 0) { /* handle error */ } if ((err = strcat_s(buf, sizeof(buf), suffix)); if (err != 0) { /* handle error */ } fputs(buf, stderr); } |
Compliant Solution (partial compile time)
...
Code Block | ||
---|---|---|
| ||
void complain(char const *msg) { errno_t err; static char const prefix[] = "Error: "; static char const suffix[] = "\n"; char buf[BUFSIZ]; /* Ensure that more than one character * is available for msg. */ static_assert(sizeof(buf) > sizeof(prefix) + sizeof(suffix), "Buffer for complain() is too small"); strcpy(buf, prefix); if ((err = strcat_s(buf, sizeof(buf), msg)); if (err != 0) { /* handle error */ } if ((err = strcat_s(buf, sizeof(buf), suffix)); if (err != 0) { /* handle error */ } fputs(buf, stderr); } |
...