Article View: borland.public.cppbuilder.internet.socket
Article #3265Re: i need ideas/comments with tclientsocket
From: Hans Galema
Date: Tue, 01 Nov 2005 08:41
Date: Tue, 01 Nov 2005 08:41
42 lines
2460 bytes
2460 bytes
Remy Lebeau (TeamB) wrote:
>> if ( MemoryStream->Position + receivelength >= MemoryStream->Size )
>> MemoryStream->Size += receivelength + 1; // +1 is not needed I think
>
> No, +1 is not needed. Neither is checking the (Position + receivelength),
> either. TMemoryStream's Size property sets the Capacity property, which
> already checks the new size before actually allocating memory.
> TMemoryStream allocates its Memory in 8K chunks.
The adjusting of Size is needed. You have to see it in the context
of the statement that folows:
int nread = Socket->ReceiveBuf (
((char*)MemoryStream->Memory) + MemoryStream->Position, receivelength );
If ->Memory had not sufficiend Size then above statement, that puts bytes
directly in the Memory-buffer, would produce a bufferoverflow.
If I had used TMemoryString::Write() then Size had been adjusted automatically.
See following code that will cause the app to exit because of a bufferoverflow.
To my surprise codeguard does nothing here.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream *MemoryStream = new TMemoryStream;
MemoryStream->Size = 1000;
char *ptr = (char*)MemoryStream->Memory;
int size = 20000;
while ( --size )
*ptr++ = 0;
delete MemoryStream;
}
Hans.
Technical Details
Message-ID:
<43672a80$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsfeed.gamma.ru!Gamma.RU!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<43662baf$1@newsgroups.borland.com> <43664511@newsgroups.borland.com> <4366672a$1@newsgroups.borland.com> <43668460@newsgroups.borland.com> <4366d220$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,5c33b8c6a9ca7fdb X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Tue, 01 Nov 2005 09:41:30 +0100 From: Hans Galema <notused@notused.nl> User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.internet.socket Subject: Re: i need ideas/comments with tclientsocket References: <43662baf$1@newsgroups.borland.com> <43664511@newsgroups.borland.com> <4366672a$1@newsgroups.borland.com> <43668460@newsgroups.borland.com> <4366d220$1@newsgroups.borland.com> In-Reply-To: <4366d220$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 145.98.201.248 Message-ID: <43672a80$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1130834560 145.98.201.248 (1 Nov 2005 00:42:40 -0700) Lines: 42 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsfeed.gamma.ru!Gamma.RU!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail