Open
Description
In this function for example _frame is freed and after that frame[6+i] is read. But frame actually points to _frame:
void Modbus::writeMultipleCoils(byte* frame,word startreg, word numoutputs, byte bytecount)
See code changes below:
Original code:
//Clean frame buffer
free(_frame);
_len = 5;
_frame = (byte *) malloc(_len);
if (!_frame) {
this->exceptionResponse(MB_FC_WRITE_COILS, MB_EX_SLAVE_FAILURE);
return;
}
_frame[0] = MB_FC_WRITE_COILS;
_frame[1] = startreg >> 8;
_frame[2] = startreg & 0x00FF;
_frame[3] = numoutputs >> 8;
_frame[4] = numoutputs & 0x00FF;
byte bitn = 0;
word totoutputs = numoutputs;
word i;
while (numoutputs--) {
i = (totoutputs - numoutputs) / 8;
this->Coil(startreg, bitRead(frame[6+i], bitn));
//increment the bit index
bitn++;
if (bitn == 8) bitn = 0;
//increment the register
startreg++;
}
Changed code:
byte bitn = 0;
word totoutputs = numoutputs;
word i;
word tempNumoutputs = numoutputs;
word tempStartreg = startreg;
while (tempNumoutputs) {
i = (totoutputs - tempNumoutputs) / 8;
this->Coil(tempStartreg, bitRead(frame[6+i], bitn));
//increment the bit index
bitn++;
if (bitn == 8) bitn = 0;
//increment the register
tempStartreg++;
tempNumoutputs--;
}
//Clean frame buffer
**free(_frame);**
_len = 5;
_frame = (byte *) malloc(_len);
if (!_frame) {
this->exceptionResponse(MB_FC_WRITE_COILS, MB_EX_SLAVE_FAILURE);
return;
}
_frame[0] = MB_FC_WRITE_COILS;
_frame[1] = startreg >> 8;
_frame[2] = startreg & 0x00FF;
_frame[3] = numoutputs >> 8;
_frame[4] = numoutputs & 0x00FF;
See issue #35 as well.
Metadata
Metadata
Assignees
Labels
No labels