From eb5525e282b0a7d2dc9eaaefe7a4b918040485be Mon Sep 17 00:00:00 2001 From: jzmaddock Date: Sun, 5 Feb 2023 12:01:41 +0000 Subject: [PATCH] Fix potentially uninitialized storage when parsing octal strings. Fixes https://github.com/boostorg/multiprecision/issues/526 --- include/boost/multiprecision/cpp_int.hpp | 3 +++ test/Jamfile.v2 | 1 + test/git_issue_526.cpp | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 test/git_issue_526.cpp diff --git a/include/boost/multiprecision/cpp_int.hpp b/include/boost/multiprecision/cpp_int.hpp index c10d327e..f0dad5a1 100644 --- a/include/boost/multiprecision/cpp_int.hpp +++ b/include/boost/multiprecision/cpp_int.hpp @@ -1854,7 +1854,10 @@ public: { // If this is the most-significant-limb, we may need to allocate an extra one for the overflow: if (limb + 1 == newsize) + { result.resize(static_cast(newsize + 1), static_cast(newsize + 1)); + result.limbs()[limb + 1] = 0; + } if (result.size() > limb + 1) { result.limbs()[limb + 1] |= val; diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 3fea2298..87556009 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -1220,6 +1220,7 @@ test-suite misc : [ run git_issue_313.cpp ] [ run git_issue_488.cpp ] [ run git_issue_506.cpp ] + [ run git_issue_526.cpp ] [ compile git_issue_98.cpp : [ check-target-builds ../config//has_float128 : TEST_FLOAT128 quadmath : ] [ check-target-builds ../config//has_gmp : TEST_GMP gmp : ] diff --git a/test/git_issue_526.cpp b/test/git_issue_526.cpp new file mode 100644 index 00000000..05d6a1df --- /dev/null +++ b/test/git_issue_526.cpp @@ -0,0 +1,18 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright 2023 John Maddock. Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include "test.hpp" + +int main() +{ + using namespace boost::multiprecision; + const std::string v("00000000000000000000000052222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222"); + cpp_int V(v); + std::string r = V.str(0, std::ios_base::oct | std::ios_base::showbase); + BOOST_CHECK_EQUAL(r, v.substr(23)); + return 0; +}