Versions Compared

Key

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

...

Code Block
/*
 *  list points to the pre-allocated base node in the list
 */
void build_list(const struct int_list *list, size_t size) {
  size_t i;
  struct int_list *c_ptr = NULL;
  list->payload = 42;
  c_ptr = list;

  for (i=0; i < size; i++) {
    struct int_list *temp=malloc(sizeof(struct int_list));
    temp->payload = c_ptr->payload+1;
    c_ptr->next = temp;
    c_ptr = c_ptr->next;
    free(temp);         /* error */
  }
    c_ptr->next = NULL;
}

Compliant Solution 1

...

Code Block
struct int_list {
  struct int_list *next;
  int payload;
};

 build_list(const struct int_list *list) { 
  int i;
  struct int_list *list = malloc(sizeof(struct int_list));
  
  struct int_list *c_ptr = NULL;
  struct int_list *temp = NULL;
  list->payload = 42;
  c_ptr = list;

  for (i=0; i < 10; i++) {
    temp = malloc(sizeof(struct int_list));
  
    temp->payload = c_ptr->payload+1;
    c_ptr->next = temp;
    c_ptr = c_ptr->next;
  }
  temp = NULL;
  c_ptr->next = NULL;

  print_list(list);
}

References

VU#390044, http://www.kb.cert.org/vuls/id/390044