EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Bug in HTTPClient OnData?

Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.
#28229
Posted: 01/31/2014 14:47:21
by GERALD MORRIS (Standard support level)
Joined: 11/08/2010
Posts: 27

When HTTPClient returns data in chunks and the return data has a leading null #0 character, the Size is wrong.
I had to make the following tweaks to Size to compensate for this
It works for me now.
Code
procedure TForm1.HTTPClientData(Sender: TObject; Buffer: Pointer;
  Size: Integer);
var
  S : {$ifndef SB_UNICODE_VCL}string{$else}AnsiString{$endif};
  Bytes: ByteArray;
  s1,vfilename:string;
begin
  SetLength(Bytes, Size + 1);
  Move(Buffer^, Bytes[1], Size + 2);
  S := sbutils.StringOfBytes(Bytes);
  Delete(s,1,1); //deletes the leading #0
#28230
Posted: 01/31/2014 16:15:24
by Ken Ivanov (EldoS Corp.)

Hello Gerald,

Thank you for contacting us.

Could you please explain what exactly is wrong with the Size parameter and what exactly problem it is causing and on what platform?

I can hardly agree with your code, sorry. Not only it contains a mistake (the Move() call is bringing in two buffer overruns), it just corrupts the output of the TElHTTPSClient object.
#28231
Posted: 01/31/2014 16:56:18
by GERALD MORRIS (Standard support level)
Joined: 11/08/2010
Posts: 27

Hi Ken
Here is your code from your sample
Code
procedure Tfrmequifax.HTTPClientData(Sender: TObject; Buffer: Pointer;
  Size: Integer);
var
  S : {$ifndef SB_UNICODE_VCL}string{$else}AnsiString{$endif};
  Bytes: ByteArray;
begin
  SetLength(Bytes, Size);
  Move(Buffer^, Bytes[1], Size);
  S := sbutils.StringOfBytes(Bytes);


It make sense but it DOES NOT WORK when the first character in the buffer is a #0

Here is my code (cleaned up) - which does not make sense - but WORKS
(The buffer contained a leading #0.) There is probably a better way to do it.

Code
procedure Tfrmequifax.HTTPClientData(Sender: TObject; Buffer: Pointer;
  Size: Integer);
var
  S : {$ifndef SB_UNICODE_VCL}string{$else}AnsiString{$endif};
  Bytes: ByteArray;
  s1:string;
begin
  SetLength(Bytes, Size + 1);
  Move(Buffer^, Bytes[1], Size + 1);
  S := sbutils.StringOfBytes(Bytes);
  if length(s)>0 then
  begin
    s1:=inttostr(ord(s[1]));
    if ord(s[1])=0 then Delete(s,1,1);
  end;


Gerald
#28232
Posted: 01/31/2014 17:00:23
by GERALD MORRIS (Standard support level)
Joined: 11/08/2010
Posts: 27

I am using SBB Version 10.0.229 in DelphiXE2 on Winserver 2008 R2 (64bit OS)
#28233
Posted: 01/31/2014 17:21:32
by Ken Ivanov (EldoS Corp.)

Gerald,

Hmm, it seems to be a bug of the sample (which one are you using BTW?). The correct handler moves the data at the 0th offset of the Bytes array:
Code
  Size: Integer);
var
  S : {$ifndef SB_UNICODE_VCL}string{$else}AnsiString{$endif};
  Bytes: ByteArray;
begin
  SetLength(Bytes, Size);
  Move(Buffer^, Bytes[0], Size); // <------ note Bytes[0]
  S := sbutils.StringOfBytes(Bytes);
end;


I do not have the version 10 sample by hand, but the code in version 11 is correct - just like in the snippet I posted above. So, if the issue used to be there at some point in time, it has been eventually fixed.

Thank you anyway for reporting a problem, it might be useful for other users. Still, I want to stress that the correct fix is to copy the data at the 0th offset of the Bytes array, not to truncate the converted string.
#28234
Posted: 01/31/2014 18:04:13
by GERALD MORRIS (Standard support level)
Joined: 11/08/2010
Posts: 27

Fantastic
Will correct my code as shown
Tks!
Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages

Reply

Statistics

Topic viewed 1341 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!