Merge bitcoin/bitcoin#27036: test: Remove last uses of snprintf and simplify

b8032293e6 Remove use of snprintf and simplify (John Moffett)

Pull request description:

  These are the only remaining uses of `snprintf` in our project, and they can cause unexpected issues -- for example, see https://github.com/bitcoin/bitcoin/issues/27014. Change them to use our `ToString` (which uses a locale-independent version of `std::to_string`) to convert an `int` to `std::string`. Also remove resulting unused parts of `StringContentsSerializer`.

  Closes https://github.com/bitcoin/bitcoin/issues/27014

ACKs for top commit:
  Sjors:
    tACK b8032293e6, fixes #27014.

Tree-SHA512: c903977e654711929decafe8887d0de13b38a340d7082875acc5d41950d834dcfde074e9cabecaf5f9a760f62c34322297b4b156af29761650ef5803b1a54b59
This commit is contained in:
MarcoFalke 2023-02-06 10:32:39 +01:00
commit aff75463e2
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 14 additions and 30 deletions

View file

@ -5,6 +5,7 @@
#include <dbwrapper.h>
#include <test/util/setup_common.h>
#include <uint256.h>
#include <util/string.h>
#include <memory>
@ -324,12 +325,6 @@ struct StringContentsSerializer {
StringContentsSerializer() = default;
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>
void Serialize(Stream& s) const
{
@ -343,44 +338,34 @@ struct StringContentsSerializer {
{
str.clear();
uint8_t c{0};
while (true) {
try {
s >> c;
str.push_back(c);
} catch (const std::ios_base::failure&) {
break;
}
while (!s.eof()) {
s >> c;
str.push_back(c);
}
}
};
BOOST_AUTO_TEST_CASE(iterator_string_ordering)
{
char buf[10];
fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
CDBWrapper dbw(ph, (1 << 20), true, false, false);
for (int x=0x00; x<10; ++x) {
for (int y = 0; y < 10; y++) {
snprintf(buf, sizeof(buf), "%d", x);
StringContentsSerializer key(buf);
for (int z = 0; z < y; z++)
for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
std::string key{ToString(x)};
for (int z = 0; z < y; ++z)
key += key;
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());
for (const int seek_start : {0, 5}) {
snprintf(buf, sizeof(buf), "%d", seek_start);
StringContentsSerializer seek_key(buf);
it->Seek(seek_key);
for (unsigned int x=seek_start; x<10; ++x) {
for (int y = 0; y < 10; y++) {
snprintf(buf, sizeof(buf), "%d", x);
std::string exp_key(buf);
for (int z = 0; z < y; z++)
it->Seek(StringContentsSerializer{ToString(seek_start)});
for (unsigned int x = seek_start; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
std::string exp_key{ToString(x)};
for (int z = 0; z < y; ++z)
exp_key += exp_key;
StringContentsSerializer key;
uint32_t value;

View file

@ -45,7 +45,6 @@ from subprocess import check_output, CalledProcessError
KNOWN_VIOLATIONS = [
"src/dbwrapper.cpp:.*vsnprintf",
"src/test/dbwrapper_tests.cpp:.*snprintf",
"src/test/fuzz/locale.cpp:.*setlocale",
"src/test/fuzz/string.cpp:.*strtol",
"src/test/fuzz/string.cpp:.*strtoul",