File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44//
55
66#include " WasmData.h"
7+ #include " Defer.h"
78
89#include < TrezorCrypto/memzero.h>
910
@@ -12,19 +13,23 @@ using namespace emscripten;
1213namespace TW ::Wasm {
1314
1415auto DataToVal (Data&& data) -> val {
15- auto view = val (typed_memory_view (data.size (), data.data ()));
16- auto jsArray = val::global (" Uint8Array" ).new_ (data.size ());
16+ Data local = std::move (data); // take ownership; caller's object is now empty
17+ defer {
18+ if (!local.empty ()) { memzero (local.data (), local.size ()); }
19+ };
20+ auto view = val (typed_memory_view (local.size (), local.data ()));
21+ auto jsArray = val::global (" Uint8Array" ).new_ (local.size ());
1722 jsArray.call <void >(" set" , view);
18- if (!data.empty ()) { memzero (data.data (), data.size ()); }
1923 return jsArray;
2024}
2125
2226auto TWDataToVal (TWData* _Nonnull data) -> val {
27+ defer {
28+ TWDataDelete (data);
29+ };
2330 auto * v = reinterpret_cast <Data*>(data);
24- auto result = DataToVal (std::move (*v));
25- // v is now moved-from (empty); TWDataDelete skips memzero but frees the object.
26- TWDataDelete (data);
27- return result;
31+ Data local = std::move (*v); // *v is now genuinely empty; local owns the buffer
32+ return DataToVal (std::move (local));
2833}
2934
3035} // namespace TW::Wasm
You can’t perform that action at this time.
0 commit comments