Commit e1754372 authored by Clemens Backes's avatar Clemens Backes Committed by V8 LUCI CQ

[wasm] Fix types used in Result and Decoder::toResult

The type stored in {Result} should not always be the same as derived by
the compiler for the argument to {Decoder::toResult}. If we pass in a
temporary, we most often want it to be stored by value, not by
reference.
This CL enforces this; if requirements change in the future, we can
remove the static assertions and think about how to protect against
accidental UAF when referencing a temporary value.

R=jkummerow@chromium.org
CC=mliedtke@chromium.org

Change-Id: Ia0449e6ed7342319799479b200af35660fccc6d7
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3792115Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#82115}
parent 5f405c7d
......@@ -338,13 +338,13 @@ class Decoder {
}
// Converts the given value to a {Result}, copying the error if necessary.
template <typename T>
Result<T> toResult(T&& val) {
template <typename T, typename R = std::decay_t<T>>
Result<R> toResult(T&& val) {
if (failed()) {
TRACE("Result error: %s\n", error_.message().c_str());
return Result<T>(error_);
return Result<R>{error_};
}
return Result<T>(std::move(val));
return Result<R>{std::forward<T>(val)};
}
// Resets the boundaries of this decoder.
......
......@@ -67,14 +67,21 @@ template <typename T>
class Result {
public:
static_assert(!std::is_same<T, WasmError>::value);
static_assert(!std::is_reference<T>::value,
"Holding a reference in a Result looks like a mistake; remove "
"this assertion if you know what you are doing");
Result() = default;
Result(const Result&) = delete;
Result& operator=(const Result<T>&) = delete;
// Allow moving.
Result(Result<T>&&) = default;
Result& operator=(Result<T>&&) = default;
// Disallow copying.
Result& operator=(const Result<T>&) = delete;
Result(const Result&) = delete;
explicit Result(T&& value) : value_(std::forward<T>(value)) {}
// Construct a Result from anything that can be used to construct a T value.
template <typename U>
explicit Result(U&& value) : value_(std::forward<U>(value)) {}
explicit Result(WasmError error) : error_(std::move(error)) {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment