Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

clang/C++: issue warning when iterators from different variables are compared #5574

Open Quuxplusone opened 15 years ago

Quuxplusone commented 15 years ago
Bugzilla Link PR5067
Status NEW
Importance P enhancement
Reported by Török Edwin (edwin+bugs@etorok.eu)
Reported on 2009-09-27 02:52:22 -0700
Last modified on 2013-04-14 11:48:39 -0700
Version trunk
Hardware PC Linux
CC dgregor@apple.com, ganna@apple.com, gribozavr@gmail.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
Consider following program, where neither gcc, nor clang emits any warnings:
#include <vector>
typedef std::vector<int> ivect;
int foo(ivect &x, ivect &y)
{
    int sum=0;
    for (std::vector<int>::iterator I=x.begin(), E=y.end();
     I != E; ++I) {
    sum += *I;
    }
    return sum;
}

This program will crash when dereferencing I (if either x or y is non-empty).

It is quite easy to typo the end iterator, especially when copy+pasting, it
would be nice if clang could emit a warning in this case.

BTW to compile the above code I need to add an -I to clang, otherwise it
doesn't find bits/c++config.h:
clang x.cpp -I/usr/include/c++/4.1.3/x86_64-linux-gnu/ -c
Quuxplusone commented 15 years ago

You can call foo(X,X)... but I agree that this is almost certainly an error.

Quuxplusone commented 15 years ago

This and other iterator-validity checks would be really useful. To do them well, I think they will have to be done by the static analyzer; Clang's semantic analysis doesn't have the ability to track the state (singular, dereferenceable, past-the-end) or domain (e.g., container) of individual iterators.

Quuxplusone commented 15 years ago

Yeah, good point.

Quuxplusone commented 12 years ago

cloned to radar://12522707 for internal tracking