Skip to content

Commit fb03561

Browse files
authored
C++: add docstrings to Printf and BufferWrite
1 parent aa68c51 commit fb03561

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

cpp/ql/lib/semmle/code/cpp/commons/Printf.qll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,11 @@ class FormatLiteral extends Literal {
10211021
*/
10221022
int getMaxConvertedLength(int n) { result = max(int l | l = getMaxConvertedLength(n, _) | l) }
10231023

1024+
/**
1025+
* Gets the maximum length of the string that can be produced by the nth
1026+
* conversion specifier of this format string, specifying the estimation reason;
1027+
* has no result if this cannot be determined.
1028+
*/
10241029
int getMaxConvertedLength(int n, BufferWriteEstimationReason reason) {
10251030
exists(int len |
10261031
(
@@ -1238,6 +1243,13 @@ class FormatLiteral extends Literal {
12381243
result = max(int l | l = getMaxConvertedLengthLimited(n, _) | l)
12391244
}
12401245

1246+
/**
1247+
* Gets the maximum length of the string that can be produced by the nth
1248+
* conversion specifier of this format string, specifying the reason for the
1249+
* estimation, except that float to string conversions are assumed to be 8
1250+
* characters. This is helpful for determining whether a buffer overflow is
1251+
* caused by long float to string conversions.
1252+
*/
12411253
int getMaxConvertedLengthLimited(int n, BufferWriteEstimationReason reason) {
12421254
if this.getConversionChar(n).toLowerCase() = "f"
12431255
then result = this.getMaxConvertedLength(n, reason).minimum(8)
@@ -1319,10 +1331,21 @@ class FormatLiteral extends Literal {
13191331
*/
13201332
int getMaxConvertedLengthLimited() { result = this.getMaxConvertedLengthAfterLimited(0, _) }
13211333

1334+
/**
1335+
* Gets the maximum length of the string that can be produced by this format
1336+
* string, specifying the reason for the estimate. Has no result if no estimate
1337+
* can be found.
1338+
*/
13221339
int getMaxConvertedLengthWithReason(BufferWriteEstimationReason reason) {
13231340
result = this.getMaxConvertedLengthAfter(0, reason)
13241341
}
13251342

1343+
/**
1344+
* Gets the maximum length of the string that can be produced by this format
1345+
* string, specifying the reason for the estimate, except that float to string
1346+
* conversions are assumed to be 8 characters. This is helpful for determining
1347+
* whether a buffer overflow is caused by long float to string conversions.
1348+
*/
13261349
int getMaxConvertedLengthLimitedWithReason(BufferWriteEstimationReason reason) {
13271350
result = this.getMaxConvertedLengthAfterLimited(0, reason)
13281351
}

cpp/ql/lib/semmle/code/cpp/security/BufferWrite.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,19 @@ abstract class BufferWrite extends Expr {
7979
*/
8080
int getMaxDataLimited() { result = max(int d | d = getMaxDataLimited(_) | d) }
8181

82+
/**
83+
* Gets an upper bound to the amount of data that's being written (if one
84+
* can be found), specifying the reason for the estimation
85+
*/
8286
int getMaxData(BufferWriteEstimationReason reason) { none() }
8387

88+
/**
89+
* Gets an upper bound to the amount of data that's being written (if one
90+
* can be found), specifying the reason for the estimation, except that
91+
* float to string conversions are assumed to be much smaller (8 bytes)
92+
* than their true maximum length. This can be helpful in determining the
93+
* cause of a buffer overflow issue.
94+
*/
8495
int getMaxDataLimited(BufferWriteEstimationReason reason) { result = getMaxData(reason) }
8596

8697
/**

0 commit comments

Comments
 (0)