@@ -63,13 +63,13 @@ secret::SecretStorage::SecretStorage(KeyringType type) : m_type{ type } {
6363 }
6464}
6565
66- secret::SecretStorage::~SecretStorage () = default ;
66+ secret::SecretStorage::~SecretStorage () = default ; // NOLINT(performance-trivially-destructible)
6767
6868secret::SecretStorage::SecretStorage (SecretStorage&& other) noexcept
6969 : m_type{ other.m_type },
7070 m_ring_id{ other.m_ring_id } { }
7171
72- [[nodiscard]] helper::expected<std::string , std::string> secret::SecretStorage::load (const std::string& key) const {
72+ [[nodiscard]] helper::expected<secret::Buffer , std::string> secret::SecretStorage::load (const std::string& key) const {
7373
7474 auto key_id = get_id_from_name (m_ring_id, key);
7575
@@ -90,19 +90,17 @@ secret::SecretStorage::SecretStorage(SecretStorage&& other) noexcept
9090 };
9191 }
9292
93- auto result_string = std::string{
94- static_cast <char *>(buffer),
95- static_cast <char *>(buffer) + result // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
96- };
93+
94+ auto result_buffer =
95+ Buffer{ reinterpret_cast <std::byte*>(buffer), // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
96+ static_cast <std::size_t >(result) };
97+
9798 free (buffer); // NOLINT(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
98- return result_string ;
99+ return result_buffer ;
99100}
100101
101- [[nodiscard]] std::optional<std::string> secret::SecretStorage::store (
102- const std::string& key, // NOLINT(bugprone-easily-swappable-parameters)
103- const std::string& value,
104- bool update
105- ) const {
102+ [[nodiscard]] std::optional<std::string>
103+ secret::SecretStorage::store (const std::string& key, const Buffer& value, bool update) const {
106104
107105 auto key_id = get_id_from_name (m_ring_id, key);
108106
@@ -113,7 +111,7 @@ secret::SecretStorage::SecretStorage(SecretStorage&& other) noexcept
113111
114112 auto full_key = get_key_name (key);
115113
116- auto new_id = add_key (constants::key_type_user, full_key.c_str (), value.c_str (), value.size (), m_ring_id);
114+ auto new_id = add_key (constants::key_type_user, full_key.c_str (), value.data (), value.size (), m_ring_id);
117115
118116 if (new_id < 0 ) {
119117 return fmt::format (" Error while storing a key, can't add the key: {}" , strerror (errno));
@@ -235,7 +233,7 @@ secret::SecretStorage::SecretStorage(SecretStorage&& other) noexcept
235233}
236234
237235
238- [[nodiscard]] helper::expected<std::string , std::string> secret::SecretStorage::load (const std::string& key) const {
236+ [[nodiscard]] helper::expected<secret::Buffer , std::string> secret::SecretStorage::load (const std::string& key) const {
239237
240238 auto maybe_key_handle = get_handle_from_name (m_type, m_phProvider, key);
241239
@@ -281,15 +279,18 @@ secret::SecretStorage::SecretStorage(SecretStorage&& other) noexcept
281279
282280 NCryptFreeObject (key_handle);
283281
284- auto result_string = std::string{ reinterpret_cast <char *>(buffer), reinterpret_cast <char *>(buffer) + bytes_needed };
282+ auto result_buffer =
283+ Buffer{ reinterpret_cast <std::byte*>(buffer), // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
284+ static_cast <std::size_t >(bytes_needed) };
285+
285286
286287 delete[] buffer;
287288
288- return result_string ;
289+ return result_buffer ;
289290}
290291
291292[[nodiscard]] std::optional<std::string>
292- secret::SecretStorage::store (const std::string& key, const std::string & value, bool update) const {
293+ secret::SecretStorage::store (const std::string& key, const Buffer & value, bool update) const {
293294
294295 NCRYPT_KEY_HANDLE key_handle{};
295296
@@ -317,7 +318,7 @@ secret::SecretStorage::store(const std::string& key, const std::string& value, b
317318
318319 PBYTE buffer = new BYTE[value.size ()];
319320
320- std::memcpy (buffer, value.c_str (), value.size ());
321+ std::memcpy (buffer, value.data (), value.size ());
321322
322323 auto result2 =
323324 NCryptSetProperty (key_handle, constants::property_name, buffer, static_cast <DWORD>(value.size ()), flags2);
@@ -403,7 +404,7 @@ secret::SecretStorage::SecretStorage(SecretStorage&& other) noexcept
403404 m_file_path{ other.m_file_path } { }
404405
405406
406- [[nodiscard]] helper::expected<std::string , std::string> secret::SecretStorage::load (const std::string& key) const {
407+ [[nodiscard]] helper::expected<secret::Buffer , std::string> secret::SecretStorage::load (const std::string& key) const {
407408
408409 auto maybe_json_value = get_json_from_file (m_file_path);
409410
@@ -421,11 +422,13 @@ secret::SecretStorage::SecretStorage(SecretStorage&& other) noexcept
421422
422423 json_value.at (key).get_to (result);
423424
424- return result;
425+ auto result_buffer = Buffer{ result };
426+
427+ return result_buffer;
425428}
426429
427430[[nodiscard]] std::optional<std::string>
428- secret::SecretStorage::store (const std::string& key, const std::string & value, bool update) const {
431+ secret::SecretStorage::store (const std::string& key, const Buffer & value, bool update) const {
429432
430433 auto maybe_json_value = get_json_from_file (m_file_path);
431434
@@ -440,7 +443,7 @@ secret::SecretStorage::store(const std::string& key, const std::string& value, b
440443 }
441444
442445
443- json_value.at (key) = value;
446+ json_value.at (key) = value. as_string () ;
444447
445448 return json::try_write_json_to_file (m_file_path, json_value, true );
446449}
0 commit comments