Giter Club home page Giter Club logo

Comments (5)

JensMertelmeyer avatar JensMertelmeyer commented on August 16, 2024

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.

petergmorgan avatar petergmorgan commented on August 16, 2024

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.

JensMertelmeyer avatar JensMertelmeyer commented on August 16, 2024

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.

petergmorgan avatar petergmorgan commented on August 16, 2024

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.

JensMertelmeyer avatar JensMertelmeyer commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.