0

I'm having problems with byte array of chars. I'm creating file transfer program, which transfers binary data over TCP socket. So, when I'm getting data from chunks, I'm saving them to temporary container and then I have to add somewhere to hold whole data. I have tried std::vector but doesn't work well (or I'm using it wrong.)

size_t nbytes = 0; char buffer[5]; //temporary container. int result = 0; std::vector<char> abc; if (ioctl(sockfd, FIONREAD, (char*)&nbytes) < 0) { printf("[-] Error getting available data.\n"); return -1; } printf("[*] Bytes available: %lu\n", nbytes); while(true) { if(nbytes > sizeof(buffer)) { result = recv(sockfd, buffer, sizeof(buffer), 0); for(int i = 0; i < result; i++) { abc.push_back(buffer[i]); //big data causes memory corruption. } nbytes -= result; continue; } else if(nbytes <= sizeof(buffer) && nbytes != 0) { result = recv(sockfd, buffer, nbytes, 0); for(int i = 0; i < result; i++) { abc.push_back(buffer[i]); //big data causes memory corruption. } break; } else { result = 0; break; } } printf("Data Received: %s", &abc[0]); 
5
  • 1
    Is anybody else also thoroughly confused by "byte array of chars" or is that just me. Commented Mar 8, 2013 at 12:46
  • I already got used to that :) Commented Mar 8, 2013 at 12:48
  • What I don't understand is the use of the (very small) C style array as a buffer. Why not read directly into abc? Commented Mar 8, 2013 at 12:49
  • I guess he wants to read it in chunks and store it in vector, as total length is unknown in advance Commented Mar 8, 2013 at 12:50
  • cause I'm new to C++. Commented Mar 8, 2013 at 12:50

1 Answer 1

4

if recv fails it returns SOCKET_ERROR but you don't check it and instead use it as number of the bytes received. As SOCKET_ERROR is #defined to -1 you effectively read from address before buffer.

EDIT: it turns out that original answer was wrong as in that case the loop would not execute at all. However, the problem is that content of the vector is not null-terminated (as it is a plain buffer without any string semantics), so it looks like garbage, but it is actually OK.

Sign up to request clarification or add additional context in comments.

9 Comments

this is just small cut from code, missed error checking there, but everything is getting fine, but buffer is still corrupting. Thank you.
well, it is hard to analyze the code which differs from erroneous one :)
I'm testing it under the debugger, everything works fine, I'm getting whole data, but vector buffer is corrupting.. I mean, when I get data and save it to there, it shows my data + other defines chars. for example: char *abc = "1234"; received "AAAAAAAAAAA..." it prints "AAAAAAAA...1234 and some random binary characters :(
How many times do you see a for-loop of (int i=0; i<(-1); i++) executing? Nowhere in here is a buffer[result] being referenced, so I fail to see how he reads "from address before buffer". I concur errors should be checked, but this is not what is causing his error.
I am not sure at which member exactly you look at in debugger. I would expect something like that, as content of the vector is not null terminated, so you will not see clean string
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.