Skip to content

Commit 80dc5e7

Browse files
fix(wasm): enhance data ownership management and ensure secure memory wipe on move
1 parent b794cc3 commit 80dc5e7

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

wasm/src/WasmData.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
#include "WasmData.h"
7+
#include "Defer.h"
78

89
#include <TrezorCrypto/memzero.h>
910

@@ -12,19 +13,23 @@ using namespace emscripten;
1213
namespace TW::Wasm {
1314

1415
auto 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

2226
auto 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

0 commit comments

Comments
 (0)