[binutils-gdb] sim: avoid segfault in sim/cris/sim-if.c

Andrew Burgess via Gdb-cvs <[email protected]> Mon, 3 Aug 2026 12:06:29 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D34c9c92f0837=
51f30ab2ecf2a9ab6f3576c0989c

commit 34c9c92f083751f30ab2ecf2a9ab6f3576c0989c
Author: Andrew Burgess <[email protected]>
Date:   Mon Aug 3 12:53:27 2026 +0100

    sim: avoid segfault in sim/cris/sim-if.c
   =20
    When GDB is built with --target=3Dcris-elf and launched with "gdb
    ./a.out", typing "target sim" causes a crash.  The issue is that
    STATE_PROG_ARGV may return NULL, and in sim_open this isn't tested
    before dereferencing the pointer.
   =20
    Approved-By: Andrew Burgess <[email protected]>

Diff:
---
 sim/cris/sim-if.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/sim/cris/sim-if.c b/sim/cris/sim-if.c
index 12c80983208..dd0894e2774 100644
--- a/sim/cris/sim-if.c
+++ b/sim/cris/sim-if.c
@@ -770,8 +770,11 @@ sim_open (SIM_OPEN_KIND kind, host_callback *callback,=
 struct bfd *abfd,
       for (envc =3D 0; environ[envc] !=3D NULL; envc++)
 	len +=3D strlen (environ[envc]) + 1;
=20
-      for (i =3D 0; prog_argv[i] !=3D NULL; my_argc++, i++)
-	len +=3D strlen (prog_argv[i]) + 1;
+      if (prog_argv !=3D NULL)
+	{
+	  for (i =3D 0; prog_argv[i] !=3D NULL; my_argc++, i++)
+	    len +=3D strlen (prog_argv[i]) + 1;
+	}
=20
       envstart =3D (envtop - len) & ~8191;