fixed invalid read on double json in buffer

This commit is contained in:
Václav Šmejkal 2025-02-02 14:28:08 +01:00
parent 90be3ebc21
commit cdbbdce47c
Signed by: ENGO150
GPG Key ID: 4A57E86482968843

View File

@ -174,28 +174,31 @@ char *read_socket_raw(int socket, WHY2_UNUSED char *key)
} }
char *content_buffer = NULL; char *content_buffer = NULL;
int content_size; size_t content_size = 0;
char *wait_buffer = why2_malloc(2); //TEMP char *wait_buffer = why2_malloc(2); //TEMP
//WAIT TILl RECEIVED MSG (ik it sucks but i can't think of better solution; anyways, this is way more convenient than infinite loop that makes my computer go wroom wroom) //WAIT TILl RECEIVED MSG (ik it sucks but i can't think of better solution; anyways, this is way more convenient than infinite loop that makes my computer go wroom wroom)
recv(socket, wait_buffer, 1, MSG_PEEK); recv(socket, wait_buffer, 1, MSG_PEEK);
why2_deallocate(wait_buffer); why2_deallocate(wait_buffer);
do //FIND THE RECEIVED SIZE
{ ioctl(socket, FIONREAD, &content_size);
//FIND THE SENT SIZE
content_size = 0;
if (ioctl(socket, FIONREAD, &content_size) < 0 || content_size <= 0) continue;
//ALLOCATE //ALLOCATE
content_buffer = why2_realloc(content_buffer, content_size + 1); content_buffer = why2_malloc(content_size + 1);
for (size_t i = 0; i < content_size; i++)
{
//END OF MESSAGE REACHED (COULD BE CAUSED BY FAST SENT MESSAGES)
if (i >= 2 && strncmp(content_buffer + i - 2, "\"}", 2) == 0) break;
//READ JSON MESSAGE //READ JSON MESSAGE
if (recv(socket, content_buffer, content_size, 0) != content_size) //READ THE MESSAGE BY CHARACTERS if (recv(socket, content_buffer + i, 1, 0) != 1) //READ THE MESSAGE BY CHARACTERS
{ {
fprintf(stderr, "Socket probably read wrongly!\n"); fprintf(stderr, "Socket probably read wrongly!\n");
break;
} }
} while (content_buffer == NULL || strncmp(content_buffer + (content_size - 2), "\"}", 2) != 0); }
content_buffer[content_size] = '\0'; //NULL TERM content_buffer[content_size] = '\0'; //NULL TERM