Skip to content

Commit e257ad5

Browse files
committed
Fix possible issue with realloc usage
Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 6bb57a8 commit e257ad5

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

libraries/Wire/src/Wire.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,13 @@ void TwoWire::allocateRxBuffer(size_t length)
435435
if(rxBufferAllocated < length) {
436436
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
437437
if(length < BUFFER_LENGTH) { length = BUFFER_LENGTH; }
438-
rxBuffer = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
439-
rxBufferAllocated = (rxBuffer != nullptr) ? length: 0;
438+
uint8_t *tmp = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
439+
if(tmp != nullptr) {
440+
rxBuffer = tmp;
441+
rxBufferAllocated = length;
442+
} else {
443+
_Error_Handler("No enough memory! (%i)\n", length);
444+
}
440445
}
441446
}
442447

@@ -445,8 +450,13 @@ inline void TwoWire::allocateTxBuffer(size_t length)
445450
if(txBufferAllocated < length) {
446451
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
447452
if(length < BUFFER_LENGTH) { length = BUFFER_LENGTH; }
448-
txBuffer = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
449-
txBufferAllocated = (txBuffer != nullptr) ? length: 0;
453+
uint8_t *tmp = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
454+
if(tmp != nullptr) {
455+
txBuffer = tmp;
456+
txBufferAllocated = length;
457+
} else {
458+
_Error_Handler("No enough memory! (%i)\n", length);
459+
}
450460
}
451461
}
452462

0 commit comments

Comments
 (0)