ModbusBridge write memleak fix (19733) (#19758)

Co-authored-by: JeroenSt <nospam@nospam.org>
This commit is contained in:
JeroenSt 2023-10-16 08:26:26 +02:00 committed by GitHub
parent 8467b41290
commit 91ba05ac3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View File

@ -109,7 +109,7 @@ uint8_t TasmotaModbus::Send(uint8_t device_address, uint8_t function_code, uint1
} }
else if ((function_code == 5) || (function_code == 6)) else if ((function_code == 5) || (function_code == 6))
{ {
if (write_data == NULL) if (write_data == nullptr)
{ {
free(frame); free(frame);
#ifdef TASMOTAMODBUSDEBUG #ifdef TASMOTAMODBUSDEBUG
@ -135,7 +135,7 @@ uint8_t TasmotaModbus::Send(uint8_t device_address, uint8_t function_code, uint1
frame[framepointer++] = byte_count; frame[framepointer++] = byte_count;
if (write_data == NULL) if (write_data == nullptr)
{ {
free(frame); free(frame);
#ifdef TASMOTAMODBUSDEBUG #ifdef TASMOTAMODBUSDEBUG

View File

@ -208,9 +208,15 @@ bool ModbusBridgeBegin(void)
ClaimSerial(); ClaimSerial();
} }
AddLog(LOG_LEVEL_DEBUG, PSTR("MBS: MBR %s ser init at %d baud"), (2 == result ? "HW" : "SW"), Settings->modbus_sbaudrate * 300); AddLog(LOG_LEVEL_DEBUG, PSTR("MBS: MBR %s ser init at %d baud"), (2 == result ? "HW" : "SW"), Settings->modbus_sbaudrate * 300);
}
modbusBridge.buffer = (uint8_t *)malloc(MBR_RECEIVE_BUFFER_SIZE);
if (nullptr == modbusBridge.buffer) modbusBridge.buffer = (uint8_t *)malloc(MBR_RECEIVE_BUFFER_SIZE);
if (nullptr == modbusBridge.buffer)
{
ModbusBridgeAllocError(PSTR("BUFFER"));
result = false;
}
}
return result; return result;
} }
@ -567,11 +573,11 @@ void ModbusBridgeInit(void)
{ {
if (PinUsed(GPIO_MBR_RX) && PinUsed(GPIO_MBR_TX)) if (PinUsed(GPIO_MBR_RX) && PinUsed(GPIO_MBR_TX))
{ {
modbusBridgeModbus = new TasmotaModbus(Pin(GPIO_MBR_RX), Pin(GPIO_MBR_TX), Pin(GPIO_MBR_TX_ENA)); if (nullptr == modbusBridgeModbus) modbusBridgeModbus = new TasmotaModbus(Pin(GPIO_MBR_RX), Pin(GPIO_MBR_TX), Pin(GPIO_MBR_TX_ENA));
ModbusBridgeBegin(); ModbusBridgeBegin();
#ifdef USE_MODBUS_BRIDGE_TCP #ifdef USE_MODBUS_BRIDGE_TCP
// If TCP bridge is enabled allocate a TCP receive buffer // If TCP bridge is enabled allocate a TCP receive buffer
modbusBridgeTCP.tcp_buf = (uint8_t *)malloc(MODBUS_BRIDGE_TCP_BUF_SIZE); if (nullptr == modbusBridgeTCP.tcp_buf) modbusBridgeTCP.tcp_buf = (uint8_t *)malloc(MODBUS_BRIDGE_TCP_BUF_SIZE);
if (nullptr == modbusBridgeTCP.tcp_buf) if (nullptr == modbusBridgeTCP.tcp_buf)
{ {
ModbusBridgeAllocError(PSTR("TCP")); ModbusBridgeAllocError(PSTR("TCP"));
@ -668,7 +674,7 @@ void ModbusTCPHandle(void)
uint8_t mbdeviceaddress = (uint8_t)modbusBridgeTCP.tcp_buf[6]; uint8_t mbdeviceaddress = (uint8_t)modbusBridgeTCP.tcp_buf[6];
uint8_t mbfunctioncode = (uint8_t)modbusBridgeTCP.tcp_buf[7]; uint8_t mbfunctioncode = (uint8_t)modbusBridgeTCP.tcp_buf[7];
uint16_t mbstartaddress = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[8]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[9])); uint16_t mbstartaddress = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[8]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[9]));
uint16_t *writeData = NULL; uint16_t *writeData = nullptr;
uint16_t count = 0; uint16_t count = 0;
modbusBridgeTCP.tcp_transaction_id = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[0]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[1])); modbusBridgeTCP.tcp_transaction_id = (uint16_t)((((uint16_t)modbusBridgeTCP.tcp_buf[0]) << 8) | ((uint16_t)modbusBridgeTCP.tcp_buf[1]));
@ -696,7 +702,7 @@ void ModbusTCPHandle(void)
modbusBridge.dataCount = 1; modbusBridge.dataCount = 1;
modbusBridge.type = ModbusBridgeType::mb_uint16; modbusBridge.type = ModbusBridgeType::mb_uint16;
writeData = (uint16_t *)malloc((byteCount / 2)+1); writeData = (uint16_t *)malloc((byteCount)+1);
if (nullptr == writeData) if (nullptr == writeData)
{ {
ModbusBridgeAllocError(PSTR("write")); ModbusBridgeAllocError(PSTR("write"));
@ -746,7 +752,7 @@ void ModbusTCPHandle(void)
void CmndModbusBridgeSend(void) void CmndModbusBridgeSend(void)
{ {
uint16_t *writeData = NULL; uint16_t *writeData = nullptr;
uint8_t writeDataSize = 0; uint8_t writeDataSize = 0;
bool bitMode = false; bool bitMode = false;
ModbusBridgeError errorcode = ModbusBridgeError::noerror; ModbusBridgeError errorcode = ModbusBridgeError::noerror;
@ -900,8 +906,8 @@ void CmndModbusBridgeSend(void)
} }
else else
{ {
writeData = (uint16_t *)malloc(modbusBridge.dataCount); writeData = (uint16_t *)malloc(modbusBridge.dataCount * 2);
if (nullptr == writeData) if (nullptr == writeData)
{ {
ModbusBridgeAllocError(PSTR("write")); ModbusBridgeAllocError(PSTR("write"));
return; return;