Jump to content
djhfwk

Range check error with TSysLogServer

Recommended Posts

The UdpDataAvailable procedure should add len check before setlength,

sometimes FUdpSocket.RcvdCount will return 0, and this will cause range error in delphi 11.1, although this can be turn off by uncheck the Range Check option in Project Options.

 

procedure TSysLogServer.UdpDataAvailable(Sender: TObject; ErrCode: Word);
var
    RawMessage : AnsiString;
    Len        : Integer;
    Src        : TSockAddrIn;
    SrcLen     : Integer;
    SrcIP      : AnsiString;
    SrcPort    : AnsiString;
begin
    SrcLen := SizeOf(Src);
    Len    := FUdpSocket.RcvdCount;

    if Len = 0 then  //this should be added, but will cause the UdpDataAvailable event hang randomly
        Exit;

    SetLength(RawMessage, Len);
    Len    := FUdpSocket.ReceiveFrom(@RawMessage[1], Length(RawMessage),
                                     Src, SrcLen);
    if Len < 0 then
        Exit;
    SetLength(RawMessage, Len);
    SrcIP   := IcsStrPas(inet_ntoa(Src.sin_addr));
    SrcPort := AnsiString(IntToStr(ntohs(Src.sin_port)));
    if Assigned(FOnDataAvailable) then
        FOnDataAvailable(Self, SrcIP, SrcPort, RawMessage);
end;

 

 

Edited by djhfwk

Share this post


Link to post
9 minutes ago, David Heffernan said:

The correct solution, in my view, is to pass Pointer(RawMessage) rather than @RawMessage [1]

🤣

seems the problem is in FUdpSocket.RcvdCount , when it return 0,  i changed it to 65535 manually, FUdpSocket.ReceiveFrom still can successfuly got data

Share this post


Link to post

That function should not be using RcvdCount, it may not be accurate when it is called.  Most components receive data into a fixed size buffer, not a string, I'll change it next week. 

 

Angus

 

Share this post


Link to post
2 hours ago, djhfwk said:

🤣

seems the problem is in FUdpSocket.RcvdCount , when it return 0,  i changed it to 65535 manually, FUdpSocket.ReceiveFrom still can successfuly got data

Maybe so. But my point about how to deal with getting a pointer from a string that may be zero length stands. So long as the use of the pointer respects the zero length and doesn't try to de-reference it. 

  • Like 1

Share this post


Link to post
23 hours ago, David Heffernan said:

The correct solution, in my view, is to pass Pointer(RawMessage) rather than @RawMessage [1]

I too came to this solution and changed almost all "[1]" occurrences in my projects

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×