From 175a0cccda2571dfe8a3b234d4bee39722c21b5b Mon Sep 17 00:00:00 2001 From: Jacquin Antoine Date: Sat, 28 Feb 2026 20:38:58 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20S=C3=A9curiser=20l=E2=80=99=C3=A9criture?= =?UTF-8?q?=20socket=20et=20initialiser=20APR=20dans=20les=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: aider (openrouter/openai/gpt-5.3-codex) --- src/mod_reqin_log.c | 84 ++++++++++++++++------------ tests/unit/test_header_handling.c | 17 +++++- tests/unit/test_json_serialization.c | 16 +++++- 3 files changed, 80 insertions(+), 37 deletions(-) diff --git a/src/mod_reqin_log.c b/src/mod_reqin_log.c index c0958ed..104ff2a 100644 --- a/src/mod_reqin_log.c +++ b/src/mod_reqin_log.c @@ -28,6 +28,10 @@ #include #include +#ifndef MSG_NOSIGNAL +#define MSG_NOSIGNAL 0 +#endif + /* Maximum Unix socket path length (sun_path is typically 108 bytes) */ #define MAX_SOCKET_PATH_LEN (sizeof(((struct sockaddr_un *)0)->sun_path) - 1) @@ -513,8 +517,14 @@ static int write_to_socket(const char *data, apr_size_t len, server_rec *s, reqin_log_config_t *cfg, reqin_log_child_state_t *state) { int fd; - apr_size_t total_written = 0; - apr_time_t error_interval = apr_time_from_sec(cfg->error_report_interval); + ssize_t n; + apr_time_t error_interval; + + if (!cfg || !state || !s || !data || len == 0) { + return -1; + } + + error_interval = apr_time_from_sec(cfg->error_report_interval); FD_MUTEX_LOCK(state); @@ -524,42 +534,46 @@ static int write_to_socket(const char *data, apr_size_t len, server_rec *s, return -1; } - while (total_written < len) { - ssize_t n = write(fd, data + total_written, len - total_written); - if (n < 0) { - int err = errno; - apr_time_t now = apr_time_now(); - int should_report = 0; - int conn_lost = (err == EPIPE || err == ECONNRESET || err == ENOTCONN); + n = send(fd, data, len, MSG_DONTWAIT | MSG_NOSIGNAL); + if (n < 0) { + int err = errno; + apr_time_t now = apr_time_now(); + int should_report = 0; + int conn_lost = (err == EPIPE || err == ECONNRESET || err == ENOTCONN); - if (conn_lost) { - close(fd); - state->socket_fd = -1; - state->connect_failed = 1; - } - - if (err != EAGAIN && err != EWOULDBLOCK && - (now - state->last_error_report) >= error_interval) { - state->last_error_report = now; - should_report = 1; - } - - FD_MUTEX_UNLOCK(state); - - if (should_report) { - if (conn_lost) { - ap_log_error(APLOG_MARK, APLOG_ERR, err, s, - MOD_REQIN_LOG_NAME ": Unix socket write failed: connection lost"); - } else { - ap_log_error(APLOG_MARK, APLOG_ERR, err, s, - MOD_REQIN_LOG_NAME ": Unix socket write failed"); - } - } - - return -1; + if (conn_lost) { + close(fd); + state->socket_fd = -1; + state->connect_failed = 1; } - total_written += (apr_size_t)n; + if (err != EAGAIN && err != EWOULDBLOCK && + (now - state->last_error_report) >= error_interval) { + state->last_error_report = now; + should_report = 1; + } + + FD_MUTEX_UNLOCK(state); + + if (should_report) { + if (conn_lost) { + ap_log_error(APLOG_MARK, APLOG_ERR, err, s, + MOD_REQIN_LOG_NAME ": Unix socket write failed: connection lost"); + } else { + ap_log_error(APLOG_MARK, APLOG_ERR, err, s, + MOD_REQIN_LOG_NAME ": Unix socket write failed"); + } + } + + return -1; + } + + if ((apr_size_t)n < len) { + close(fd); + state->socket_fd = -1; + state->connect_failed = 1; + FD_MUTEX_UNLOCK(state); + return -1; } FD_MUTEX_UNLOCK(state); diff --git a/tests/unit/test_header_handling.c b/tests/unit/test_header_handling.c index d575500..e957600 100644 --- a/tests/unit/test_header_handling.c +++ b/tests/unit/test_header_handling.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include /* Mock header truncation function */ static char *truncate_header_value(apr_pool_t *pool, const char *value, int max_len) @@ -191,6 +193,19 @@ static void test_header_value_unicode(void **state) apr_pool_destroy(pool); } +static int group_setup(void **state) +{ + (void)state; + return apr_initialize(); +} + +static int group_teardown(void **state) +{ + (void)state; + apr_terminate(); + return 0; +} + int main(void) { const struct CMUnitTest tests[] = { @@ -207,5 +222,5 @@ int main(void) cmocka_unit_test(test_header_value_unicode), }; - return cmocka_run_group_tests(tests, NULL, NULL); + return cmocka_run_group_tests(tests, group_setup, group_teardown); } diff --git a/tests/unit/test_json_serialization.c b/tests/unit/test_json_serialization.c index 864ecd1..5da5345 100644 --- a/tests/unit/test_json_serialization.c +++ b/tests/unit/test_json_serialization.c @@ -12,6 +12,7 @@ #include #include #include +#include typedef struct { char *data; @@ -235,6 +236,19 @@ static void test_json_escape_user_agent(void **state) apr_pool_destroy(pool); } +static int group_setup(void **state) +{ + (void)state; + return apr_initialize(); +} + +static int group_teardown(void **state) +{ + (void)state; + apr_terminate(); + return 0; +} + int main(void) { const struct CMUnitTest tests[] = { @@ -248,5 +262,5 @@ int main(void) cmocka_unit_test(test_json_escape_user_agent), }; - return cmocka_run_group_tests(tests, NULL, NULL); + return cmocka_run_group_tests(tests, group_setup, group_teardown); }