[EXT_EP-7985] calloc doesn't check arguments to make sure the requested size is reasonable Created: 08/Jan/07  Updated: 25/Oct/23  Resolved: 25/Oct/23

Status: Fixed
Project: Embedded Software & Tools
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Low
Reporter: TI User Assignee: TI User
Resolution: Fixed Votes: 0
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Product: Code Generation Tools
Internal ID: CODEGEN-84
OldID: SDSCM00014430
Found In Release: ARM_15.12.0.LTS
C2000_16.9.0.LTS
ARM_18.1.0.LTS
MSP430_18.1.0.LTS
ARM_16.9.0.LTS
C2000_18.1.0.LTS
C2000_6.4.0B1
C2000_15.12.0.LTS
MSP430_4.4.0B1
MSP430_15.12.0.LTS
ARM_5.2.0B1
MSP430_16.9.0.LTS
Fix In Release: MSP430_18.9.0.STS
C2000_18.9.0.STS
ARM_18.9.0.STS
ARM_18.12.0.LTS
MSP430_18.12.0.LTS
C2000_18.12.0.LTS

 Description   

calloc(nelem, size) is supposed to allocate sufficient heap memory to hold "nelem" copies of "size" bytes, for a total of nelem*size bytes, and return a pointer to this memory, or NULL if the request cannot be satisfied. The bug here is that certain input values that represent an allocation request that is too large to fulfill could cause calloc to return a non-NULL pointer anyway because the unsigned multiplication to compute the total size needed wraps around to a smaller value that could be allocated, but wouldn't be big enough for the user's request. A calling function would assume this too-small packet had the full size requested, and would likely write beyond the end of the packet, causing memory corruption.

For example, on a 32-bit target, if the user calls calloc(0x00010001, 0x00010001), even though each argument by itself is reasonable, the request cannot be satisfied because the product is 0x000100020001, which exceeds size_t, and would wrap around to the value 0x20001. The heap might be able to handle a request of that size and would allocate some space and return a non-NULL pointer, but the allocated space wouldn't be nearly large enough for the user's request. The problem is worse on 16-bit targets, where calloc(0x0101, 0x0101) is enough to overflow size_t. It may not be obvious to the user that this overflows.


Generated at Tue Apr 08 00:57:21 CDT 2025 using Jira 9.12.17#9120017-sha1:aba4002bcd633f188b6a4bb5dd8a0e1f20b79ee4.