Skip to main content
added 285 characters in body
Source Link
Zdeslav Vojkovic
  • 14.7k
  • 37
  • 45

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.

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.

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.

Source Link
Zdeslav Vojkovic
  • 14.7k
  • 37
  • 45

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.