Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Alternative functions that limit the number of bytes copied are often recommended to mitigate buffer overflow vulnerabilities, for example:

  • strncpy() instead of strcpy()
  • fgets() instead of gets()
  • snprintf() instead of sprintf()

These function truncate strings that exceed the specified limits. Additionally, some functions such as strncpy() do not guarantee that the resulting string is null-terminated .
Truncation results in a loss of data, and in some cases, leads to software vulnerabilities.

Non-Compliant Code Example

...

The correct solution depends on the original intent.   If your intent was to truncate a string but ensure that the
result was a null-terminated string the following solution can be used.

...

An exception to this rule applies if the intent of the programmer was to convert a null-terminated byte string to a character array.  To be compliant with this standard, this intent must be made clear statement in comments.

Priority: P12 Level: L1

Failure to properly null terminate null-termianted byte strings can result in buffer overflows and the execution of arbitrary code with the permissions of the vulnerable process by an attacker.

Component

Value

Severity

3 (medium)

Likelihood

2 (probable)

Remediation cost

2 (medium)

References