From cdb8f500f95ed6e5b752d361b403deb8d8dc1aac Mon Sep 17 00:00:00 2001 From: frosty Date: Fri, 10 Apr 2026 17:08:06 -0400 Subject: fix: resolved potential undefined behaviour --- src/routing.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/routing.c') diff --git a/src/routing.c b/src/routing.c index d5d3969..1ae4ce9 100644 --- a/src/routing.c +++ b/src/routing.c @@ -322,16 +322,26 @@ bool serve_static_file_with_mime(const char *request_path_relative_to_static, co char file_buffer[BUFFER_SIZE]; size_t bytes_read; + bool send_error = false; - while ((bytes_read = fread(file_buffer, 1, sizeof(file_buffer), fp)) > 0) { + clearerr(fp); + while (!send_error && !feof(fp) && !ferror(fp) && (bytes_read = fread(file_buffer, 1, sizeof(file_buffer), fp)) > 0) { if (send(current_client_socket, file_buffer, bytes_read, 0) < 0) { perror("Error sending static file content"); fprintf(stderr, "[ERROR] serve_static_file_with_mime: Failed to send content for '%s'.\n", full_static_path); - break; + send_error = true; } } + if (ferror(fp)) { + perror("Error reading static file"); + fprintf(stderr, "[ERROR] serve_static_file_with_mime: Failed to read '%s'.\n", + full_static_path); + fclose(fp); + return true; + } + fclose(fp); return true; } -- cgit v1.2.3