Skip to content

Commit

Permalink
fixed template specialization UB, improved randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
belous-dp authored and ladisgin committed Mar 14, 2024
1 parent 132a7d4 commit 91a2126
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion server/src/utils/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ namespace StringUtils {
}

template<>
int stot<>(const std::string& s) {
int stot(const std::string& s) {
return std::stoi(s);
}
template<>
Expand Down
12 changes: 12 additions & 0 deletions server/src/utils/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ namespace StringUtils {
T stot(const std::string&) {
return T();
}
template<> int stot(const std::string& s);
template<> long stot(const std::string& s);
template<> long long stot(const std::string& s);
template<> unsigned int stot(const std::string& s);
template<> unsigned long stot(const std::string& s);
template<> unsigned long long stot(const std::string& s);
template<> float stot(const std::string& s);
template<> double stot(const std::string& s);
template<> long double stot(const std::string& s);
template<> bool stot(const std::string& s);
template<> __int128 stot(const std::string& s);
template<> unsigned __int128 stot(const std::string& s);

std::string wrapQuotations(const std::string &s);
}
Expand Down
19 changes: 11 additions & 8 deletions server/test/framework/Utils_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ namespace {

TEST(StringUtils_stotInt128, simple) {
__int128 val = 42;
ASSERT_TRUE(val == StringUtils::stot<__int128>("42"));
auto res = StringUtils::stot<__int128>("42");
EXPECT_EQ(val, res);
}

TEST(StringUtils_stotInt128, simple_unsigned) {
unsigned __int128 val = 42;
ASSERT_TRUE(val == StringUtils::stot<unsigned __int128>("42"));
auto res = StringUtils::stot<unsigned __int128>("42");
EXPECT_EQ(val, res);
}

TEST(StringUtils_stotInt128, INT128_MIN) {
__int128 val = 1;
val <<= 127;
ASSERT_TRUE(val == StringUtils::stot<__int128>("-170141183460469231731687303715884105728"));
auto res = StringUtils::stot<__int128>("-170141183460469231731687303715884105728");
EXPECT_EQ(val, res);
}

TEST(ReadBytesAsValueTest, Unsigned1) {
Expand Down Expand Up @@ -323,12 +326,12 @@ namespace {

template<typename T>
void readBytesAsValueTestTemplate(T val) {
srand(42);
size_t const len = sizeof(T);
std::default_random_engine gen(42);
for (size_t tcount = 0; tcount < 5; ++tcount) {
auto add = static_cast<size_t>(rand() % 10);
auto start = static_cast<size_t>(rand() % add);
size_t const len = sizeof(T);
std::vector<char> bytes(len + add);
auto extra = static_cast<size_t>(std::uniform_int_distribution<unsigned>(1, 10)(gen));
auto start = static_cast<size_t>(std::uniform_int_distribution<unsigned>(0, extra)(gen));
std::vector<char> bytes(len + extra);
for (size_t i = 1; i <= len; ++i) {
bytes[start + i - 1] = (val >> (CHAR_BIT * (i - 1))) & ((1 << CHAR_BIT) - 1);
}
Expand Down

0 comments on commit 91a2126

Please sign in to comment.