Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b247c6f

Browse filesBrowse files
committed
kbtree: pointer UB and unitialized value fixes
- don't underflow itr->p pointer (C standard only allows one past the end, not one before the beginning) - make sure itr->p->i is always initialized (even when not used) - don't rely on `NULL < &object` (likely UB)
1 parent 5625c7d commit b247c6f
Copy full SHA for b247c6f

File tree

Expand file treeCollapse file tree

1 file changed

+12
-5
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+12
-5
lines changed

‎src/nvim/lib/kbtree.h

Copy file name to clipboardExpand all lines: src/nvim/lib/kbtree.h
+12-5Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@
317317
#define __KB_ITR(name, key_t, kbnode_t) \
318318
static inline void kb_itr_first_##name(kbtree_##name##_t *b, kbitr_##name##_t *itr) \
319319
{ \
320-
itr->p = 0; \
320+
itr->p = NULL; \
321321
if (b->n_keys == 0) return; \
322322
itr->p = itr->stack; \
323323
itr->p->x = b->root; itr->p->i = 0; \
@@ -329,30 +329,36 @@
329329
} \
330330
static inline int kb_itr_next_##name(kbtree_##name##_t *b, kbitr_##name##_t *itr) \
331331
{ \
332-
if (itr->p < itr->stack) return 0; \
332+
if (itr->p == NULL) return 0; \
333333
for (;;) { \
334334
++itr->p->i; \
335335
while (itr->p->x && itr->p->i <= itr->p->x->n) { \
336336
itr->p[1].i = 0; \
337337
itr->p[1].x = itr->p->x->is_internal? __KB_PTR(b, itr->p->x)[itr->p->i] : 0; \
338338
++itr->p; \
339339
} \
340+
if (itr->p == itr->stack) { \
341+
itr->p = NULL; \
342+
return 0; \
343+
} \
340344
--itr->p; \
341-
if (itr->p < itr->stack) return 0; \
342345
if (itr->p->x && itr->p->i < itr->p->x->n) return 1; \
343346
} \
344347
} \
345348
static inline int kb_itr_prev_##name(kbtree_##name##_t *b, kbitr_##name##_t *itr) \
346349
{ \
347-
if (itr->p < itr->stack) return 0; \
350+
if (itr->p == NULL) return 0; \
348351
for (;;) { \
349352
while (itr->p->x && itr->p->i >= 0) { \
350353
itr->p[1].x = itr->p->x->is_internal? __KB_PTR(b, itr->p->x)[itr->p->i] : 0; \
351354
itr->p[1].i = itr->p[1].x ? itr->p[1].x->n : -1; \
352355
++itr->p; \
353356
} \
357+
if (itr->p == itr->stack) { \
358+
itr->p = NULL; \
359+
return 0; \
360+
} \
354361
--itr->p; \
355-
if (itr->p < itr->stack) return 0; \
356362
--itr->p->i; \
357363
if (itr->p->x && itr->p->i >= 0) return 1; \
358364
} \
@@ -374,6 +380,7 @@
374380
itr->p[1].x = itr->p->x->is_internal? __KB_PTR(b, itr->p->x)[i + 1] : 0; \
375381
++itr->p; \
376382
} \
383+
itr->p->i = 0; \
377384
return 0; \
378385
} \
379386
static inline int kb_itr_get_##name(kbtree_##name##_t *b, key_t k, kbitr_##name##_t *itr) \

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.