backends: allocate enough stace for null terminator

Message ID 20240715212340.190915-1-slyich@gmail.com
State Committed
Headers
Series backends: allocate enough stace for null terminator |

Commit Message

Sergei Trofimovich July 15, 2024, 9:23 p.m. UTC
  `gcc-15` added a new warning in https://gcc.gnu.org/PR115185:

    i386_regs.c:88:11: error: initializer-string for array of 'char' is too long [-Werror=unterminated-string-initialization]
       88 |           "ax", "cx", "dx", "bx", "sp", "bp", "si", "di", "ip"
          |           ^~~~

`elfutils` does not need to store '\0'. We could either initialize the
arrays with individual bytes or allocate extra byte for null.

This change allocates extra byte per string to fit in null terminator.

	* backends/i386_regs.c (i386_register_info): Add extra byte per
	entry to fix gcc-15 warning.
	* backends/x86_64_regs.c (x86_64_register_info): Ditto.

Signed-off-by: Sergei Trofimovich <slyich@gmail.com>
---
 backends/i386_regs.c   | 2 +-
 backends/x86_64_regs.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
  

Comments

Mark Wielaard July 17, 2024, 1:10 p.m. UTC | #1
Hi Sergei,

On Mon, 2024-07-15 at 22:23 +0100, Sergei Trofimovich wrote:
> `gcc-15` added a new warning in https://gcc.gnu.org/PR115185:
> 
>     i386_regs.c:88:11: error: initializer-string for array of 'char' is too long [-Werror=unterminated-string-initialization]
>        88 |           "ax", "cx", "dx", "bx", "sp", "bp", "si", "di", "ip"
>           |           ^~~~

OK, I see why you want to warn for this. There is a real chance you use
those char array's with some str functions and then there is trouble.

> `elfutils` does not need to store '\0'. We could either initialize the
> arrays with individual bytes or allocate extra byte for null.

With initialize with individual bytes you mean:

diff --git a/backends/i386_regs.c b/backends/i386_regs.c
index 7ec93bb9fc13..ead55ef7f931 100644
--- a/backends/i386_regs.c
+++ b/backends/i386_regs.c
@@ -85,7 +85,15 @@ i386_register_info (Ebl *ebl __attribute__
((unused)),
     {
       static const char baseregs[][2] =
        {
-         "ax", "cx", "dx", "bx", "sp", "bp", "si", "di", "ip"
+         {'a', 'x'},
+         {'c', 'x'},
+         {'d', 'x'},
+         {'b', 'x'},
+         {'s', 'p'},
+         {'b', 'p'},
+         {'s', 'i'},
+         {'d', 'i'},
+         {'i', 'p'},
        };
 
     case 4:

?

Since the use of this array is basically just:

      name[0] = 'r';
      name[1] = baseregs[regno][0];
      name[2] = baseregs[regno][1];

I think I prefer the individual bytes init way. It makes more clear
what we really use these arrays for. imho.

Thanks,

Mark
  

Patch

diff --git a/backends/i386_regs.c b/backends/i386_regs.c
index 7ec93bb9..4bca1b1a 100644
--- a/backends/i386_regs.c
+++ b/backends/i386_regs.c
@@ -83,7 +83,7 @@  i386_register_info (Ebl *ebl __attribute__ ((unused)),
 
   switch (regno)
     {
-      static const char baseregs[][2] =
+      static const char baseregs[][3] =
 	{
 	  "ax", "cx", "dx", "bx", "sp", "bp", "si", "di", "ip"
 	};
diff --git a/backends/x86_64_regs.c b/backends/x86_64_regs.c
index ef987daf..c92c862a 100644
--- a/backends/x86_64_regs.c
+++ b/backends/x86_64_regs.c
@@ -80,7 +80,7 @@  x86_64_register_info (Ebl *ebl __attribute__ ((unused)),
 
   switch (regno)
     {
-      static const char baseregs[][2] =
+      static const char baseregs[][3] =
 	{
 	  "ax", "dx", "cx", "bx", "si", "di", "bp", "sp"
 	};