Review a generated pointer-range search

from C pointers
C23 (GCC 13.3.0) advanced 6 min 4 issues to find

Review this generated function before it is used with external counts.

Search at most 16 integers for the first value in the inclusive [low, high] range. Reject a null output pointer, never access beyond count, and leave *out unchanged on every failure.

C
int find_between(const int values[16],
                 size_t count,
                 int low,
                 int high,
                 const int **out) {
    *out = NULL;
    if (low > high) {
        return 0;
    }

    for (size_t index = 0; index <= count; ++index) {
        if (values[index] > low && values[index] < high) {
            *out = &values[index];
            return 1;
        }
    }

    return 0;
}

generated code is illustrative, not from any one model

Open in playground
Report an error