summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-01-13 15:48:09 -0500
committerTim Flynn <trflynn89@pm.me>2023-01-13 18:50:47 -0500
commitafc0e461e123489808930e5433f0d8b3f95d9256 (patch)
tree0ac45b4bbc1c55890555cddf394b2d056502e9ea /Userland/Libraries
parent3de75f6436a1e729691a40c35e8e8a5be5aa2f41 (diff)
downloadserenity-afc0e461e123489808930e5433f0d8b3f95d9256.zip
AK+Everywhere: Disallow returning a reference from a fallible expression
This will silently make a copy. Rather than masking this behavior, let's explicitly disallow it.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibAudio/LoaderError.h14
-rw-r--r--Userland/Libraries/LibJS/Runtime/Completion.h2
-rw-r--r--Userland/Libraries/LibJS/Runtime/PromiseCapability.h6
-rw-r--r--Userland/Libraries/LibVideo/DecoderError.h20
-rw-r--r--Userland/Libraries/LibVideo/PlaybackManager.cpp2
-rw-r--r--Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp14
-rw-r--r--Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp14
7 files changed, 45 insertions, 27 deletions
diff --git a/Userland/Libraries/LibAudio/LoaderError.h b/Userland/Libraries/LibAudio/LoaderError.h
index 2464d5cb64..e659050f38 100644
--- a/Userland/Libraries/LibAudio/LoaderError.h
+++ b/Userland/Libraries/LibAudio/LoaderError.h
@@ -66,10 +66,12 @@ struct LoaderError {
}
// Convenience TRY-like macro to convert an Error to a LoaderError
-#define LOADER_TRY(expression) \
- ({ \
- auto _temporary_result = (expression); \
- if (_temporary_result.is_error()) \
- return LoaderError(_temporary_result.release_error()); \
- _temporary_result.release_value(); \
+#define LOADER_TRY(expression) \
+ ({ \
+ auto _temporary_result = (expression); \
+ if (_temporary_result.is_error()) \
+ return LoaderError(_temporary_result.release_error()); \
+ static_assert(!IsLvalueReference<decltype(_temporary_result.release_value())>, \
+ "Do not return a reference from a fallible expression"); \
+ _temporary_result.release_value(); \
})
diff --git a/Userland/Libraries/LibJS/Runtime/Completion.h b/Userland/Libraries/LibJS/Runtime/Completion.h
index 0e36f3b8a0..fed974f7b6 100644
--- a/Userland/Libraries/LibJS/Runtime/Completion.h
+++ b/Userland/Libraries/LibJS/Runtime/Completion.h
@@ -25,6 +25,8 @@ namespace JS {
VERIFY(_temporary_result.error().code() == ENOMEM); \
return vm.throw_completion<JS::InternalError>(JS::ErrorType::OutOfMemory); \
} \
+ static_assert(!IsLvalueReference<decltype(_temporary_result.release_value())>, \
+ "Do not return a reference from a fallible expression"); \
_temporary_result.release_value(); \
})
diff --git a/Userland/Libraries/LibJS/Runtime/PromiseCapability.h b/Userland/Libraries/LibJS/Runtime/PromiseCapability.h
index c2cee43deb..fac53cadb3 100644
--- a/Userland/Libraries/LibJS/Runtime/PromiseCapability.h
+++ b/Userland/Libraries/LibJS/Runtime/PromiseCapability.h
@@ -53,6 +53,9 @@ private:
return (capability)->promise(); \
} \
\
+ static_assert(!IsLvalueReference<decltype(_temporary_try_or_reject_result.release_value())>, \
+ "Do not return a reference from a fallible expression"); \
+ \
/* 2. Else if value is a Completion Record, set value to value.[[Value]]. */ \
_temporary_try_or_reject_result.release_value(); \
})
@@ -76,6 +79,9 @@ private:
return Value { (capability)->promise() }; \
} \
\
+ static_assert(!IsLvalueReference<decltype(_temporary_try_or_reject_result.release_value())>, \
+ "Do not return a reference from a fallible expression"); \
+ \
/* 2. Else if value is a Completion Record, set value to value.[[Value]]. */ \
_temporary_try_or_reject_result.release_value(); \
})
diff --git a/Userland/Libraries/LibVideo/DecoderError.h b/Userland/Libraries/LibVideo/DecoderError.h
index 402b9c8c22..94a7b73993 100644
--- a/Userland/Libraries/LibVideo/DecoderError.h
+++ b/Userland/Libraries/LibVideo/DecoderError.h
@@ -77,15 +77,17 @@ private:
DeprecatedString m_description;
};
-#define DECODER_TRY(category, expression) \
- ({ \
- auto _result = ((expression)); \
- if (_result.is_error()) [[unlikely]] { \
- auto _error_string = _result.release_error().string_literal(); \
- return DecoderError::from_source_location( \
- ((category)), _error_string, SourceLocation::current()); \
- } \
- _result.release_value(); \
+#define DECODER_TRY(category, expression) \
+ ({ \
+ auto _result = ((expression)); \
+ if (_result.is_error()) [[unlikely]] { \
+ auto _error_string = _result.release_error().string_literal(); \
+ return DecoderError::from_source_location( \
+ ((category)), _error_string, SourceLocation::current()); \
+ } \
+ static_assert(!IsLvalueReference<decltype(_result.release_value())>, \
+ "Do not return a reference from a fallible expression"); \
+ _result.release_value(); \
})
#define DECODER_TRY_ALLOC(expression) DECODER_TRY(DecoderErrorCategory::Memory, expression)
diff --git a/Userland/Libraries/LibVideo/PlaybackManager.cpp b/Userland/Libraries/LibVideo/PlaybackManager.cpp
index 77d881a8fb..acb14feefb 100644
--- a/Userland/Libraries/LibVideo/PlaybackManager.cpp
+++ b/Userland/Libraries/LibVideo/PlaybackManager.cpp
@@ -273,6 +273,8 @@ bool PlaybackManager::decode_and_queue_one_sample()
m_present_timer->start(0); \
return false; \
} \
+ static_assert(!IsLvalueReference<decltype(_temporary_result.release_value())>, \
+ "Do not return a reference from a fallible expression"); \
_temporary_result.release_value(); \
})
diff --git a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index d0117b3c2c..b3a8c92401 100644
--- a/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -42,12 +42,14 @@
namespace Web::Fetch::Fetching {
-#define TRY_OR_IGNORE(expression) \
- ({ \
- auto _temporary_result = (expression); \
- if (_temporary_result.is_error()) \
- return; \
- _temporary_result.release_value(); \
+#define TRY_OR_IGNORE(expression) \
+ ({ \
+ auto _temporary_result = (expression); \
+ if (_temporary_result.is_error()) \
+ return; \
+ static_assert(!IsLvalueReference<decltype(_temporary_result.release_value())>, \
+ "Do not return a reference from a fallible expression"); \
+ _temporary_result.release_value(); \
})
// https://fetch.spec.whatwg.org/#concept-fetch
diff --git a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
index 1fa2b62263..2c414b47aa 100644
--- a/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
+++ b/Userland/Libraries/LibWeb/WebDriver/ExecuteScript.cpp
@@ -31,12 +31,14 @@
namespace Web::WebDriver {
-#define TRY_OR_JS_ERROR(expression) \
- ({ \
- auto _temporary_result = (expression); \
- if (_temporary_result.is_error()) [[unlikely]] \
- return ExecuteScriptResultType::JavaScriptError; \
- _temporary_result.release_value(); \
+#define TRY_OR_JS_ERROR(expression) \
+ ({ \
+ auto _temporary_result = (expression); \
+ if (_temporary_result.is_error()) [[unlikely]] \
+ return ExecuteScriptResultType::JavaScriptError; \
+ static_assert(!IsLvalueReference<decltype(_temporary_result.release_value())>, \
+ "Do not return a reference from a fallible expression"); \
+ _temporary_result.release_value(); \
})
static ErrorOr<JsonValue, ExecuteScriptResultType> internal_json_clone_algorithm(JS::Realm&, JS::Value, HashTable<JS::Object*>& seen);