mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-06 01:41:01 +02:00
Remove use of snprintf and simplify
One test case uses snprintf to convert an int to a string. Change it to use ToString (which uses a locale-independent version of std::to_string). Also remove unnecessary parts of StringContentsSerializer.
This commit is contained in:
parent
aaa55971f6
commit
b8032293e6
@ -5,6 +5,7 @@
|
|||||||
#include <dbwrapper.h>
|
#include <dbwrapper.h>
|
||||||
#include <test/util/setup_common.h>
|
#include <test/util/setup_common.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
|
#include <util/string.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@ -324,12 +325,6 @@ struct StringContentsSerializer {
|
|||||||
StringContentsSerializer() = default;
|
StringContentsSerializer() = default;
|
||||||
explicit StringContentsSerializer(const std::string& inp) : str(inp) {}
|
explicit StringContentsSerializer(const std::string& inp) : str(inp) {}
|
||||||
|
|
||||||
StringContentsSerializer& operator+=(const std::string& s) {
|
|
||||||
str += s;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
StringContentsSerializer& operator+=(const StringContentsSerializer& s) { return *this += s.str; }
|
|
||||||
|
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void Serialize(Stream& s) const
|
void Serialize(Stream& s) const
|
||||||
{
|
{
|
||||||
@ -343,44 +338,34 @@ struct StringContentsSerializer {
|
|||||||
{
|
{
|
||||||
str.clear();
|
str.clear();
|
||||||
uint8_t c{0};
|
uint8_t c{0};
|
||||||
while (true) {
|
while (!s.eof()) {
|
||||||
try {
|
s >> c;
|
||||||
s >> c;
|
str.push_back(c);
|
||||||
str.push_back(c);
|
|
||||||
} catch (const std::ios_base::failure&) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(iterator_string_ordering)
|
BOOST_AUTO_TEST_CASE(iterator_string_ordering)
|
||||||
{
|
{
|
||||||
char buf[10];
|
|
||||||
|
|
||||||
fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
|
fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
|
||||||
CDBWrapper dbw(ph, (1 << 20), true, false, false);
|
CDBWrapper dbw(ph, (1 << 20), true, false, false);
|
||||||
for (int x=0x00; x<10; ++x) {
|
for (int x = 0; x < 10; ++x) {
|
||||||
for (int y = 0; y < 10; y++) {
|
for (int y = 0; y < 10; ++y) {
|
||||||
snprintf(buf, sizeof(buf), "%d", x);
|
std::string key{ToString(x)};
|
||||||
StringContentsSerializer key(buf);
|
for (int z = 0; z < y; ++z)
|
||||||
for (int z = 0; z < y; z++)
|
|
||||||
key += key;
|
key += key;
|
||||||
uint32_t value = x*x;
|
uint32_t value = x*x;
|
||||||
BOOST_CHECK(dbw.Write(key, value));
|
BOOST_CHECK(dbw.Write(StringContentsSerializer{key}, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
|
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
|
||||||
for (const int seek_start : {0, 5}) {
|
for (const int seek_start : {0, 5}) {
|
||||||
snprintf(buf, sizeof(buf), "%d", seek_start);
|
it->Seek(StringContentsSerializer{ToString(seek_start)});
|
||||||
StringContentsSerializer seek_key(buf);
|
for (unsigned int x = seek_start; x < 10; ++x) {
|
||||||
it->Seek(seek_key);
|
for (int y = 0; y < 10; ++y) {
|
||||||
for (unsigned int x=seek_start; x<10; ++x) {
|
std::string exp_key{ToString(x)};
|
||||||
for (int y = 0; y < 10; y++) {
|
for (int z = 0; z < y; ++z)
|
||||||
snprintf(buf, sizeof(buf), "%d", x);
|
|
||||||
std::string exp_key(buf);
|
|
||||||
for (int z = 0; z < y; z++)
|
|
||||||
exp_key += exp_key;
|
exp_key += exp_key;
|
||||||
StringContentsSerializer key;
|
StringContentsSerializer key;
|
||||||
uint32_t value;
|
uint32_t value;
|
||||||
|
@ -45,7 +45,6 @@ from subprocess import check_output, CalledProcessError
|
|||||||
|
|
||||||
KNOWN_VIOLATIONS = [
|
KNOWN_VIOLATIONS = [
|
||||||
"src/dbwrapper.cpp:.*vsnprintf",
|
"src/dbwrapper.cpp:.*vsnprintf",
|
||||||
"src/test/dbwrapper_tests.cpp:.*snprintf",
|
|
||||||
"src/test/fuzz/locale.cpp:.*setlocale",
|
"src/test/fuzz/locale.cpp:.*setlocale",
|
||||||
"src/test/fuzz/string.cpp:.*strtol",
|
"src/test/fuzz/string.cpp:.*strtol",
|
||||||
"src/test/fuzz/string.cpp:.*strtoul",
|
"src/test/fuzz/string.cpp:.*strtoul",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user