fix: Sécuriser l’écriture socket et initialiser APR dans les tests

Co-authored-by: aider (openrouter/openai/gpt-5.3-codex) <aider@aider.chat>
This commit is contained in:
Jacquin Antoine
2026-02-28 20:38:58 +01:00
parent ce1692bfba
commit 175a0cccda
3 changed files with 80 additions and 37 deletions

View File

@ -28,6 +28,10 @@
#include <stdlib.h>
#include <limits.h>
#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);

View File

@ -10,6 +10,8 @@
#include <stdio.h>
#include <apr_strings.h>
#include <apr_tables.h>
#include <apr_pools.h>
#include <apr_general.h>
/* 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);
}

View File

@ -12,6 +12,7 @@
#include <apr_strings.h>
#include <apr_time.h>
#include <apr_lib.h>
#include <apr_general.h>
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);
}