[v2] backends: allocate enough stace for null terminator

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

Commit Message

Sergei Trofimovich July 17, 2024, 10:03 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 initializes the array bytewise.

	* backends/i386_regs.c (i386_register_info): Initialize the
	array bytewise 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   | 10 +++++++++-
 backends/x86_64_regs.c |  9 ++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)
  

Comments

Mark Wielaard July 18, 2024, 4:49 p.m. UTC | #1
Hi Sergei,

On Wed, 2024-07-17 at 23:03 +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"
>           |           ^~~~
> 
> `elfutils` does not need to store '\0'. We could either initialize the
> arrays with individual bytes or allocate extra byte for null.
> 
> This change initializes the array bytewise.
> 
> 	* backends/i386_regs.c (i386_register_info): Initialize the
> 	array bytewise to fix gcc-15 warning.
> 	* backends/x86_64_regs.c (x86_64_register_info): Ditto.

This looks good. I have pushed it to main.

Will this warning be enabled by -Wall or -Wextra (both of which are
enabled by default for elfutils builds)? Or would be need to enable it
explicitly?

Thanks,

Mark
  
Sergei Trofimovich July 18, 2024, 9:32 p.m. UTC | #2
On Thu, 18 Jul 2024 18:49:54 +0200
Mark Wielaard <mark@klomp.org> wrote:

> Hi Sergei,
> 
> On Wed, 2024-07-17 at 23:03 +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"
> >           |           ^~~~
> > 
> > `elfutils` does not need to store '\0'. We could either initialize the
> > arrays with individual bytes or allocate extra byte for null.
> > 
> > This change initializes the array bytewise.
> > 
> > 	* backends/i386_regs.c (i386_register_info): Initialize the
> > 	array bytewise to fix gcc-15 warning.
> > 	* backends/x86_64_regs.c (x86_64_register_info): Ditto.  
> 
> This looks good. I have pushed it to main.
> 
> Will this warning be enabled by -Wall or -Wextra (both of which are
> enabled by default for elfutils builds)? Or would be need to enable it
> explicitly?

Thank you! It's enabled only with `-Wextra` (and that's how I encountered
on build failure in `elfutils`).
  

Patch

diff --git a/backends/i386_regs.c b/backends/i386_regs.c
index 7ec93bb9..ead55ef7 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:
diff --git a/backends/x86_64_regs.c b/backends/x86_64_regs.c
index ef987daf..dab8f27f 100644
--- a/backends/x86_64_regs.c
+++ b/backends/x86_64_regs.c
@@ -82,7 +82,14 @@  x86_64_register_info (Ebl *ebl __attribute__ ((unused)),
     {
       static const char baseregs[][2] =
 	{
-	  "ax", "dx", "cx", "bx", "si", "di", "bp", "sp"
+	  {'a', 'x'},
+	  {'d', 'x'},
+	  {'c', 'x'},
+	  {'b', 'x'},
+	  {'s', 'i'},
+	  {'d', 'i'},
+	  {'b', 'p'},
+	  {'s', 'p'},
 	};
 
     case 6 ... 7: