Skip to content

Commit 2f89975

Browse files
authored
Use static_cast<unsigned char> on DecodeBase64 to prevent SEGV on negative values (#1051)
1 parent 1713859 commit 2f89975

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/binary.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ std::vector<unsigned char> DecodeBase64(const std::string &input) {
7979
// skip newlines
8080
continue;
8181
}
82-
unsigned char d = decoding[static_cast<unsigned>(input[i])];
82+
unsigned char d = decoding[static_cast<unsigned char>(input[i])];
8383
if (d == 255)
8484
return ret_type();
8585

test/binary_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "gtest/gtest.h"
2+
#include <yaml-cpp/binary.h>
3+
4+
TEST(BinaryTest, DecodingSimple) {
5+
std::string input{90, 71, 86, 104, 90, 71, 74, 108, 90, 87, 89, 61};
6+
const std::vector<unsigned char> &result = YAML::DecodeBase64(input);
7+
EXPECT_EQ(std::string(result.begin(), result.end()), "deadbeef");
8+
}
9+
10+
TEST(BinaryTest, DecodingNoCrashOnNegative) {
11+
std::string input{-58, -1, -99, 109};
12+
const std::vector<unsigned char> &result = YAML::DecodeBase64(input);
13+
EXPECT_TRUE(result.empty());
14+
}

0 commit comments

Comments
 (0)