Open edwintorok opened 15 years ago
cloned to radar://12522707 for internal tracking
Yeah, good point.
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.
You can call foo(X,X)... but I agree that this is almost certainly an error.
Extended Description
Consider following program, where neither gcc, nor clang emits any warnings:
include
typedef std::vector ivect;
int foo(ivect &x, ivect &y)
{
int sum=0;
for (std::vector::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