Skip to content

Commit 1064002

Browse files
authored
C++: tweak overrunning write qhelp files
1 parent 8ac34f3 commit 1064002

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

cpp/ql/src/Security/CWE/CWE-120/OverrunWrite.qhelp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
<p>In this example, the call to <code>sprintf</code> writes a message of 14 characters (including the terminating null) plus the length of the string conversion of `userId` into a buffer with space for just 18 characters. As such, if `userId` is greater or equal to `10000`, the last characters overflow the buffer resulting in undefined behavior.</p>
1717

18-
<p>To fix this issue one of three changes should be made:</p>
18+
<p>To fix this issue these changes should be made:</p>
1919
<ul>
20-
<li>Preferably, replace the call to <code>sprintf</code> with <code>snprintf</code>, specifying a define or `sizeof(buffer)` as maximum length to copy. This will prevent the buffer overflow.</li>
21-
<li>If `userId` is expected to be less than `10000`, then return or throw an error if `userId` is out of bounds.</li>
22-
<li>Consider increasing the buffer size to at least 25 characters, so that the message is displayed correctly regardless of the value of `userId`.</li>
20+
<li>Control the size of the buffer by declaring it with a compile time constant</li>
21+
<li>Preferably, replace the call to <code>sprintf</code> with <code>snprintf</code>, using the defined constant size of the buffer or `sizeof(buffer)` as maximum length to write. This will prevent the buffer overflow.</li>
22+
<li>Optionally, if `userId` is expected to be less than `10000`, then return or throw an error if `userId` is out of bounds.</li>
23+
<li>Otherwise, consider increasing the buffer size to at least 25 characters, so that the message is displayed correctly regardless of the value of `userId`.</li>
2324
</ul>
2425

2526
</example>

cpp/ql/src/Security/CWE/CWE-120/VeryLikelyOverrunWrite.qhelp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
<p>To fix this issue these changes should be made:</p>
1919
<ul>
20-
<li>Preferably, replace the call to <code>sprintf</code> with <code>snprintf</code>, specifying a define or `sizeof(buffer)` as maximum length to copy. This will prevent the buffer overflow.</li>
20+
<li>Control the size of the buffer by declaring it with a compile time constant</li>
21+
<li>Preferably, replace the call to <code>sprintf</code> with <code>snprintf</code>, using the defined constant size of the buffer or `sizeof(buffer)` as maximum length to write. This will prevent the buffer overflow.</li>
2122
<li>Increasing the buffer size to account for the full range of `userId` and the terminating null character.</li>
2223
</ul>
2324

0 commit comments

Comments
 (0)