aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend_stmt.cpp
diff options
context:
space:
mode:
authorbobsayshilol <bobsayshilol@live.co.uk>2024-10-27 18:35:14 +0000
committerbobsayshilol <bobsayshilol@live.co.uk>2024-10-27 22:02:34 +0000
commite67692b0661ab23837079b2ed7340ff85ca88cd6 (patch)
tree3f7d84fd3b0ba464717299fb59dee79e665728e0 /src/llvm_backend_stmt.cpp
parent771d308d643f9e3d59b2451fe18c5bdd845aba1e (diff)
Avoid member access through nullptr in debug
If |result_count| is 0 then |results| will be a nullptr and hence the access |results->Tuple| is undefined behaviour. There's already an early return in the 0 branch so move that to be the first thing so that we can guarantee that it's not a nullptr. Note that technically we take the address of the result so it's not actually dereferencing it, however UBSan doesn't care about that.
Diffstat (limited to 'src/llvm_backend_stmt.cpp')
-rw-r--r--src/llvm_backend_stmt.cpp19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp
index 288e7206a..06d66ac80 100644
--- a/src/llvm_backend_stmt.cpp
+++ b/src/llvm_backend_stmt.cpp
@@ -2018,14 +2018,7 @@ gb_internal void lb_build_return_stmt_internal(lbProcedure *p, lbValue res) {
gb_internal void lb_build_return_stmt(lbProcedure *p, Slice<Ast *> const &return_results) {
lb_ensure_abi_function_type(p->module, p);
- lbValue res = {};
-
- TypeTuple *tuple = &p->type->Proc.results->Tuple;
isize return_count = p->type->Proc.result_count;
- isize res_count = return_results.count;
-
- lbFunctionType *ft = lb_get_function_type(p->module, p->type);
- bool return_by_pointer = ft->ret.kind == lbArg_Indirect;
if (return_count == 0) {
// No return values
@@ -2038,7 +2031,17 @@ gb_internal void lb_build_return_stmt(lbProcedure *p, Slice<Ast *> const &return
LLVMBuildRetVoid(p->builder);
}
return;
- } else if (return_count == 1) {
+ }
+
+ lbValue res = {};
+
+ TypeTuple *tuple = &p->type->Proc.results->Tuple;
+ isize res_count = return_results.count;
+
+ lbFunctionType *ft = lb_get_function_type(p->module, p->type);
+ bool return_by_pointer = ft->ret.kind == lbArg_Indirect;
+
+ if (return_count == 1) {
Entity *e = tuple->variables[0];
if (res_count == 0) {
rw_mutex_shared_lock(&p->module->values_mutex);