rspec/rules/S5782/cfamily/how-to-fix-it/buffer-overflow.adoc
Sebastien Andrivet e5e7204f41
Modify rule S5782: Change text to education framework format (APPSEC-1211) (#3346)
## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the
guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)

---------

Co-authored-by: gaetan-ferry-sonarsource <112399173+gaetan-ferry-sonarsource@users.noreply.github.com>
2023-10-24 16:10:06 +02:00

26 lines
780 B
Plaintext

== How to fix it
=== Code examples
In the following code example, ``memchr`` is used to find the first occurrence of the ``@`` character in a buffer, ``array``. The second argument does not match the size of this buffer, so it may find an occurrence of the character after the end of the buffer and read data it is not supposed to. If the returned ``pos`` pointer is later used to write characters, this could unexpectedly overwrite data.
==== Noncompliant code example
[source,cpp,diff-id=1,diff-type=noncompliant]
----
char array[10];
initialize(array);
char *pos = memchr(array, '@', 42); // Noncompliant
----
==== Compliant solution
[source,cpp,diff-id=1,diff-type=compliant]
----
char array[10];
initialize(array);
char *pos = memchr(array, '@', sizeof(array));
----