Fix for CWE-190: Integer Overflow or Wraparound #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🐕 Corgea issued a PR to fix a vulnerability found in lib/libwasm.c.
It is CWE-190: Integer Overflow or Wraparound that has a severity of 🔴 High.
🪄 Fix explanation
The fix prevents integer overflow when calculating memory allocation size by casting to "size_t" and validating that adding 1 doesn’t wrap around, ensuring safe allocation and mitigating overflow-related vulnerabilities.
- Cast "field_len" to "size_t" before adding 1 to avoid overflow in pointer-size arithmetic: "size_t alloc_size = (size_t) field_len + 1;".
- Verify no wraparound by checking "alloc_size <= field_len"; if true, jump to error handling to prevent unsafe allocation.
- Replace original allocation with "calloc(1, alloc_size)", ensuring correct memory size is requested.
- Add a null check for the allocation result, jumping to "parse_error" if allocation fails, improving robustness.
💡 Important Instructions
Ensure that
parse_errorhandles cleanup correctly to prevent memory leaks or undefined behavior after this early exit.See the issue and fix in Corgea.