Versions Compared

Key

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

...

The following piece of code validates the number of command line arguemnts. If the correct number of commmand line arguments have been specified, memory is allocated with malloc() and referenced by str. Next, the second command line argument is copied into str for further processing. Once this processing is complete, str is freed.However, if the incorrect number of arguments have been specified, str is set to a string literal and printed. Because str now references memory that was not dynamically allocated, an error will occur when str memory is freed.

Code Block

#define MAX_SIZE_ALLOWED 1000

int main(int argc, char *argv[]) {
  char *str = NULL;
  size_t len;

  if (argc == 2) {
    len = strlen(argv[1])+1;
    if (len > MAX_SIZE_ALLOWED) {
      /* Handle Error */
    }
    str = malloc(len);
    if (str == NULL) {
      /* Handle Allocation Error */
    }
    strcpy(str,argv[1]);
  }
  else {
    str = "usage: $>a.exe [string]";
    printf("%s\n", str);
  }
  /* ... */
  free(str);
  return 0;
}


Compliant Solution 1

In the compliant solution, the program has been changed to eliminate the possibility of str referencing non-dynamic memory and when it is supplied to free().

Code Block
int main(int argc, char *argv[]) {
  char *str = NULL;
   size_t len;

  if (argc == 2) {
    strlen = malloc(strlen(argv[1])+1;
    if (len > MAX_SIZE_ALLOWED) {
      /* Handle Error */
    }
    str = malloc(len);
    if (str == NULL) {
      /* Handle Allocation Error */
    }
    strcpy(str, argv[1]);
  }
  else {
    printf("usage: $>a.exe [string]\n");
    return -1;
  }
  /* ... */
  free(str);
  return 0;
}

...