Skip to content

Commit 9c24f9c

Browse files
authored
Check y for NULL too, remove duplicate zero'ing (#919)
* Check y for NULL too There are some rare cases where the x and y offsets are split across tables and the table with y gets incorrectly garbage collected in nk_clear(). This prevents that from causing a segfault. * Remove redundant nk_zero_struct() calls
2 parents a9fef84 + 7002929 commit 9c24f9c

5 files changed

Lines changed: 46 additions & 12 deletions

File tree

nuklear.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19896,7 +19896,6 @@ nk_create_table(struct nk_context *ctx)
1989619896
struct nk_page_element *elem;
1989719897
elem = nk_create_page_element(ctx);
1989819898
if (!elem) return 0;
19899-
nk_zero_struct(*elem);
1990019899
return &elem->data.tbl;
1990119900
}
1990219901
NK_LIB void
@@ -19986,7 +19985,6 @@ nk_create_panel(struct nk_context *ctx)
1998619985
struct nk_page_element *elem;
1998719986
elem = nk_create_page_element(ctx);
1998819987
if (!elem) return 0;
19989-
nk_zero_struct(*elem);
1999019988
return &elem->data.pan;
1999119989
}
1999219990
NK_LIB void
@@ -23340,7 +23338,13 @@ nk_group_begin_titled(struct nk_context *ctx, const char *id,
2334023338
NK_ASSERT(y_offset);
2334123339
if (!x_offset || !y_offset) return 0;
2334223340
*x_offset = *y_offset = 0;
23343-
} else y_offset = nk_find_value(win, id_hash+1);
23341+
} else if (!(y_offset = nk_find_value(win, id_hash+1))) {
23342+
y_offset = nk_add_value(ctx, win, id_hash+1, 0);
23343+
NK_ASSERT(y_offset);
23344+
if (!y_offset) return 0;
23345+
*x_offset = *y_offset = 0; /* I think this covers the degenerate case */
23346+
}
23347+
2334423348
return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
2334523349
}
2334623350
NK_API nk_bool
@@ -23382,7 +23386,12 @@ nk_group_get_scroll(struct nk_context *ctx, const char *id, nk_uint *x_offset, n
2338223386
NK_ASSERT(y_offset_ptr);
2338323387
if (!x_offset_ptr || !y_offset_ptr) return;
2338423388
*x_offset_ptr = *y_offset_ptr = 0;
23385-
} else y_offset_ptr = nk_find_value(win, id_hash+1);
23389+
} else if (!(y_offset_ptr = nk_find_value(win, id_hash+1))) {
23390+
y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
23391+
NK_ASSERT(y_offset_ptr);
23392+
if (!y_offset_ptr) return;
23393+
*x_offset_ptr = *y_offset_ptr = 0;
23394+
}
2338623395
if (x_offset)
2338723396
*x_offset = *x_offset_ptr;
2338823397
if (y_offset)
@@ -23417,7 +23426,11 @@ nk_group_set_scroll(struct nk_context *ctx, const char *id, nk_uint x_offset, nk
2341723426
NK_ASSERT(y_offset_ptr);
2341823427
if (!x_offset_ptr || !y_offset_ptr) return;
2341923428
*x_offset_ptr = *y_offset_ptr = 0;
23420-
} else y_offset_ptr = nk_find_value(win, id_hash+1);
23429+
} else if (!(y_offset_ptr = nk_find_value(win, id_hash+1))) {
23430+
NK_ASSERT(y_offset_ptr);
23431+
if (!y_offset_ptr) return;
23432+
*x_offset_ptr = *y_offset_ptr = 0;
23433+
}
2342123434
*x_offset_ptr = x_offset;
2342223435
*y_offset_ptr = y_offset;
2342323436
}
@@ -23467,7 +23480,11 @@ nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
2346723480
NK_ASSERT(y_offset);
2346823481
if (!x_offset || !y_offset) return 0;
2346923482
*x_offset = *y_offset = 0;
23470-
} else y_offset = nk_find_value(win, title_hash+1);
23483+
} else if (!(y_offset = nk_find_value(win, title_hash+1))) {
23484+
NK_ASSERT(y_offset);
23485+
if (!y_offset) return 0;
23486+
*x_offset = *y_offset = 0;
23487+
}
2347123488
view->scroll_value = *y_offset;
2347223489
view->scroll_pointer = y_offset;
2347323490

