tree-wide: add bsearch_safe and use where appropriate

Should fix #8557.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-03-26 15:28:03 +02:00
parent 989290dbf1
commit d6c5d19b66
4 changed files with 21 additions and 10 deletions
+4 -8
View File
@@ -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;