🔒 Locked
Malloc Garbage
Advertisement
Original Post
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?
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.
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.
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.
ahh, so how do I ''null terminate'' my buffer? Would I manually have to go in and replace those characters with NULL''s?
Just wondering, Since I''m making an HTTP proxy server, does internet explorer or other browsers null-terminate their HTTP request headers?
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
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
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement