Skip to main content
gamedev.net
🔒 Locked

Malloc Garbage

Started by MattCarpenter 7 replies 300+ views
Original Post
MattCarpenter
MattCarpenter
soooo I finally got my winsock stuff in vc++ to copy received data into a buffer. I declare the buffer and malloc it like this: LPVOID buffer; buffer=malloc(255); I then use the CAsyncSocket::Receive function to put data into this buffer thing. When I do MessageBox(NULL,(LPCTSTR)buffer...., It shows the data that was received by the winsock, but it also shows a bunch of garbage y''ish characters after it in the message box. Is this actually data thats in the buffer, or could it be some messagebox quirk?
//att
niyaw
niyaw
unless you terminated whatever you sent with 0, you''ll have garbage after the stuff you''ve received.

also, keep in mind that stream-oriented protocols (educated guess at what you''re using) don''t have to transfer entire string at once. are you sure you got the string completely before printing it? winsock won''t null-terminate your string in the middle of it.
mutex
mutex
Those "y"ish characters is actually data prepended and appended to all buffers created with debug malloc to aid in debugging situations where you overstep the bounds of an array. The reason MessageBox shows them is because your data isn''t null terminated, and thinks the string hasn''t ended yet, thus printing the stuff after your buffer.

MattCarpenter
MattCarpenter
ahh, so how do I ''null terminate'' my buffer? Would I manually have to go in and replace those characters with NULL''s?
//att
niyaw
niyaw
send a null-terminated string and make sure you receive all of it before printing it.

in general, to null-terminate a buffer 'buf' at position 'pos', you'd do

buf[pos] = 0;

[edited by - niyaw on December 12, 2002 10:55:45 PM]
MattCarpenter
MattCarpenter
Just wondering, Since I''m making an HTTP proxy server, does internet explorer or other browsers null-terminate their HTTP request headers?
//att
barazor
barazor
when allocating an array, you should use calloc() instead of malloc()

the format is:

void * calloc(size_t num,size_t size)

this ensures that the array is allocated in contiguous memory, and it also sets all the allocated elements to 0

so, instead of using malloc(255), the call would be calloc(255,sizeof(char))

or you could also use memset on the malloc call to achieve the same null termination effect, but calloc was really designed for this
Shannon Barber
Shannon Barber
quote:
Original post by MattCarpenter
Just wondering, Since I''m making an HTTP proxy server, does internet explorer or other browsers null-terminate their HTTP request headers?


I don''t think so - it''s like sending and receiving text files. They ought to have CRLF at the end of each line.
Even if a protocol said that strings are to be null-terminated, I wouldn''t trust it. It''s a huge source of bugs and security flaws.

Size First, Always First.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Teggy
Teggy
quote:

I don''t think so - it''s like sending and receiving text files. They ought to have CRLF at the end of each line.



Also, i''m pretty sure that the end of the header is signified by a blank line. ie. a single CRLF

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.