aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2023-10-13 12:19:48 +0100
committergingerBill <bill@gingerbill.org>2023-10-13 12:19:48 +0100
commit23c4615f5e4aff1ff85918aa666b35864ab34089 (patch)
tree7b273f64ce1f1120fe12c24605960dcfe1942060 /src
parentec2635131b162d8152408d7633009383104659df (diff)
Disallow direct return a compound literal of a slice with elements
Diffstat (limited to 'src')
-rw-r--r--src/check_stmt.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp
index 0fe44289c..b93be734e 100644
--- a/src/check_stmt.cpp
+++ b/src/check_stmt.cpp
@@ -2291,6 +2291,23 @@ gb_internal void check_return_stmt(CheckerContext *ctx, Ast *node) {
}
}
}
+
+ for (Operand &o : operands) {
+ if (o.expr == nullptr) {
+ continue;
+ }
+ if (o.expr->kind != Ast_CompoundLit || !is_type_slice(o.type)) {
+ continue;
+ }
+ ast_node(cl, CompoundLit, o.expr);
+ if (cl->elems.count == 0) {
+ continue;
+ }
+ gbString s = type_to_string(o.type);
+ error(o.expr, "It is unsafe to return a compound literal of a slice ('%s') with elements from a procedure, as the contents of the slice uses the current stack frame's memory", s);
+ gb_string_free(s);
+ }
+
}
gb_internal void check_for_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {