From 2f8997565b082eab700aa20025561d20b1a47f61 Mon Sep 17 00:00:00 2001 From: Robert Sebastian Herlim Date: Thu, 21 Oct 2021 00:18:08 +0900 Subject: [PATCH] Use static_cast on DecodeBase64 to prevent SEGV on negative values (#1051) --- src/binary.cpp | 2 +- test/binary_test.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/binary_test.cpp diff --git a/src/binary.cpp b/src/binary.cpp index 4311a2d..5949dd3 100644 --- a/src/binary.cpp +++ b/src/binary.cpp @@ -79,7 +79,7 @@ std::vector DecodeBase64(const std::string &input) { // skip newlines continue; } - unsigned char d = decoding[static_cast(input[i])]; + unsigned char d = decoding[static_cast(input[i])]; if (d == 255) return ret_type(); diff --git a/test/binary_test.cpp b/test/binary_test.cpp new file mode 100644 index 0000000..7b17823 --- /dev/null +++ b/test/binary_test.cpp @@ -0,0 +1,14 @@ +#include "gtest/gtest.h" +#include + +TEST(BinaryTest, DecodingSimple) { + std::string input{90, 71, 86, 104, 90, 71, 74, 108, 90, 87, 89, 61}; + const std::vector &result = YAML::DecodeBase64(input); + EXPECT_EQ(std::string(result.begin(), result.end()), "deadbeef"); +} + +TEST(BinaryTest, DecodingNoCrashOnNegative) { + std::string input{-58, -1, -99, 109}; + const std::vector &result = YAML::DecodeBase64(input); + EXPECT_TRUE(result.empty()); +}