Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buffer overflow for non-batched send when the message metadata size exceeds 64KB #443

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 11 additions & 4 deletions lib/Commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ SharedBuffer Commands::newConsumerStats(uint64_t consumerId, uint64_t requestId)
return buffer;
}

PairSharedBuffer Commands::newSend(SharedBuffer& headers, BaseCommand& cmd, ChecksumType checksumType,
PairSharedBuffer Commands::newSend(SharedBuffer& originalHeaders, BaseCommand& cmd, ChecksumType checksumType,
const SendArguments& args) {
cmd.set_type(BaseCommand::SEND);
CommandSend* send = cmd.mutable_send();
Expand Down Expand Up @@ -221,9 +221,16 @@ PairSharedBuffer Commands::newSend(SharedBuffer& headers, BaseCommand& cmd, Chec
int totalSize = headerContentSize + payloadSize;
int checksumReaderIndex = -1;

headers.reset();
assert(headers.writableBytes() >= (4 + headerContentSize)); // totalSize + headerLength
headers.writeUnsignedInt(totalSize); // External frame
// By default, headers refers a static buffer whose capacity is 64KB, which can be reused for headers to
// avoid frequent memory allocation. However, if users configure many properties, the size could be great
// that results a buffer overflow. In this case, we can only allocate a new larger buffer.
originalHeaders.reset();
auto headers = originalHeaders;
if (headers.writableBytes() < (4 /* header length */ + headerContentSize)) {
headers = SharedBuffer::allocate(4 + headerContentSize);
}

headers.writeUnsignedInt(totalSize); // External frame

// Write cmd
headers.writeUnsignedInt(cmdSize);
Expand Down
31 changes: 31 additions & 0 deletions tests/ProducerTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,35 @@ TEST(ProducerTest, testFailedToCreateNewPartitionProducer) {
client.close();
}

TEST(ProducerTest, testLargeProperties) {
const std::string topic = "producer-test-large-properties-" + std::to_string(time(nullptr));
Client client(serviceUrl);
Producer producer;
ProducerConfiguration conf;
conf.setBatchingEnabled(false);
ASSERT_EQ(ResultOk, client.createProducer(topic, conf, producer));
Consumer consumer;
ASSERT_EQ(ResultOk, client.subscribe(topic, "sub", consumer));

MessageBuilder::StringMap properties;
constexpr int propertyCount = 20000;
auto builder = MessageBuilder().setContent("msg");
for (int i = 0; i < propertyCount; i++) {
builder.setProperty("key" + std::to_string(i), "value-" + std::to_string(i));
}

// ASSERT_EQ(ResultOk,
// producer.send(MessageBuilder().setContent("msg").setProperties(properties).build()));
ASSERT_EQ(ResultOk, producer.send(builder.build()));

Message msg;
ASSERT_EQ(ResultOk, consumer.receive(msg, 3000));
ASSERT_EQ(msg.getProperties().size(), propertyCount);
for (int i = 0; i < propertyCount; i++) {
auto it = msg.getProperties().find("key" + std::to_string(i));
ASSERT_NE(it, msg.getProperties().cend());
}
client.close();
}

INSTANTIATE_TEST_CASE_P(Pulsar, ProducerTest, ::testing::Values(true, false));
Loading