-
Hi all, I'd greatly appreciate your input here. I'm working on C code analysis and would like to use Range Analysis to detect potential out of bounds accesses in (mostly) static arrays. While I can easily use the I'm using this simple query to just print the ranges of the import cpp
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
from VariableAccess va
select lowerBound(va.getFullyConverted()), upperBound(va.getFullyConverted()), va In the C code I need to analyse, #include <stdio.h>
#include <stdlib.h>
char bar(int index, char buffer[]) {
return buffer[index]; // lowerBound == -2,147,483,648 && upperBound == 2,147,483,647
}
char bar2(int index, char buffer[]) {
return buffer[index - 1]; // lowerBound == -2,147,483,648 && upperBound == 2,147,483,647
}
char foo(int index, char buffer[]) {
if (index >= 0 && index <= 4) {
return bar(index, buffer); // lowerBound == 0 && upperBound == 4
}
else if (index > 4 && index <= 11) {
return bar2(index, buffer); // lowerBound == 5 && upperBound == 11
}
return '\0';
}
int main(int argc, char *argv[]) {
int index = atoi(argv[1]);
char buffer[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
char result = foo(index, buffer);
return 0;
}` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @elManto 👋🏻 Thanks for the question! Indeed, If you are willing to do so, it is possible to extend |
Beta Was this translation helpful? Give feedback.
Hi @elManto 👋🏻
Thanks for the question!
Indeed,
SimpleRangeAnalysis
only supports intra-procedural range analysis. I am afraid that we also don't have anything off-the-shelf for inter-procedural range analysis. This is partially because, e.g. in your code, it can be difficult to tell whetherbar
andbar2
are only ever called fromfoo
.If you are willing to do so, it is possible to extend
SimpleRangeAnalysis
with custom bounds for things by extending theSimpleRangeAnalysisExpr
class. For example, seeCustomAddFunctionCall
in this directory. You could use this approach to model bounds for particular functions, or even to link up the ranges for arguments to a call with the parameters of a f…