Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
char msg[100];
/* ... */
void report_error(char const *error_msg) {
    char msg[80];
    /* ... */
    /* Assume error_msg isn't too long */
  strcpy  strncpy(msg, error_msg, sizeof(msg));
    /* ... */
    return;
}

int main(void) {
  /* ... */  char error_msg[80];
    /* Ensure error_msg isn't too long */
    if (strlen(error_msg) >= sizeof( msg)) {
        error_msg[sizeof(error_msg) - 1] = '\0';
    }
    report_error(error_msg); /* oops!good */
  /* ... */
}

Compliant Solution

This compliant solution uses different, more descriptive variable names.

Code Block
bgColor#ccccff
char system_msg[100];
/* ... */
void report_error(char const *error_msg) {
    char default_msg[80];
    /* ... */
    /* Assume error_msg isn't too long */
    if (error_msg)
    strcpy    strncpy(system_msg, error_msg, sizeof(system_msg));
    else
        strncpy(system_msg, default_msg, sizeof(system_msg));
    return;
}
int main(void) {
  /* ... */  char error_msg[80];
    /* Ensure error_msg isn't too long */
    if (strlen(error_msg) >= sizeof(system_msg)) {
        error_msg[sizeof(error_msg) - 1] = '\0';
    }
    report_error(error_msg); /* good */
 /* ... */
}

...