From bd5da108857c1408abdb0fd8f7d7a4e43312bffa Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Mon, 24 Oct 2022 07:42:46 -0400 Subject: [PATCH] fixes issue #2029 when read/writing a .loki privkey file we dont rewind a llarp_buffer_t after use. this is an argument in favor of just removing that type from the code entirely. fixes by using 2 distinct locally scoped llarp_buffer_t, one for read, one for write. --- llarp/service/identity.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/llarp/service/identity.cpp b/llarp/service/identity.cpp index f54a025a5..a28bb2204 100644 --- a/llarp/service/identity.cpp +++ b/llarp/service/identity.cpp @@ -80,7 +80,6 @@ namespace llarp Clear(); std::array tmp; - llarp_buffer_t buf(tmp); // this can throw bool exists = fs::exists(fname); @@ -94,6 +93,7 @@ namespace llarp // check for file if (!exists) { + llarp_buffer_t buf{tmp}; // regen and encode RegenerateKeys(); if (!BEncode(&buf)) @@ -108,6 +108,7 @@ namespace llarp { throw std::runtime_error{fmt::format("failed to write {}: {}", fname, e.what())}; } + return; } if (not fs::is_regular_file(fname)) @@ -125,10 +126,11 @@ namespace llarp throw std::length_error{"service identity too big"}; } // (don't catch io error exceptions) - - if (!bencode_decode_dict(*this, &buf)) - throw std::length_error{"could not decode service identity"}; - + { + llarp_buffer_t buf{tmp}; + if (!bencode_decode_dict(*this, &buf)) + throw std::length_error{"could not decode service identity"}; + } auto crypto = CryptoManager::instance(); // ensure that the encryption key is set