diff --git a/test/read_api_test.cpp b/test/read_api_test.cpp index df68caa..8ccaa9c 100644 --- a/test/read_api_test.cpp +++ b/test/read_api_test.cpp @@ -267,4 +267,34 @@ TEST_CASE("Write complicated, nested data format, then read it") ulog_cpp::AccessException); } +TEST_CASE("Read string values that fill the whole field or are empty") +{ + std::vector written_data; + TestWriter writer([&](const uint8_t* data, int length) { + const int prev_size = written_data.size(); + written_data.resize(written_data.size() + length); + memcpy(written_data.data() + prev_size, data, length); + }); + + // ULog strings carry no terminating zero, and PX4 logs contain empty ones (char[0]) + writer.fileHeader(ulog_cpp::FileHeader{}); + writer.messageInfo(ulog_cpp::MessageInfo{"empty", ""}); + writer.messageInfo(ulog_cpp::MessageInfo{"full", "hello"}); + writer.headerComplete(); + CHECK_EQ(writer.num_errors, 0); + + const auto data_container = + std::make_shared(ulog_cpp::DataContainer::StorageConfig::FullLog); + ulog_cpp::Reader reader{data_container}; + reader.readChunk(written_data.data(), written_data.size()); + CHECK(data_container->parsingErrors().empty()); + CHECK_FALSE(data_container->hadFatalError()); + + const auto& info = data_container->messageInfo(); + CHECK_EQ(info.at("empty").field().arrayLength(), 0); + CHECK_EQ(info.at("empty").value().as(), ""); + CHECK_EQ(info.at("full").field().arrayLength(), 5); + CHECK_EQ(info.at("full").value().as(), "hello"); +} + TEST_SUITE_END(); diff --git a/ulog_cpp/utils.hpp b/ulog_cpp/utils.hpp index f0af646..5bc7f5c 100644 --- a/ulog_cpp/utils.hpp +++ b/ulog_cpp/utils.hpp @@ -33,7 +33,7 @@ static inline ReturnType staticCastEnsureUnsignedChar(const OriginType& v) inline int strnlen(const uint8_t* ptr, int max_len) { int len = 0; - while ((*ptr != 0) && (len < max_len)) { + while ((len < max_len) && (*ptr != 0)) { ptr++; len++; }