#include "includes.h"
-#define MAX_MALLOC_SIZE 0x7fffffff
-
-/*
- realloc an array, checking for integer overflow in the array size
-*/
-void *realloc_array(void *ptr, size_t el_size, unsigned count)
-{
- if (count == 0 ||
- count >= MAX_MALLOC_SIZE/el_size) {
- return NULL;
- }
- if (!ptr) {
- return malloc(el_size * count);
- }
- return realloc(ptr, el_size * count);
-}
-
-
/*
find an element in a list, using the given comparison function and
assuming that the list is already sorted using comp_fn
max_i = nmemb-1;
while (min_i < max_i) {
- size_t test_t;
int r;
test_i = (min_i + max_i) / 2;
- r = comp_fn(needle, *(void **)(base_p + (size * test_i)));
+ r = comp_fn(needle, *(void * const *)(base_p + (size * test_i)));
if (r == 0) {
/* scan back for first element */
- while (test_t > 0 &&
- comp_fn(needle, *(void **)(base_p + (size * (test_i-1)))) == 0) {
+ while (test_i > 0 &&
+ comp_fn(needle, *(void * const *)(base_p + (size * (test_i-1)))) == 0) {
test_i--;
}
return test_i;
}
- if (r == -1) {
+ if (r < 0) {
+ if (test_i == 0) {
+ return -1;
+ }
max_i = test_i - 1;
}
- if (r == 1) {
+ if (r > 0) {
min_i = test_i + 1;
}
}
- if (comp_fn(needle, *(void **)(base_p + (size * min_i))) == 0) {
+ if (comp_fn(needle, *(void * const *)(base_p + (size * min_i))) == 0) {
return min_i;
}