Skip to content

Commit 58edbcb

Browse files
authored
Merge pull request karafka#200 from mensfeld/fix-190
fix crash on empty partition key karafka#190
2 parents eb1a1fa + 38efe71 commit 58edbcb

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Diff for: lib/rdkafka/bindings.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@ def self.partitioner(str, partition_count, partitioner_name = "consistent_random
256256
# Return RD_KAFKA_PARTITION_UA(unassigned partition) when partition count is nil/zero.
257257
return -1 unless partition_count&.nonzero?
258258

259-
str_ptr = FFI::MemoryPointer.from_string(str)
259+
str_ptr = str.empty? ? FFI::MemoryPointer::NULL : FFI::MemoryPointer.from_string(str)
260260
method_name = PARTITIONERS.fetch(partitioner_name) do
261261
raise Rdkafka::Config::ConfigError.new("Unknown partitioner: #{partitioner_name}")
262262
end
263-
public_send(method_name, nil, str_ptr, str.size, partition_count, nil, nil)
263+
public_send(method_name, nil, str_ptr, str.size > 0 ? str.size : 1, partition_count, nil, nil)
264264
end
265265

266266
# Create Topics

Diff for: spec/rdkafka/producer_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,28 @@ def call(_, handle)
251251
expect(messages[2].key).to eq key
252252
end
253253

254+
it "should produce a message with empty string without crashing" do
255+
messages = [{key: 'a', partition_key: ''}]
256+
257+
messages = messages.map do |m|
258+
handle = producer.produce(
259+
topic: "partitioner_test_topic",
260+
payload: "payload partition",
261+
key: m[:key],
262+
partition_key: m[:partition_key]
263+
)
264+
report = handle.wait(max_wait_timeout: 5)
265+
266+
wait_for_message(
267+
topic: "partitioner_test_topic",
268+
delivery_report: report,
269+
)
270+
end
271+
272+
expect(messages[0].partition).to eq 0
273+
expect(messages[0].key).to eq 'a'
274+
end
275+
254276
it "should produce a message with utf-8 encoding" do
255277
handle = producer.produce(
256278
topic: "produce_test_topic",

0 commit comments

Comments
 (0)