File 4651-erts-Use-C99-style-flexible-array-for-environment-in.patch of Package erlang

From 5776ef37e4b3667e50cf9a2df154ac71f2ccb48b Mon Sep 17 00:00:00 2001
From: Frej Drejhammar <frej.drejhammar@gmail.com>
Date: Thu, 6 Apr 2023 11:23:23 +0200
Subject: [PATCH] erts: Use C99-style flexible array for environment in
 ErlFunThing

Consider the code fragment from erts/emulator/beam/erl_math.c below:

```
Eterm tmp_hp[ERL_FUN_SIZE];
ErlFunThing *funp;

funp = (ErlFunThing*)tmp_hp;
funp->thing_word = HEADER_FUN;
funp->entry.exp = NULL;
```

As `ErlFunThing` is not using a C99-style flexible array, GCC 12 is
smart enough to figure out that `sizeof(*funp)` is larger than
sizeof(tmp_hp) as ERL_FUN_SIZE subtracts the word used for the 1-sized
environment array in `ErlFunThing`. The difference in size leads to
multiple warnings in the style of: `array subscript ErlFunThing {aka
struct erl_fun_thing}[0] is partly outside array bounds of Eterm[5]`.

We can avoid the warning by converting the `env`-field in
`ErlFunThing` to a flexible array and then remove the `-1` from the
definition of `ERL_FUN_SIZE`.

The runtime is already using C99-style flexible arrays for microstate
accounting, in `erl_lock_count.h` and a comment in
`erts/emulator/beam/erl_math.c` documents a requirement for a
C99-capable compiler. Hopefully this is enough to not inadvertently
break compilation with older compilers.
---
 erts/emulator/beam/erl_fun.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/erts/emulator/beam/erl_fun.h b/erts/emulator/beam/erl_fun.h
index 43f3ec3b22..a67afaceeb 100644
--- a/erts/emulator/beam/erl_fun.h
+++ b/erts/emulator/beam/erl_fun.h
@@ -74,14 +74,15 @@ typedef struct erl_fun_thing {
 
     /* -- The following may be compound Erlang terms ---------------------- */
     Eterm creator;          /* Pid of creator process (contains node). */
-    Eterm env[1];           /* Environment (free variables). */
+    Eterm env[];            /* Environment (free variables). */
 } ErlFunThing;
 
 #define is_local_fun(FunThing) ((FunThing)->creator != am_external)
 #define is_external_fun(FunThing) ((FunThing)->creator == am_external)
 
-/* ERL_FUN_SIZE does _not_ include space for the environment */
-#define ERL_FUN_SIZE ((sizeof(ErlFunThing)/sizeof(Eterm))-1)
+/* ERL_FUN_SIZE does _not_ include space for the environment which is a
+ * C99-style flexible array */
+#define ERL_FUN_SIZE ((sizeof(ErlFunThing)/sizeof(Eterm)))
 
 ErlFunThing *erts_new_export_fun_thing(Eterm **hpp, Export *exp, int arity);
 ErlFunThing *erts_new_local_fun_thing(Process *p,
-- 
2.35.3

openSUSE Build Service is sponsored by