fix(ll): prevent use after free when removing items from any type of ll
This commit is contained in:
parent
6d04487bc7
commit
5e4ae41113
1 changed files with 32 additions and 42 deletions
50
ds.c
50
ds.c
|
|
@ -92,21 +92,13 @@ void
|
|||
return NULL;
|
||||
}
|
||||
|
||||
for (i = -1, cur = *ll; cur; i++, cur = cur->next) {
|
||||
if (i + 1 == idx) {
|
||||
if (idx == 0) {
|
||||
rm = cur;
|
||||
if (!rm) {
|
||||
return NULL;
|
||||
}
|
||||
*ll = cur->next;
|
||||
rm = *ll;
|
||||
*ll = rm->next;
|
||||
} else {
|
||||
for (i = -1, cur = *ll; cur; i++, cur = cur->next) {
|
||||
if (idx == i + 1) {
|
||||
rm = cur->next;
|
||||
if (!rm) {
|
||||
return NULL;
|
||||
}
|
||||
cur->next = cur->next->next;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -115,6 +107,9 @@ void
|
|||
return NULL;
|
||||
}
|
||||
|
||||
cur->next = rm->next;
|
||||
}
|
||||
|
||||
data = rm->data;
|
||||
free(rm);
|
||||
return data;
|
||||
|
|
@ -132,27 +127,16 @@ void
|
|||
return NULL;
|
||||
}
|
||||
|
||||
for (i = -1, cur = *ll; cur; i++, cur = cur->next) {
|
||||
if (i + 1 == idx) {
|
||||
if (idx == 0) {
|
||||
rm = cur;
|
||||
if (!rm) {
|
||||
return NULL;
|
||||
}
|
||||
if (cur->next) {
|
||||
cur->next->prev = NULL;
|
||||
*ll = cur->next;
|
||||
rm = *ll;
|
||||
*ll = rm->next;
|
||||
if (rm->next) {
|
||||
rm->next->prev = NULL;
|
||||
}
|
||||
} else {
|
||||
rm = cur->next;
|
||||
if (!rm) {
|
||||
return NULL;
|
||||
}
|
||||
cur->next = cur->next->next;
|
||||
if (cur->next) {
|
||||
cur->next->prev = cur;
|
||||
}
|
||||
}
|
||||
for (i = 0, cur = *ll; cur; i++, cur = cur->next) {
|
||||
if (i == idx) {
|
||||
rm = cur;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -161,6 +145,12 @@ void
|
|||
return NULL;
|
||||
}
|
||||
|
||||
rm->prev->next = rm->next;
|
||||
if (rm->next) {
|
||||
rm->next->prev = rm->prev;
|
||||
}
|
||||
}
|
||||
|
||||
data = rm->data;
|
||||
free(rm);
|
||||
return data;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue