djhfwk 0 Posted March 31, 2022 (edited) 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 March 31, 2022 by djhfwk Share this post Link to post
David Heffernan 2345 Posted March 31, 2022 The correct solution, in my view, is to pass Pointer(RawMessage) rather than @RawMessage [1] 1 Share this post Link to post
djhfwk 0 Posted March 31, 2022 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
Angus Robertson 574 Posted March 31, 2022 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
David Heffernan 2345 Posted March 31, 2022 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. 1 Share this post Link to post
Angus Robertson 574 Posted March 31, 2022 I agree Pointer() would be better here, and probably in many other places in ICS... Angus Share this post Link to post
Fr0sT.Brutal 900 Posted April 1, 2022 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