aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-04-05 23:20:33 +0200
committerGitHub <noreply@github.com>2022-04-05 23:20:33 +0200
commitc0ad86f6817684efc2955dae904f548ea99ad6ee (patch)
tree7b871a13f396a7159216e9334b7f8245904986dd /src
parentf89fa299141ef9c4d019097b02ce731c60938551 (diff)
parent1fa03934791189f02208e1807d822128b216f087 (diff)
Merge pull request #6366 from danpls/base64-reserve
libutil: Reserve memory when en/decoding base64
Diffstat (limited to 'src')
-rw-r--r--src/libutil/util.cc4
1 files changed, 4 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 8ab385837..bc24201d5 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -1471,6 +1471,7 @@ constexpr char base64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv
std::string base64Encode(std::string_view s)
{
std::string res;
+ res.reserve((s.size() + 2) / 3 * 4);
int data = 0, nbits = 0;
for (char c : s) {
@@ -1502,6 +1503,9 @@ std::string base64Decode(std::string_view s)
}();
std::string res;
+ // Some sequences are missing the padding consisting of up to two '='.
+ // vvv
+ res.reserve((s.size() + 2) / 4 * 3);
unsigned int d = 0, bits = 0;
for (char c : s) {