src/nuklear_group.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,13 @@ nk_group_begin_titled(struct nk_context *ctx, const char *id,
153153
NK_ASSERT(y_offset);
154154
if (!x_offset || !y_offset) return 0;
155155
*x_offset = *y_offset = 0;
156-
} else y_offset = nk_find_value(win, id_hash+1);
156+
} else if (!(y_offset = nk_find_value(win, id_hash+1))) {
157+
y_offset = nk_add_value(ctx, win, id_hash+1, 0);
158+
NK_ASSERT(y_offset);
159+
if (!y_offset) return 0;
160+
*x_offset = *y_offset = 0; /* I think this covers the degenerate case */
161+
}
162+
157163
return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
158164
}
159165
NK_API nk_bool
@@ -195,7 +201,12 @@ nk_group_get_scroll(struct nk_context *ctx, const char *id, nk_uint *x_offset, n
195201
NK_ASSERT(y_offset_ptr);
196202
if (!x_offset_ptr || !y_offset_ptr) return;
197203
*x_offset_ptr = *y_offset_ptr = 0;
198-
} else y_offset_ptr = nk_find_value(win, id_hash+1);
204+
} else if (!(y_offset_ptr = nk_find_value(win, id_hash+1))) {
205+
y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
206+
NK_ASSERT(y_offset_ptr);
207+
if (!y_offset_ptr) return;
208+
*x_offset_ptr = *y_offset_ptr = 0;
209+
}
199210
if (x_offset)
200211
*x_offset = *x_offset_ptr;
201212
if (y_offset)
@@ -230,7 +241,11 @@ nk_group_set_scroll(struct nk_context *ctx, const char *id, nk_uint x_offset, nk
230241
NK_ASSERT(y_offset_ptr);
231242
if (!x_offset_ptr || !y_offset_ptr) return;
232243
*x_offset_ptr = *y_offset_ptr = 0;
233-
} else y_offset_ptr = nk_find_value(win, id_hash+1);
244+
} else if (!(y_offset_ptr = nk_find_value(win, id_hash+1))) {
245+
NK_ASSERT(y_offset_ptr);
246+
if (!y_offset_ptr) return;
247+
*x_offset_ptr = *y_offset_ptr = 0;
248+
}
234249
*x_offset_ptr = x_offset;
235250
*y_offset_ptr = y_offset;
236251
}

src/nuklear_list_view.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
4343
NK_ASSERT(y_offset);
4444
if (!x_offset || !y_offset) return 0;
4545
*x_offset = *y_offset = 0;
46-
} else y_offset = nk_find_value(win, title_hash+1);
46+
} else if (!(y_offset = nk_find_value(win, title_hash+1))) {
47+
NK_ASSERT(y_offset);
48+
if (!y_offset) return 0;
49+
*x_offset = *y_offset = 0;
50+
}
4751
view->scroll_value = *y_offset;
4852
view->scroll_pointer = y_offset;
4953

src/nuklear_panel.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ nk_create_panel(struct nk_context *ctx)
1212
struct nk_page_element *elem;
1313
elem = nk_create_page_element(ctx);
1414
if (!elem) return 0;
15-
nk_zero_struct(*elem);
1615
return &elem->data.pan;
1716
}
1817
NK_LIB void

src/nuklear_table.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ nk_create_table(struct nk_context *ctx)
1212
struct nk_page_element *elem;
1313
elem = nk_create_page_element(ctx);
1414
if (!elem) return 0;
15-
nk_zero_struct(*elem);
1615
return &elem->data.tbl;
1716
}
1817
NK_LIB void

0 commit comments

Comments
 (0)