Skip to content

Commit a2d7087

Browse files
committed
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.
1 parent a9fef84 commit a2d7087

3 files changed

Lines changed: 46 additions & 8 deletions

File tree

nuklear.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23340,7 +23340,13 @@ nk_group_begin_titled(struct nk_context *ctx, const char *id,
2334023340
NK_ASSERT(y_offset);
2334123341
if (!x_offset || !y_offset) return 0;
2334223342
*x_offset = *y_offset = 0;
23343-
} else y_offset = nk_find_value(win, id_hash+1);
23343+
} else if (!(y_offset = nk_find_value(win, id_hash+1))) {
23344+
y_offset = nk_add_value(ctx, win, id_hash+1, 0);
23345+
NK_ASSERT(y_offset);
23346+
if (!y_offset) return 0;
23347+
*x_offset = *y_offset = 0; /* I think this covers the degenerate case */
23348+
}
23349+
2334423350
return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
2334523351
}
2334623352
NK_API nk_bool
@@ -23382,7 +23388,12 @@ nk_group_get_scroll(struct nk_context *ctx, const char *id, nk_uint *x_offset, n
2338223388
NK_ASSERT(y_offset_ptr);
2338323389
if (!x_offset_ptr || !y_offset_ptr) return;
2338423390
*x_offset_ptr = *y_offset_ptr = 0;
23385-
} else y_offset_ptr = nk_find_value(win, id_hash+1);
23391+
} else if (!(y_offset_ptr = nk_find_value(win, id_hash+1))) {
23392+
y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
23393+
NK_ASSERT(y_offset_ptr);
23394+
if (!y_offset_ptr) return;
23395+
*x_offset_ptr = *y_offset_ptr = 0;
23396+
}
2338623397
if (x_offset)
2338723398
*x_offset = *x_offset_ptr;
2338823399
if (y_offset)
@@ -23417,7 +23428,11 @@ nk_group_set_scroll(struct nk_context *ctx, const char *id, nk_uint x_offset, nk
2341723428
NK_ASSERT(y_offset_ptr);
2341823429
if (!x_offset_ptr || !y_offset_ptr) return;
2341923430
*x_offset_ptr = *y_offset_ptr = 0;
23420-
} else y_offset_ptr = nk_find_value(win, id_hash+1);
23431+
} else if (!(y_offset_ptr = nk_find_value(win, id_hash+1))) {
23432+
NK_ASSERT(y_offset_ptr);
23433+
if (!y_offset_ptr) return;
23434+
*x_offset_ptr = *y_offset_ptr = 0;
23435+
}
2342123436
*x_offset_ptr = x_offset;
2342223437
*y_offset_ptr = y_offset;
2342323438
}
@@ -23467,7 +23482,11 @@ nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
2346723482
NK_ASSERT(y_offset);
2346823483
if (!x_offset || !y_offset) return 0;
2346923484
*x_offset = *y_offset = 0;
23470-
} else y_offset = nk_find_value(win, title_hash+1);
23485+
} else if (!(y_offset = nk_find_value(win, title_hash+1))) {
23486+
NK_ASSERT(y_offset);
23487+
if (!y_offset) return 0;
23488+
*x_offset = *y_offset = 0;
23489+
}
2347123490
view->scroll_value = *y_offset;
2347223491
view->scroll_pointer = y_offset;
2347323492

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

0 commit comments

Comments
 (0)