Skip to content

Commit 47486a4

Browse files
authored
Update OverflowCalculated.ql
1 parent d659d40 commit 47486a4

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

cpp/ql/src/Critical/OverflowCalculated.ql

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
2-
* @name Buffer not sufficient for string
3-
* @description A buffer allocated using 'malloc' may not have enough space for a string that is being copied into it. The operation can cause a buffer overrun. Make sure that the buffer contains enough room for the string (including the zero terminator).
2+
* @name Buffer overflow from insufficient space or incorrect size calculation
3+
* @description A buffer allocated using 'malloc' may not have enough space for a string being copied into it, or wide character functions may receive incorrect size parameters causing buffer overrun. Make sure that buffers contain enough room for strings (including zero terminator) and that size parameters are correctly calculated.
44
* @kind problem
5+
* @precision medium
56
* @id cpp/overflow-calculated
67
* @problem.severity warning
78
* @security-severity 9.8
@@ -40,6 +41,25 @@ predicate spaceProblem(FunctionCall append, string msg) {
4041
)
4142
}
4243

44+
predicate wideCharSizeofProblem(FunctionCall call, string msg) {
45+
exists(
46+
Variable buffer, SizeofExprOperator sizeofOp, ArrayType arrayType
47+
|
48+
// Function call is to wcsftime
49+
call.getTarget().hasGlobalOrStdName("wcsftime") and
50+
// Second argument (count parameter) is a sizeof operation
51+
call.getArgument(1) = sizeofOp and
52+
// The sizeof is applied to a buffer variable
53+
sizeofOp.getExprOperand() = buffer.getAnAccess() and
54+
// The buffer is an array of wchar_t
55+
arrayType = buffer.getType() and
56+
arrayType.getBaseType().hasName("wchar_t") and
57+
msg =
58+
"Using sizeof(" + buffer.getName() + ") passes byte count instead of wchar_t element count to wcsftime. " +
59+
"Use sizeof(" + buffer.getName() + ")/sizeof(wchar_t) or array length instead."
60+
)
61+
}
62+
4363
from Expr problem, string msg
44-
where spaceProblem(problem, msg)
64+
where spaceProblem(problem, msg) or wideCharSizeofProblem(problem, msg)
4565
select problem, msg

0 commit comments

Comments
 (0)