forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gulshan Singh
committed
Mar 24, 2023
1 parent
bb27ba7
commit b87f12d
Showing
5 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
cpp/ql/lib/experimental/semmle/code/cpp/rangeanalysis/extensions/StrlenLiteralRangeExpr.qll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() } | ||
} |
2 changes: 2 additions & 0 deletions
2
...l/test/experimental/library-tests/rangeanalysis/strlenliteral/StrlenLiteralRange.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | |
6 changes: 6 additions & 0 deletions
6
cpp/ql/test/experimental/library-tests/rangeanalysis/strlenliteral/StrlenLiteralRange.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
6 changes: 6 additions & 0 deletions
6
cpp/ql/test/experimental/library-tests/rangeanalysis/strlenliteral/test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
unsigned long strlen(const char *); | ||
|
||
void func(const char *s) { | ||
strlen("literal"); | ||
strlen(s); | ||
} |