From 94c12d21305fe337acf3637f79dae14fe625a424 Mon Sep 17 00:00:00 2001 From: Simon Gene Gottlieb Date: Tue, 11 Aug 2026 16:11:33 +0200 Subject: [PATCH] fix: UB when calling empty YAML::Binary::data() --- include/yaml-cpp/binary.h | 2 +- test/binary_test.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/yaml-cpp/binary.h b/include/yaml-cpp/binary.h index 1050dae98..4dec1b4b7 100644 --- a/include/yaml-cpp/binary.h +++ b/include/yaml-cpp/binary.h @@ -30,7 +30,7 @@ class YAML_CPP_API Binary { bool owned() const { return !m_unownedData; } std::size_t size() const { return owned() ? m_data.size() : m_unownedSize; } const unsigned char *data() const { - return owned() ? &m_data[0] : m_unownedData; + return owned() ? m_data.data() : m_unownedData; } void swap(std::vector &rhs) { diff --git a/test/binary_test.cpp b/test/binary_test.cpp index 9d7f2712b..30e0e46b0 100644 --- a/test/binary_test.cpp +++ b/test/binary_test.cpp @@ -18,3 +18,9 @@ TEST(BinaryTest, DecodingTooShort) { const std::vector &result = YAML::DecodeBase64(input); EXPECT_TRUE(result.empty()); } + +TEST(BinaryTest, EmptyBinary) { + YAML::Binary b; + EXPECT_TRUE(b.size() == 0); + EXPECT_TRUE(b.data() == b.data()); // caused UB in the past +}