Call to memset family with unintended value
memset
or wmemset
used with possibly
incorrect arguments
Description
This defect occurs when Polyspace®
Bug Finder™ detects a use of the memset
or
wmemset
function with possibly incorrect arguments.
void *memset (void *ptr, int value, size_t num)
fills the first
num
bytes of the memory block that ptr
points
to with the specified value
. If the argument value
is incorrect, the memory block is initialized with an unintended value.
The unintended initialization can occur in the following cases.
Issue | Risk | Possible Fix |
---|---|---|
The second argument is '0' instead of
0 or '\0' . | The ASCII value of character '0' is
48 (decimal), 0x30
(hexadecimal), 069 (octal) but not
0 (or '\0' ). | If you want to initialize with '0' , use one of
the ASCII values. Otherwise, use 0 or
'\0' . |
The second and third arguments are probably reversed. For instance, the third argument is a literal and the second argument is not a literal. | If the order is reversed, a memory block of unintended size is initialized with incorrect arguments. | Reverse the order of the arguments. |
The second argument cannot be represented in a byte. | If the second argument cannot be represented in a byte, and you expect each byte of a memory block to be filled with that argument, the initialization does not occur as intended. | Apply a bit mask to the argument to produce a wrapped or truncated result that can be represented in a byte. When you apply a bit mask, make sure that it produces an expected result. For instance, replace |
Fix
The fix depends on the root cause of the defect. See fixes in the table above and code examples with fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Results in Polyspace User Interface Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: Off |
Command-Line Syntax:
MEMSET_INVALID_VALUE |
Impact: Low |
Version History
Introduced in R2015b
See Also
Find
defects (-checkers)
| Use of
memset with size argument zero
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)