BewareMyPower commented on code in PR #22:
URL: https://github.com/apache/pulsar-client-cpp/pull/22#discussion_r1004255282


##########
lib/KeyValueImpl.cc:
##########
@@ -0,0 +1,78 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include "KeyValueImpl.h"
+
+#include <pulsar/Schema.h>
+
+#include "SharedBuffer.h"
+
+using namespace pulsar;
+
+namespace pulsar {
+
+KeyValueImpl::KeyValueImpl(const char *data, int length, KeyValueEncodingType 
keyValueEncodingType) {
+    if (keyValueEncodingType == KeyValueEncodingType::INLINE) {
+        SharedBuffer buffer = SharedBuffer::wrap(const_cast<char *>(data), 
length);
+        auto keySize = buffer.readUnsignedInt();
+        if (keySize != INVALID_SIZE) {
+            SharedBuffer keyContent = buffer.slice(0, keySize);
+            key_ = std::string(keyContent.data(), keySize);
+            buffer.consume(keySize);
+        }
+        auto valueSize = buffer.readUnsignedInt();
+        if (valueSize != INVALID_SIZE) {
+            valueBuffer_ = buffer.slice(0, valueSize);
+        }
+    } else {
+        valueBuffer_ = SharedBuffer::wrap(const_cast<char *>(data), length);
+    }
+}

Review Comment:
   There is a problem that I didn't look deeper, must I just wrote a corner 
case test.
   
   ```c++
       auto schema = SchemaInfo(SchemaInfo(SchemaType::STRING, "String", ""),
                                SchemaInfo(SchemaType::STRING, "String", ""));
       Producer producer;
       client.createProducer(topic, ProducerConfiguration().setSchema(schema), 
producer);
       producer.send(MessageBuilder().setContent(KeyValue("", 
"value")).build());
   
       ConsumerConfiguration conf;
       conf.setSubscriptionInitialPosition(InitialPositionEarliest);
       conf.setSchema(schema);
       Consumer consumer;
       client.subscribe(topic, "sub", conf, consumer);
   
       Message msg;
       auto result = consumer.receive(msg, 3000);
       if (result == ResultOk) {
           const auto kv = msg.getKeyValueData();
           LOG_INFO("Received KeyValue(key=" << kv.getKey() << ", value=" << 
kv.getValueAsString() << ") from "
                                             << msg.getMessageId());
        }
   ```
   
   Segfault happened. I think the key should be allowed be empty.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to