Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: tightened up formatting; added error check

...

Code Block
bgColor#ccccff
/* memset_s.c */
errno_t memset_s(void *v, rsize_t smax, int c, rsize_t n) {
  if (v == NULL) {
    return EINVAL;
  }
  if (smax > RSIZE_MAX) {
    return EINVAL;
  }
  if (n > smax) {
    return EINVAL;
  }

  volatile unsigned char *p = v;
  while (smax-- && n--) {
    *p++ = c;
  }

  return 0;
}

/* getPassword.c */
extern errno_t memset_s(void *v, rsize_t smax, int c, rsize_t n);

void getPassword(void) {
  char pwd[64];

  if (retrievePassword(pwd, sizeof(pwd))) {
     /*checking of password, secure operations, etc */
  }
  if (memset_s(pwd, sizeof(pwd), 0, sizeof(pwd)); != 0) {
    /* Handle error */
  }
}

This is the preferred solution for C99 because C1X introduces a memset_s function with this signature. See the C1X compliant solution for more information.

...