Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive in a loop using '!=' instead of '<' #102

Open
gh2375 opened this issue Feb 10, 2019 · 2 comments
Open

False positive in a loop using '!=' instead of '<' #102

gh2375 opened this issue Feb 10, 2019 · 2 comments
Labels
C-false-positive Category: False Positive L-c++ Language: C++ L-c Language: C P-medium Priority: Medium

Comments

@gh2375
Copy link

gh2375 commented Feb 10, 2019

This is for tracking the remaining warning from #27 (comment).

Code:

struct MyStruct
{
    int arr[10] = {};
};

int main(int argc, char * argv[])
{
    MyStruct s;

    return 0;
}

Output:

# Results
test-array.cpp: In function 'MyStruct::MyStruct()':
test-array.cpp:4:16: warning: possible buffer overflow, pointer '{0: &this->arr[0], 1: &(...)[1]}' points to local variable 's' of size 40 bytes
        int arr[10] = {};
                      ^

System:

MSYS2

Version:

79942e6

@arthaud
Copy link
Member

arthaud commented Feb 11, 2019

This is a false positive in the constructor of MyStruct.

Here is the equivalent C code:

int main(int argc, char *argv[]) {
  int array[10];
  int* p = &array[0];
  int* q = &array[10];
  for (; p != q; ++p) {
    *p = 0;
  }
  return 0;
}
test.c: In function 'main':
test.c:6:8: warning: possible buffer overflow, could not bound index for access of local variable 'array' of 10 elements
    *p = 0;
       ^

This is a classic problem in abstract interpretation. The widening is aggressive and it infers that 0 <= offset(p) <= +oo, and the narrowing cannot infer the proper bound.

FIY, this code can be proven by rewriting p != q to p < q and using ikos -d=gauge-interval-congruence.

@arthaud arthaud added C-false-positive Category: False Positive L-c++ Language: C++ L-c Language: C labels Feb 11, 2019
@arthaud arthaud changed the title warning: possible buffer overflow, pointer points to local variable False position in a loop using '!=' instead of '<' Jul 9, 2019
@arthaud arthaud added the P-medium Priority: Medium label Jul 10, 2019
@ivanperez-keera ivanperez-keera changed the title False position in a loop using '!=' instead of '<' False positive in a loop using '!=' instead of '<' Dec 1, 2023
@ivanperez-keera
Copy link
Collaborator

Is there anything that can be done about this in the near term? If not, maybe we can close this, mark it as a question, or move it to discussions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-false-positive Category: False Positive L-c++ Language: C++ L-c Language: C P-medium Priority: Medium
Projects
None yet
Development

No branches or pull requests

3 participants