Comments (5)
Yes, using Move(..)
without any checks is extremely dangerous. A crash to desktop is plausible, since Move(..)
does not do any checks at all. I believe replacing this line with
ReceiveBuffer := TBitConverter.InTo<TModBusResponseBuffer>( TBytes(RecBuffer) );
with no other changes at all does solve the memory corruption issue.
The current behaviour is going to be the same when the reply is too long but will now raise a range check error (ERangeError
) when the received reply is not sufficient to fill a TModbusResponseBuffer
.
Please let me know if I should submit a pull request.
from delphi-modbus.
TBitConverter isn't available in D2007 but I agree that it is better an error is returned.
maybe something like:
//compare the size of the input buffer against the max packet size
if iSize<=Sizeof(ReceiveBuffer) then
Move(RecBuffer[0], ReceiveBuffer, iSize)
else
raise Exception.Create(sBufferLengthExceptionMessage);
Then at least an exception is raised if the buffer is an invalid size.
from delphi-modbus.
Yes, but this alone is not going to solve your problem when there is too much data (when iSize > SizeOf(ReceiveBuffer)
). Then Move(..)
will still corrupt your memory as it will write beyond the end of ReceiveBuffer
. Another check is needed as well.
(Having to support environments older than a decade is such a bane for Delphi code.)
from delphi-modbus.
Would the if iSize<=sizeof(ReceiveBuffer) then...
check not be enough to prevent the move operation all all instances where the server has returned too much data? (I may have missed further move commands in the module - this was the only one that caused us a problem so far)
from delphi-modbus.
No: iSize
is the number of raw bytes you received from the network buffer (RecBuffer
). We are trying to copy those bytes into a record of type TModbusResponseBuffer
(which has a size of 269 bytes). If we have more than 269 bytes in our RecBuffer
(this means iSize > SizeOf(ReceiveBuffer)
) then the Move(..)
command will first completely fill the TModbusResponseBuffer
-record. After that, it will happy write the remaining bytes into the memory after that. This leads to memory corruption.
from delphi-modbus.
Related Issues (20)
- Compatibility with Delphi 10.4 HOT 2
- Incorrect platform identifier in "ModBusPlatforms.inc" for Delphi XE5
- ModBusClient.ReadHoldingRegisters - Amount HOT 1
- ModBusMaster Demo reads values fine but gives an error when writing values
- ModBusClient - cycle read ? HOT 1
- Modbus client for service application HOT 1
- Runtime error 216 HOT 2
- XE11 Alexandria HOT 1
- TIdModBusServer: WriteRegisters gets triggered multiple times HOT 3
- The register number to be read is inconsistent with the PLC register number actually read HOT 2
- Read and write registers 4x HOT 1
- TIdModBusClient.Connect don`t work in manual mode
- Cannot read the Shark270
- Fix issues on poor network and poor Modbus controllers
- TIdModBusServer Connection Status public property
- How to check for errors
- ALGUEM AJUDAR INSTALAR XE7 HOT 1
- Problem install with Lazarus 2.2.6 HOT 1
- Need modbus private function support
- Support for Function Code 43 - Read Device Identification
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from delphi-modbus.