I’m trying to free a character pointer after having used it but it returns a strange error.
The error says:
_CrtDbgREport: String too long or IO Error
The debugger itself returns no errors while compiling.
The code currently looks like this:
void RespondToUser(SOCKET client, SOCKET server)
{
char buffer[80];
char *temp = malloc(_scprintf("HTTP/1.1 200 OK\r\n%s\r\nServer: %s\r\nConnection: close\r\n\r\nHi!", buffer, SERVER_NAME));
sprintf(temp, "HTTP/1.1 200 OK\r\n%s\r\nServer: %s\r\nConnection: close\r\n\r\nHi!", buffer, SERVER_NAME);
send(client, temp, strlen(temp), 0);
closesocket(client);
free(temp);
ListenToUsers(server);
}
The problem only occurs when I try to free the temp pointer from the memory and not otherwise. What might be causing this?
The call to
sprintf()is writing one past the end (as it appends a NULL terminator), as the return value from_scprintf()does not include the NULL terminator. From_scprintf()reference page:This means the program has undefined behaviour. To correct
+ 1to_scprintf()s return value.