rspec/rules/S5273/cfamily/rule.adoc

43 lines
1.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Size argument of ``++strncat++``, ``++strlcat++`` and ``++strlcpy++`` should define the size of the destination to prevent buffer overflow.
Moreover, ``++strncat++`` always adds a terminating null character at the end of the appended characters so the size argument should be smaller than the size of the destination to let enough space for it.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
void f(char* src) {
char dest[10];
strlcpy(dest, src, sizeof(src)); // Noncompliant; size argument is the size of the source instead of the size of the destination
strncat(dest, src, sizeof(src)); // Noncompliant; size of the source instead of the size of the destination
strncat(dest, src, sizeof(dest)); // Noncompliant; size argument is too large
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
void f(char* src) {
char dest[10];
strncat(dest, src, sizeof(dest) - 1); // Compliant
}
----
 
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]