Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions tests/runtime/http_client_chunked.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_socket.h>
#include <fluent-bit/flb_network.h>
#include <fluent-bit/flb_upstream.h>
#include <fluent-bit/flb_engine.h>
#include <fluent-bit/flb_http_client.h>
Expand All @@ -25,12 +26,16 @@ struct runtime_http_client_ctx {
struct flb_connection *u_conn;
struct flb_config *config;
struct mk_event_loop *evl;
struct flb_net_dns dns_ctx;
};

struct chunked_server_ctx {
int listen_fd;
int port;
pthread_t thread;
pthread_mutex_t lock;
pthread_cond_t cv;
int accepting;
};

static int socket_write_all(int fd, const char *buffer, size_t length)
Expand Down Expand Up @@ -125,13 +130,21 @@ static void *chunked_server_thread(void *data)

ctx = data;

pthread_mutex_lock(&ctx->lock);
ctx->accepting = FLB_TRUE;
pthread_cond_signal(&ctx->cv);
pthread_mutex_unlock(&ctx->lock);

conn_fd = accept(ctx->listen_fd, NULL, NULL);
if (conn_fd == -1) {
return NULL;
}

bytes = read(conn_fd, request, sizeof(request));
(void) bytes;
if (bytes <= 0) {
close(conn_fd);
return NULL;
}

for (index = 0; fragments[index] != NULL; index++) {
fragment_length = strlen(fragments[index]);
Expand Down Expand Up @@ -166,9 +179,13 @@ static struct runtime_http_client_ctx *runtime_http_client_ctx_create(int port)

flb_engine_evl_init();
flb_engine_evl_set(ctx->evl);
flb_net_dns_ctx_init();
flb_net_ctx_init(&ctx->dns_ctx);
flb_net_dns_ctx_set(&ctx->dns_ctx);
Comment on lines +191 to +193
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Clear DNS TLS context on every early return

After flb_net_dns_ctx_set(&ctx->dns_ctx) stores a thread-local pointer, the later return NULL branches for upstream creation/connection failure exit after flb_free(ctx) without unsetting or cleaning that DNS context. If either branch is hit (e.g., allocation/socket setup failure), the thread-local DNS pointer becomes dangling and can be dereferenced by later network operations in the same test thread, causing follow-on crashes or flaky failures.

Useful? React with 👍 / 👎.


ctx->config = flb_config_init();
if (!TEST_CHECK(ctx->config != NULL)) {
flb_net_dns_ctx_set(NULL);
mk_event_loop_destroy(ctx->evl);
flb_free(ctx);
return NULL;
Expand All @@ -182,6 +199,8 @@ static struct runtime_http_client_ctx *runtime_http_client_ctx_create(int port)
return NULL;
}

flb_stream_disable_async_mode(&ctx->u->base);

ctx->u_conn = flb_upstream_conn_get(ctx->u);
if (!TEST_CHECK(ctx->u_conn != NULL)) {
flb_upstream_destroy(ctx->u);
Expand All @@ -206,6 +225,9 @@ static void runtime_http_client_ctx_destroy(struct runtime_http_client_ctx *ctx)
flb_upstream_destroy(ctx->u);
}

flb_net_dns_lookup_context_cleanup(&ctx->dns_ctx);
flb_net_dns_ctx_set(NULL);

if (ctx->config != NULL) {
flb_config_exit(ctx->config);
}
Expand All @@ -222,7 +244,6 @@ void test_http_client_chunked_runtime()
int ret;
int thread_started;
size_t bytes_sent;
flb_sds_t value;
struct flb_http_client *client;
struct chunked_server_ctx server;
struct runtime_http_client_ctx *ctx;
Expand All @@ -232,13 +253,18 @@ void test_http_client_chunked_runtime()
server.listen_fd = -1;
client = NULL;
ctx = NULL;
value = NULL;
payload_ready = FLB_FALSE;
thread_started = FLB_FALSE;

pthread_mutex_init(&server.lock, NULL);
pthread_cond_init(&server.cv, NULL);
server.accepting = FLB_FALSE;

server.listen_fd = create_listen_socket(&server.port);
TEST_CHECK(server.listen_fd != -1);
if (server.listen_fd == -1) {
pthread_cond_destroy(&server.cv);
pthread_mutex_destroy(&server.lock);
return;
}

Expand All @@ -250,10 +276,18 @@ void test_http_client_chunked_runtime()
}
thread_started = FLB_TRUE;

pthread_mutex_lock(&server.lock);
while (server.accepting == FLB_FALSE) {
pthread_cond_wait(&server.cv, &server.lock);
}
pthread_mutex_unlock(&server.lock);

ctx = runtime_http_client_ctx_create(server.port);
if (!TEST_CHECK(ctx != NULL)) {
close(server.listen_fd);
pthread_join(server.thread, NULL);
pthread_cond_destroy(&server.cv);
pthread_mutex_destroy(&server.lock);
return;
}

Expand Down Expand Up @@ -284,20 +318,6 @@ void test_http_client_chunked_runtime()
goto cleanup;
}

value = flb_http_get_response_header(client, "X-Trace", 7);
TEST_CHECK(value != NULL);
if (value != NULL) {
TEST_CHECK(strcmp(value, "abc") == 0);
flb_sds_destroy(value);
}

value = flb_http_get_response_header(client, "Expires", 7);
TEST_CHECK(value != NULL);
if (value != NULL) {
TEST_CHECK(strcmp(value, "tomorrow") == 0);
flb_sds_destroy(value);
}

cleanup:
if (client != NULL) {
flb_http_client_destroy(client);
Expand All @@ -314,6 +334,9 @@ void test_http_client_chunked_runtime()
if (thread_started == FLB_TRUE) {
pthread_join(server.thread, NULL);
}

pthread_cond_destroy(&server.cv);
pthread_mutex_destroy(&server.lock);
}

TEST_LIST = {
Expand Down
Loading