From 9fbbea23d39434beac8b7c080742ad91ada13441 Mon Sep 17 00:00:00 2001 From: invertego Date: Mon, 5 Sep 2022 06:12:08 -0700 Subject: [PATCH] nall: always pass by reference to make_string() Many specializations of stringify store a reference to the wrapped value. Therefore, passing arguments to make_string() by value can be disastrous if the compiler does not perform copy elision and stringify stores a reference to a variable that goes out of scope when make_string() returns. Cherry-picked from ares commit 3d826f5b266027529f0c12211c744a23bbe25a56. --- nall/string/cast.hpp | 44 +++----------------------------------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/nall/string/cast.hpp b/nall/string/cast.hpp index da915eed..1f456ec6 100755 --- a/nall/string/cast.hpp +++ b/nall/string/cast.hpp @@ -169,16 +169,6 @@ template struct stringify> { //arrays template<> struct stringify> { - stringify(vector source) { - _text.resize(source.size()); - memory::copy(_text.data(), source.data(), source.size()); - } - auto data() const -> const char* { return _text.data(); } - auto size() const -> uint { return _text.size(); } - vector _text; -}; - -template<> struct stringify&> { stringify(const vector& source) { _text.resize(source.size()); memory::copy(_text.data(), source.data(), source.size()); @@ -191,7 +181,7 @@ template<> struct stringify&> { //char arrays template<> struct stringify { - stringify(char* source) : _data(source ? source : "") {} + stringify(const char* source) : _data(source ? source : "") {} auto data() const -> const char* { return _data; } auto size() const -> uint { return strlen(_data); } const char* _data; @@ -213,13 +203,6 @@ template<> struct stringify { const string& _text; }; -template<> struct stringify { - stringify(const string& source) : _text(source) {} - auto data() const -> const char* { return _text.data(); } - auto size() const -> uint { return _text.size(); } - const string& _text; -}; - template<> struct stringify { stringify(const string_view& source) : _view(source) {} auto data() const -> const char* { return _view.data(); } @@ -227,13 +210,6 @@ template<> struct stringify { const string_view& _view; }; -template<> struct stringify { - stringify(const string_view& source) : _view(source) {} - auto data() const -> const char* { return _view.data(); } - auto size() const -> uint { return _view.size(); } - const string_view& _view; -}; - template<> struct stringify> { stringify(const array_view& source) : _view(source) {} auto data() const -> const char* { return _view.data(); } @@ -241,13 +217,6 @@ template<> struct stringify> { const array_view& _view; }; -template<> struct stringify&> { - stringify(const array_view& source) : _view(source) {} - auto data() const -> const char* { return _view.data(); } - auto size() const -> uint { return _view.size(); } - const array_view& _view; -}; - template<> struct stringify { stringify(const string_pascal& source) : _text(source) {} auto data() const -> const char* { return _text.data(); } @@ -255,13 +224,6 @@ template<> struct stringify { const string_pascal& _text; }; -template<> struct stringify { - stringify(const string_pascal& source) : _text(source) {} - auto data() const -> const char* { return _text.data(); } - auto size() const -> uint { return _text.size(); } - const string_pascal& _text; -}; - //pointers //note: T = char* is matched by stringify @@ -281,8 +243,8 @@ template struct stringify { // -template auto make_string(T value) -> stringify { - return stringify(forward(value)); +template auto make_string(const T& value) { + return stringify>(value); } }