Skip to content

Commit

Permalink
Use static_cast<unsigned char> on DecodeBase64 to prevent SEGV on neg…
Browse files Browse the repository at this point in the history
…ative values (jbeder#1051)
  • Loading branch information
Ngiong authored Oct 20, 2021
1 parent 1713859 commit 2f89975
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ std::vector<unsigned char> DecodeBase64(const std::string &input) {
// skip newlines
continue;
}
unsigned char d = decoding[static_cast<unsigned>(input[i])];
unsigned char d = decoding[static_cast<unsigned char>(input[i])];
if (d == 255)
return ret_type();

Expand Down
14 changes: 14 additions & 0 deletions test/binary_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "gtest/gtest.h"
#include <yaml-cpp/binary.h>

TEST(BinaryTest, DecodingSimple) {
std::string input{90, 71, 86, 104, 90, 71, 74, 108, 90, 87, 89, 61};
const std::vector<unsigned char> &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<unsigned char> &result = YAML::DecodeBase64(input);
EXPECT_TRUE(result.empty());
}

0 comments on commit 2f89975

Please sign in to comment.