Skip to content

Commit 1c5dc84

Browse files
committed
Address case of strncat
1 parent 9ced913 commit 1c5dc84

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

cpp/common/src/codingstandards/cpp/OutOfBounds.qll

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,16 @@ module OOB {
379379
StrncatLibraryFunction() { this.getName() = getNameOrInternalName(["strncat", "wcsncat"]) }
380380

381381
override predicate getALengthParameterIndex(int i) {
382-
// `strncat` and `wcsncat` exclude the size of a null terminator
383-
i = 2
382+
// `strncat` and `wcsncat` exclude the size of a null terminator, but
383+
// both stops copying right after the null terminator is encountered.
384+
// In fact, they don't care if the source buffer is null-terminated
385+
// or not.
386+
none()
387+
}
388+
389+
override predicate getANullTerminatedParameterIndex(int i) {
390+
// `strncat` does not require null-terminated parameters
391+
none()
384392
}
385393
}
386394

cpp/misra/test/rules/RULE-8-7-1/test.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,15 @@ void test_wrong_buf_size(void) {
411411

412412
// strncat
413413
{
414-
char buf[64];
415-
char buf2[64];
416-
strncat(buf, buf2, sizeof(buf)); // COMPLIANT
417-
strncat(buf, buf2, sizeof(buf) + 1); // NON_COMPLIANT
418-
strncat(buf, buf2, sizeof(buf) - 1); // COMPLIANT
419-
strncat(buf + 1, buf2, sizeof(buf)); // NON_COMPLIANT
420-
strncat(buf, buf2 + 1, sizeof(buf) * 2); // NON_COMPLIANT
414+
char buf[65];
415+
char buf2[32];
416+
strncat(buf, buf2, sizeof(buf2)); // COMPLIANT
417+
strncat(buf, buf2, sizeof(buf2) + 1); // NON_COMPLIANT
418+
strncat(buf, buf2, sizeof(buf2) - 1); // COMPLIANT
419+
strncat(buf + 1, buf2, sizeof(buf2)); // COMPLIANT
420+
strncat(buf, buf2 + 1, sizeof(buf2) * 2); // NON_COMPLIANT
421421
}
422+
422423
// wcsxfrm
423424
{
424425
wchar_t wbuf[64];

0 commit comments

Comments
 (0)