refactor: fix scan-build bugs

This commit is contained in:
2025-10-09 00:19:29 +02:00
parent f406e1f648
commit 8d1e44db2d
3 changed files with 44 additions and 23 deletions

View File

@@ -2,6 +2,7 @@
#include "http/http.h" #include "http/http.h"
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <myclib/mysocket.h> #include <myclib/mysocket.h>
#include <stdio.h> #include <stdio.h>
@@ -17,6 +18,7 @@ static cws_http_s *http_new() {
if (!request) { if (!request) {
return NULL; return NULL;
} }
memset(request, 0, sizeof *request);
request->http_version = string_new("", 16); request->http_version = string_new("", 16);
request->location = string_new("", 128); request->location = string_new("", 128);
@@ -88,10 +90,16 @@ static size_t file_data(const char *path, char **data) {
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
const size_t content_length = ftell(file); const size_t content_length = ftell(file);
rewind(file); rewind(file);
if (errno != 0) {
fclose(file);
CWS_LOG_ERROR("Unable to rewind");
return 0;
}
/* Retrieve file data */ /* Retrieve file data */
*data = malloc(content_length); *data = malloc(content_length);
if (!data) { if (!*data) {
fclose(file); fclose(file);
CWS_LOG_ERROR("Unable to allocate file data"); CWS_LOG_ERROR("Unable to allocate file data");
@@ -99,11 +107,10 @@ static size_t file_data(const char *path, char **data) {
} }
/* Read file data */ /* Read file data */
size_t read_bytes = fread(data, 1, content_length, file); size_t read_bytes = fread(*data, 1, content_length, file);
fclose(file); fclose(file);
if (read_bytes != content_length) { if (read_bytes != content_length) {
free(data);
CWS_LOG_ERROR("Partial read from file"); CWS_LOG_ERROR("Partial read from file");
return 0; return 0;
@@ -119,7 +126,7 @@ static cws_server_ret http_send_resource(cws_http_s *request) {
/* TODO: Check for keep-alive */ /* TODO: Check for keep-alive */
char *data; char *data = NULL;
char *response; char *response;
size_t content_length = file_data(request->location_path->data, &data); size_t content_length = file_data(request->location_path->data, &data);
@@ -136,7 +143,7 @@ static cws_server_ret http_send_resource(cws_http_s *request) {
static size_t http_simple_html(char **response, cws_http_status_e status, char *title, char *description) { static size_t http_simple_html(char **response, cws_http_status_e status, char *title, char *description) {
char body[512]; char body[512];
memset(body, 0, sizeof(body)); memset(body, 0, sizeof body);
snprintf(body, sizeof(body), snprintf(body, sizeof(body),
"<html>\n" "<html>\n"
@@ -181,8 +188,6 @@ cws_http_s *cws_http_parse(string_s *request_str) {
if (request->method == HTTP_UNKNOWN) { if (request->method == HTTP_UNKNOWN) {
cws_http_free(request); cws_http_free(request);
cws_http_send_response(request, HTTP_NOT_IMPLEMENTED);
return NULL; return NULL;
} }
@@ -285,7 +290,7 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
} }
void cws_http_send_response(cws_http_s *request, cws_http_status_e status) { void cws_http_send_response(cws_http_s *request, cws_http_status_e status) {
char *response; char *response = NULL;
switch (status) { switch (status) {
case HTTP_OK: case HTTP_OK:
@@ -304,12 +309,30 @@ void cws_http_send_response(cws_http_s *request, cws_http_status_e status) {
break; break;
} }
} }
free(response);
} }
void cws_http_free(cws_http_s *request) { void cws_http_free(cws_http_s *request) {
if (!request) {
return;
}
if (request->headers) {
hm_free(request->headers); hm_free(request->headers);
}
if (request->http_version) {
string_free(request->http_version); string_free(request->http_version);
}
if (request->location) {
string_free(request->location); string_free(request->location);
}
if (request->location_path) {
string_free(request->location_path); string_free(request->location_path);
}
free(request); free(request);
} }

View File

@@ -12,7 +12,7 @@
#include "utils/utils.h" #include "utils/utils.h"
static void cws_server_setup_hints(struct addrinfo *hints, const char *hostname) { static void cws_server_setup_hints(struct addrinfo *hints, const char *hostname) {
memset(hints, 0, sizeof(struct addrinfo)); memset(hints, 0, sizeof *hints);
/* IPv4 or IPv6 */ /* IPv4 or IPv6 */
hints->ai_family = AF_UNSPEC; hints->ai_family = AF_UNSPEC;
@@ -49,7 +49,7 @@ cws_server_ret cws_server_setup(cws_server_s *server, cws_config_s *config) {
return CWS_SERVER_CONFIG; return CWS_SERVER_CONFIG;
} }
memset(server, 0, sizeof(cws_server_s)); memset(server, 0, sizeof *server);
/* Setup basic stuff */ /* Setup basic stuff */
struct addrinfo hints; struct addrinfo hints;
@@ -107,7 +107,7 @@ cws_server_ret cws_server_setup(cws_server_s *server, cws_config_s *config) {
cws_server_ret cws_server_start(cws_server_s *server) { cws_server_ret cws_server_start(cws_server_s *server) {
struct epoll_event events[128]; struct epoll_event events[128];
memset(events, 0, sizeof(events)); memset(events, 0, sizeof events);
int client_fd = 0; int client_fd = 0;
size_t workers_index = 0; size_t workers_index = 0;

View File

@@ -35,7 +35,7 @@ static int cws_worker_setup_epoll(cws_worker_s *worker) {
static int cws_read_data(int sockfd, string_s *str) { static int cws_read_data(int sockfd, string_s *str) {
char tmp[4096]; char tmp[4096];
memset(tmp, 0, sizeof(tmp)); memset(tmp, 0, sizeof tmp);
int bytes = sock_readall(sockfd, tmp, sizeof(tmp)); int bytes = sock_readall(sockfd, tmp, sizeof(tmp));
string_append(str, tmp); string_append(str, tmp);
@@ -44,24 +44,23 @@ static int cws_read_data(int sockfd, string_s *str) {
} }
cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) { cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) {
cws_worker_s **workers = malloc(sizeof(cws_worker_s) * workers_num); cws_worker_s **workers = malloc(workers_num * sizeof *workers);
if (workers == NULL) { if (workers == NULL) {
return NULL; return NULL;
} }
memset(workers, 0, sizeof(cws_worker_s) * workers_num); memset(workers, 0, sizeof **workers * workers_num);
for (size_t i = 0; i < workers_num; ++i) { for (size_t i = 0; i < workers_num; ++i) {
workers[i] = malloc(sizeof(cws_worker_s)); workers[i] = malloc(sizeof(cws_worker_s));
if (workers[i] == NULL) { if (workers[i] == NULL) {
for (size_t j = 0; j < i; ++j) { for (size_t j = 0; j < i; ++j) {
free(workers[j]); free(workers[j]);
return NULL;
} }
free(workers); free(workers);
return NULL;
} }
memset(workers[i], 0, sizeof(cws_worker_s)); memset(workers[i], 0, sizeof **workers);
workers[i]->config = config; workers[i]->config = config;
@@ -71,11 +70,10 @@ cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) {
if (ret == -1) { if (ret == -1) {
for (size_t j = 0; j < i; ++j) { for (size_t j = 0; j < i; ++j) {
free(workers[j]); free(workers[j]);
return NULL;
} }
free(workers); free(workers);
return NULL;
} }
} }