1

I still cannot understand why I got this warning array subscript is above array bounds [-Warray-bounds] for a small C code as the following:

#include <stdio.h>
#include <string.h>

static int _memcmp( const void *x, const void *y, size_t size ){
    const char *s1 = (char*)x, *s2 = (char*)y;

    int ret;
    ret = s1[0] - s2[0];
    if ( size == 1 || ret != 0 )
        return ret;

    ret = s1[1] - s2[1];
    if ( size == 2 || ret != 0 )
        return ret;

    ret = s1[2] - s2[2];
    if ( size == 3 || ret != 0 )
        return ret;

    ret = s1[3] - s2[3];
    if ( size == 4 || ret != 0 )
        return ret;

    ret = s1[4] - s2[4];
    if ( size == 5 || ret != 0 )
        return ret;

    ret = s1[5] - s2[5];
    if ( size == 6 || ret != 0 )
        return ret;

    ret = s1[6] - s2[6];
    if ( size == 7 || ret != 0 )
        return ret;

    ret = s1[7] - s2[7];
    if ( size == 8 || ret != 0 )
        return ret;

    ret = s1[8] - s2[8];
    if ( size == 9 || ret != 0 )
        return ret;

    ret = s1[9] - s2[9];
    if ( size == 10 || ret != 0 )
        return ret;

//0-20
    ret = s1[10] - s2[10];
    if ( size == 11 || ret != 0 )
        return ret;

    ret = s1[11] - s2[11];
    if ( size == 12 || ret != 0 )
        return ret;

    ret = s1[12] - s2[12];
    if ( size == 13 || ret != 0 )
        return ret;

    ret = s1[13] - s2[13];
    if ( size == 14 || ret != 0 )
        return ret;

    ret = s1[14] - s2[14];
    if ( size == 15 || ret != 0 )
        return ret;

    ret = s1[15] - s2[15];
    if ( size == 16 || ret != 0 )
        return ret;

    ret = s1[16] - s2[16];
    if ( size == 17 || ret != 0 )
        return ret;

    ret = s1[17] - s2[17];
    if ( size == 18 || ret != 0 )
        return ret;

    ret = s1[18] - s2[18];
    if ( size == 19 || ret != 0 )
        return ret;

    ret = s1[19] - s2[19];
    if ( size == 20 || ret != 0 )
        return ret;

    return memcmp( s1 + 20, s2 + 20, size - 20 );
}

void t1(){
    char *x = "hihihaha";
    printf("%d\n", _memcmp( x, "ha", 2 ));
}

void t2(){
    char *x = "hihihaha";
    printf("%d\n", _memcmp( x, "hi", 2 ));
}
int main(){
    return 0;
}

When I compiled with 03 flag, I got this message:

gcc-7 -O3 -Wall -o mem_cmp mem_cmp.c
mem_cmp.c: In function ‘_memcmp.part.0.constprop’:
mem_cmp.c:89:23: warning: array subscript is above array bounds [-Warray-bounds]
     return memcmp( s1 + 20, s2 + 20, size - 20 );

I've tried with gcc-4.9 and gcc-5 but no luck.

Edit:

I'm curious to know about why the warning appears rather than about the style of code. (I know well memcmp. Certainly _memcmp can be reimplemented by for but I want to gain even some cycles.)

2
  • What will be s1+20 in case of s1="hihihaha"??? Also never use leading underscore in your names. Especially with the standard-like names such as memcmp. Commented Feb 20, 2018 at 17:24
  • 1
    From my experience, -Warray-bounds often produces false positives. Commented Feb 20, 2018 at 17:30

1 Answer 1

1

Your memcmp assumes that the memory s1+20 is within an array s1 - which it is not. Even if the compiler didn't complain about this - you would get into undefined behavior (which may range from crashing of your program to correct execution. This is not what you should reply on.). Mostly from the string literals size which is known at compile tile it infers about this.

Correct one would be

return memcmp( s1, s2, min(strlen(s1),strlen(s2)));

where min(x,y) returns minimum of x and y.

First check the size and then access the array. First do the size checking then index into the array. repetitive code over here can be reduced using for loop. By that I mean whatever you are doing for different indices in those if statements can be packed into a single block, so that it can be used with for loop (with index variables denoting the changed value in each iteration).

Also note if you are using memcmp at last then don't go for checking characters individually - it is not needed. You can just check it directly using memcmp and then output the result.

Sign up to request clarification or add additional context in comments.

1 Comment

@IljaEverilä.: Nice point - and it would be undefined behavior not necessarily runtime error (I should be more specific correctly).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.