Skip to content

a problem in the linklist.c #2

@bunnerlemon

Description

@bunnerlemon

I encountered a dangling pointer problem in the function r_utils_linklist_pop when I implemented the project.

void* r_utils_list_pop(r_utils_linklist_s *l) {
  r_utils_linklist_cell_s *c;
  void *elem;

  assert(l != NULL);

  if(l->head == NULL)
    return NULL;

  c = l->tail;
  elem = c->elem;
  l->tail = c->prev;

  if(l->tail == NULL)
    l->head = NULL;

  free(c);
  l->num--;

  return elem;
}

And I think I should set l->tail->next = NULL if l->tail != NULL when I pop an element.

if (l->tail == NULL) l->head = NULL;
else l->tail->next = NULL;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions