Skip to content

Commit aff7546

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#27036: test: Remove last uses of snprintf and simplify
b803229 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 bitcoin/bitcoin#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 bitcoin/bitcoin#27014 ACKs for top commit: Sjors: tACK b803229, fixes #27014. Tree-SHA512: c903977e654711929decafe8887d0de13b38a340d7082875acc5d41950d834dcfde074e9cabecaf5f9a760f62c34322297b4b156af29761650ef5803b1a54b59
2 parents d8f9826 + b803229 commit aff7546

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

src/test/dbwrapper_tests.cpp

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <dbwrapper.h>
66
#include <test/util/setup_common.h>
77
#include <uint256.h>
8+
#include <util/string.h>
89

910
#include <memory>
1011

@@ -324,12 +325,6 @@ struct StringContentsSerializer {
324325
StringContentsSerializer() = default;
325326
explicit StringContentsSerializer(const std::string& inp) : str(inp) {}
326327

327-
StringContentsSerializer& operator+=(const std::string& s) {
328-
str += s;
329-
return *this;
330-
}
331-
StringContentsSerializer& operator+=(const StringContentsSerializer& s) { return *this += s.str; }
332-
333328
template<typename Stream>
334329
void Serialize(Stream& s) const
335330
{
@@ -343,44 +338,34 @@ struct StringContentsSerializer {
343338
{
344339
str.clear();
345340
uint8_t c{0};
346-
while (true) {
347-
try {
348-
s >> c;
349-
str.push_back(c);
350-
} catch (const std::ios_base::failure&) {
351-
break;
352-
}
341+
while (!s.eof()) {
342+
s >> c;
343+
str.push_back(c);
353344
}
354345
}
355346
};
356347

357348
BOOST_AUTO_TEST_CASE(iterator_string_ordering)
358349
{
359-
char buf[10];
360-
361350
fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
362351
CDBWrapper dbw(ph, (1 << 20), true, false, false);
363-
for (int x=0x00; x<10; ++x) {
364-
for (int y = 0; y < 10; y++) {
365-
snprintf(buf, sizeof(buf), "%d", x);
366-
StringContentsSerializer key(buf);
367-
for (int z = 0; z < y; z++)
352+
for (int x = 0; x < 10; ++x) {
353+
for (int y = 0; y < 10; ++y) {
354+
std::string key{ToString(x)};
355+
for (int z = 0; z < y; ++z)
368356
key += key;
369357
uint32_t value = x*x;
370-
BOOST_CHECK(dbw.Write(key, value));
358+
BOOST_CHECK(dbw.Write(StringContentsSerializer{key}, value));
371359
}
372360
}
373361

374362
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
375363
for (const int seek_start : {0, 5}) {
376-
snprintf(buf, sizeof(buf), "%d", seek_start);
377-
StringContentsSerializer seek_key(buf);
378-
it->Seek(seek_key);
379-
for (unsigned int x=seek_start; x<10; ++x) {
380-
for (int y = 0; y < 10; y++) {
381-
snprintf(buf, sizeof(buf), "%d", x);
382-
std::string exp_key(buf);
383-
for (int z = 0; z < y; z++)
364+
it->Seek(StringContentsSerializer{ToString(seek_start)});
365+
for (unsigned int x = seek_start; x < 10; ++x) {
366+
for (int y = 0; y < 10; ++y) {
367+
std::string exp_key{ToString(x)};
368+
for (int z = 0; z < y; ++z)
384369
exp_key += exp_key;
385370
StringContentsSerializer key;
386371
uint32_t value;

test/lint/lint-locale-dependence.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
KNOWN_VIOLATIONS = [
4747
"src/dbwrapper.cpp:.*vsnprintf",
48-
"src/test/dbwrapper_tests.cpp:.*snprintf",
4948
"src/test/fuzz/locale.cpp:.*setlocale",
5049
"src/test/fuzz/string.cpp:.*strtol",
5150
"src/test/fuzz/string.cpp:.*strtoul",

0 commit comments

Comments
 (0)