rspec/rules/S3519/cfamily/rule.adoc

36 lines
1.3 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Array overruns and buffer overflows happen when memory access accidentally goes beyond the boundary of the allocated array or buffer. These overreaching accesses cause some of the most damaging, and hard to track defects.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
int array[10];
array[10] = 0; // Noncompliant: index should be between 0 & 9
char *buffer1 = (char *) malloc(100);
char *buffer2 = (char *) malloc(50);
memcpy(buffer2, buffer1, 100); // Noncompliant: buffer2 will overflow.
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
int array[10];
array[9] = 0;
char *buffer1 = (char *) malloc(100);
char *buffer2 = (char *) malloc(50);
memcpy(buffer2, buffer1, 50);
----
2021-04-28 16:49:39 +02:00
== See
* https://cwe.mitre.org/data/definitions/119.html[MITRE, CWE-119] - Improper Restriction of Operations within the Bounds of a Memory Buffer
* https://cwe.mitre.org/data/definitions/131[MITRE, CWE-131] - Incorrect Calculation of Buffer Size
* https://cwe.mitre.org/data/definitions/788.html[MITRE, CWE-788] - Access of Memory Location After End of Buffer
* https://wiki.sei.cmu.edu/confluence/x/wtYxBQ[CERT, ARR30-C.] - Do not form or use out-of-bounds pointers or array subscripts
* https://wiki.sei.cmu.edu/confluence/x/i3w-BQ[CERT, STR50-CPP.] - Guarantee that storage for strings has sufficient space for character data and the null terminator