Skip to content

Commit

Permalink
utlist: Fix a segfault in DL_INSERT_INORDER2.
Browse files Browse the repository at this point in the history
This was a cut-and-paste error in 47145f5.

Thanks to @MattiasErikssonAxentia for the patch and test-case!
  • Loading branch information
Quuxplusone committed May 10, 2018
1 parent 9556e3b commit 8488951
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/utlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ do {
(head) = (add); \
} else { \
char *_tmp = (char*)(head); \
while ((char*)(head)->next != _tmp && (cmp((head)->next, add)) < 0) { \
while ((head)->next && (cmp((head)->next, add)) < 0) { \
(head) = (head)->next; \
} \
(add)->prev = (head); \
Expand Down
11 changes: 9 additions & 2 deletions tests/test94.ans
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
n g m f l e k d j c i b o h a
LL_INSERT_INORDER
list1: n g m f l e k d j c i b o h a
list2: o h a i b j c k d l e m f n g
DL_INSERT_INORDER
n g m f l e k d j c i b o h a
list1: n g m f l e k d j c i b o h a
list2: o h a i b j c k d l e m f n g
CDL_INSERT_INORDER
list1:
n g m f l e k d j c i b o h a
n a h o b i c j d k e l f m g
list2:
o h a i b j c k d l e m f n g
o g n f m e l d k c j b i a h
55 changes: 47 additions & 8 deletions tests/test94.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,91 @@

typedef struct el {
int id, score;
struct el *next2, *prev2;
struct el *next, *prev;
struct el *next_list2, *prev_list2;
} el;

static int order_desc(el *a, el *b)
{
return (a->score > b->score) ? -1 : (a->score < b->score);
}

static int order_asc(el *a, el *b)
{
return -order_desc(a, b);
}

int main(int argc, char *argv[])
{
int i;
el *head = NULL;
el *head2 = NULL;
el els[15], *e;

for (i=0; i<15; i++) {
els[i].id = (int)'a'+i;
els[i].score = i%7;
LL_INSERT_INORDER2(head, &els[i], order_desc, next2);
LL_INSERT_INORDER(head, &els[i], order_desc);
LL_INSERT_INORDER2(head2, &els[i], order_asc, next_list2);
}

printf("LL_INSERT_INORDER\n");
printf("list1: ");
LL_FOREACH(head, e) {
printf("%c ", e->id);
}
LL_FOREACH2(head, e, next2) {
printf("\n");
printf("list2: ");
LL_FOREACH2(head2, e, next_list2) {
printf("%c ", e->id);
}
printf("\n");

printf("DL_INSERT_INORDER\n");
head = NULL;
head2 = NULL;
for (i=0; i<15; i++) {
DL_INSERT_INORDER2(head, &els[i], order_desc, prev2, next2);
DL_INSERT_INORDER(head, &els[i], order_desc);
DL_INSERT_INORDER2(head2, &els[i], order_asc, prev_list2, next_list2);
}
DL_FOREACH2(head, e, next2) {

printf("list1: ");
DL_FOREACH(head, e) {
printf("%c ", e->id);
}
printf("\n");
printf("list2: ");
DL_FOREACH2(head2, e, next_list2) {
printf("%c ", e->id);
}
printf("\n");

printf("CDL_INSERT_INORDER\n");
head = NULL;
head2 = NULL;
for (i=0; i<15; i++) {
CDL_INSERT_INORDER2(head, &els[i], order_desc, prev2, next2);
CDL_INSERT_INORDER(head, &els[i], order_desc);
CDL_INSERT_INORDER2(head2, &els[i], order_asc, prev_list2, next_list2);
}
CDL_FOREACH2(head, e, next2) {
printf("list1:\n");
CDL_FOREACH(head, e) {
printf("%c ", e->id);
}
printf("\n");
CDL_FOREACH2(head, e, prev2) {
CDL_FOREACH2(head, e, prev) {
printf("%c ", e->id);
}
printf("\n");

printf("list2:\n");
CDL_FOREACH2(head2, e, next_list2) {
printf("%c ", e->id);
}
printf("\n");
CDL_FOREACH2(head2, e, prev_list2) {
printf("%c ", e->id);
}
printf("\n");

return 0;
}

0 comments on commit 8488951

Please sign in to comment.