From 0a177148e7dbac2e88ddc142f0ef10c6a4de8ec8 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Mon, 27 Feb 2017 07:43:37 +0100 Subject: [PATCH 1/2] uint256: replace sprintf with HexStr and reverse-iterator Instead of calling sprintf for every byte, format the hex bytes ourselves by help of HexStr and a reverse_iterator. --- src/uint256.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/uint256.cpp b/src/uint256.cpp index bd3d01708..c4c7b716f 100644 --- a/src/uint256.cpp +++ b/src/uint256.cpp @@ -20,10 +20,7 @@ base_blob::base_blob(const std::vector& vch) template std::string base_blob::GetHex() const { - char psz[sizeof(data) * 2 + 1]; - for (unsigned int i = 0; i < sizeof(data); i++) - sprintf(psz + i * 2, "%02x", data[sizeof(data) - i - 1]); - return std::string(psz, psz + sizeof(data) * 2); + return HexStr(std::reverse_iterator(data + sizeof(data)), std::reverse_iterator(data)); } template From 19cafc6239abd14f2b9c3d883dc7df0472cac52b Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Sun, 26 Feb 2017 21:08:26 +0100 Subject: [PATCH 2/2] test: Replace remaining sprintf with snprintf Use of `sprintf` is seen as a red flag as many of its uses are insecure. OpenBSD warns about it while compiling, and some modern platforms, e.g. [cloudlibc from cloudabi](https://github.com/NuxiNL/cloudlibc) don't even provide it anymore. Although our uses of these functions are secure, it can't hurt to replace them anyway. There are only 3 occurences left, all in the tests. --- src/test/dbwrapper_tests.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index d5d158027..22c90bd95 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -277,7 +277,7 @@ BOOST_AUTO_TEST_CASE(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++) { - sprintf(buf, "%d", x); + snprintf(buf, sizeof(buf), "%d", x); StringContentsSerializer key(buf); for (int z = 0; z < y; z++) key += key; @@ -293,12 +293,12 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering) seek_start = 0; else seek_start = 5; - sprintf(buf, "%d", seek_start); + snprintf(buf, sizeof(buf), "%d", seek_start); StringContentsSerializer seek_key(buf); it->Seek(seek_key); for (int x=seek_start; x<10; ++x) { for (int y = 0; y < 10; y++) { - sprintf(buf, "%d", x); + snprintf(buf, sizeof(buf), "%d", x); std::string exp_key(buf); for (int z = 0; z < y; z++) exp_key += exp_key;