mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Merge pull request #12650 from gsingh93/strlen-literal-range-expr
C++: Add StrlenLiteralRangeExpr
This commit is contained in:
@@ -3,3 +3,4 @@ import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
|
||||
// Import each extension we want to enable
|
||||
import extensions.SubtractSelf
|
||||
import extensions.ConstantBitwiseAndExprRange
|
||||
import extensions.StrlenLiteralRangeExpr
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
private import cpp
|
||||
private import experimental.semmle.code.cpp.models.interfaces.SimpleRangeAnalysisExpr
|
||||
|
||||
/**
|
||||
* Provides range analysis information for calls to `strlen` on literal strings.
|
||||
* For example, the range of `strlen("literal")` will be 7.
|
||||
*/
|
||||
class StrlenLiteralRangeExpr extends SimpleRangeAnalysisExpr, FunctionCall {
|
||||
StrlenLiteralRangeExpr() {
|
||||
getTarget().hasGlobalOrStdName("strlen") and getArgument(0).isConstant()
|
||||
}
|
||||
|
||||
override int getLowerBounds() { result = getArgument(0).getValue().length() }
|
||||
|
||||
override int getUpperBounds() { result = getArgument(0).getValue().length() }
|
||||
|
||||
override predicate dependsOnChild(Expr e) { none() }
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
| test.cpp:4:3:4:8 | call to strlen | 7.0 | 7.0 |
|
||||
| test.cpp:5:3:5:8 | call to strlen | 1.8446744073709552E19 | 0.0 |
|
||||
@@ -0,0 +1,6 @@
|
||||
import cpp
|
||||
import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis
|
||||
import experimental.semmle.code.cpp.rangeanalysis.extensions.StrlenLiteralRangeExpr
|
||||
|
||||
from FunctionCall fc
|
||||
select fc, upperBound(fc), lowerBound(fc)
|
||||
@@ -0,0 +1,6 @@
|
||||
unsigned long strlen(const char *);
|
||||
|
||||
void func(const char *s) {
|
||||
strlen("literal");
|
||||
strlen(s);
|
||||
}
|
||||
Reference in New Issue
Block a user