The BPF backend was allocating an unnecessarily large string when
constructing CO-RE relocations for enum types.

gcc/ChangeLog:
        * config/bpf/core-builtins.cc (process_enum_value): Correct
        string allocation.
---
 gcc/config/bpf/core-builtins.cc | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index e03e986e2c1..beb039ea6e0 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -870,12 +870,14 @@ process_enum_value (struct cr_builtins *data)
       unsigned int index = 0;
       for (tree l = TYPE_VALUES (type); l; l = TREE_CHAIN (l))
        {
+         gcc_assert (index < (1 << 16));
          if (TREE_VALUE (l) == expr)
            {
-             char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
-             sprintf (tmp, "%d", index);
-             ret.str = (const char *) tmp;
-
+             /* Index can only be a value up to 2^16.  Should always fit
+                in 6 chars.  */
+             char tmp[6];
+             sprintf (tmp, "%u", index);
+             ret.str = CONST_CAST (char *, ggc_strdup(tmp));
              break;
            }
          index++;
-- 
2.30.2

Reply via email to