mirror of
https://github.com/NixOS/nix
synced 2025-07-07 01:51:47 +02:00
fix(libstore-tests): remove use-after-free bug for StringSource
Unfortunately `StringSource` class is very easy was very easy to misuse
because the ctor took a plain `std::string_view` which has a bad habit
of being implicitly convertible from an rvalue `std::string`. This lead
to unintentional use-after-free bugs.
This patch makes `StringSource` much harder to misuse by disabling the ctor
from a `std::string &&` (but `const std::string &` is ok).
Fix affected tests from libstore-tests.
Reformat those tests with clangd's range formatting since the diff is tiny
and it seems appropriate.
(cherry picked from commit 5bc8957c73
)
This commit is contained in:
parent
e0c8b0fc4f
commit
e863e6ab83
3 changed files with 16 additions and 22 deletions
|
@ -2,6 +2,7 @@
|
|||
///@file
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "types.hh"
|
||||
#include "util.hh"
|
||||
|
@ -192,7 +193,14 @@ struct StringSource : Source
|
|||
{
|
||||
std::string_view s;
|
||||
size_t pos;
|
||||
|
||||
// NOTE: Prevent unintentional dangling views when an implicit conversion
|
||||
// from std::string -> std::string_view occurs when the string is passed
|
||||
// by rvalue.
|
||||
StringSource(std::string &&) = delete;
|
||||
StringSource(std::string_view s) : s(s), pos(0) { }
|
||||
StringSource(const std::string& str): StringSource(std::string_view(str)) {}
|
||||
|
||||
size_t read(char * data, size_t len) override;
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue