tree-wide: add bsearch_safe and use where appropriate
Should fix #8557.
This commit is contained in:
+4
-8
@@ -24,6 +24,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "strbuf.h"
|
||||
#include "util.h"
|
||||
|
||||
/*
|
||||
* Strbuf stores given strings in a single continuous allocated memory
|
||||
@@ -144,7 +145,6 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
|
||||
str->in_len += len;
|
||||
|
||||
node = str->root;
|
||||
c = s[len-1];
|
||||
for (depth = 0; depth <= len; depth++) {
|
||||
struct strbuf_child_entry search;
|
||||
|
||||
@@ -158,15 +158,11 @@ ssize_t strbuf_add_string(struct strbuf *str, const char *s, size_t len) {
|
||||
|
||||
c = s[len - 1 - depth];
|
||||
|
||||
/* bsearch is not allowed on a NULL sequence */
|
||||
if (node->children_count == 0)
|
||||
break;
|
||||
|
||||
/* lookup child node */
|
||||
search.c = c;
|
||||
child = bsearch(&search, node->children, node->children_count,
|
||||
sizeof(struct strbuf_child_entry),
|
||||
(__compar_fn_t) strbuf_children_cmp);
|
||||
child = bsearch_safe(&search, node->children, node->children_count,
|
||||
sizeof(struct strbuf_child_entry),
|
||||
(__compar_fn_t) strbuf_children_cmp);
|
||||
if (!child)
|
||||
break;
|
||||
node = child->child;
|
||||
|
||||
Reference in New Issue
Block a user