From 698722e91e2674289b969fcaba4643134388c7b5 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Fri, 11 Oct 2024 13:08:56 -0400 Subject: [PATCH] Fix max_allowed_packet math Prior to this commit our `max_allowed_packed` would fail any time the `len` of the data being written to the buffer was itself greater than the configured `max_allowed_packet`. This is because both the `len` and the `max_allowed_packet` (aka `builder->packet_max_length`) are unsigned (`size_t`). This commit bypasses the problem by adding to the left side instead of subtracting from the right. --- contrib/ruby/test/client_test.rb | 6 ++++++ src/builder.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/contrib/ruby/test/client_test.rb b/contrib/ruby/test/client_test.rb index b6f7bfff..6b968e98 100644 --- a/contrib/ruby/test/client_test.rb +++ b/contrib/ruby/test/client_test.rb @@ -832,6 +832,12 @@ def test_configured_max_packet_below_server assert_equal "trilogy_query_send: TRILOGY_MAX_PACKET_EXCEEDED", exception.message + exception = assert_raises Trilogy::QueryError do + client.query query_for_target_packet_size(32 * 1024 * 1024 + 1) + end + + assert_equal "trilogy_query_send: TRILOGY_MAX_PACKET_EXCEEDED", exception.message + assert client.ping ensure ensure_closed client diff --git a/src/builder.c b/src/builder.c index 6603412f..5ecb40d5 100644 --- a/src/builder.c +++ b/src/builder.c @@ -182,7 +182,7 @@ int trilogy_builder_write_buffer(trilogy_builder_t *builder, const void *data, s size_t fragment_remaining = TRILOGY_MAX_PACKET_LEN - builder->fragment_length; - if (builder->packet_length >= builder->packet_max_length - len) { + if (builder->packet_length + len >= builder->packet_max_length) { return TRILOGY_MAX_PACKET_EXCEEDED; }