Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: Dominic Clifton <me@dominicclifton.name>, gdb-patches@sourceware.org
Subject: Re: [PATCH] read_file_scope - avoid eventual crash when the file or directory name is null.
Date: Mon, 13 Oct 2025 18:14:26 +0100	[thread overview]
Message-ID: <877bwybu6l.fsf@redhat.com> (raw)
In-Reply-To: <017301dc3943$0ba55950$22f00bf0$@dominicclifton.name>

"Dominic Clifton" <me@dominicclifton.name> writes:

> * without this, launching the debugger results in the following error:
>
> ```
> terminate called after throwing an instance of 'std::logic_error' what():
> basic_string: construction from null is not valid
> ```

Thanks for looking into this failure.

>
> find_file_and_directory - return empty strings instead of null pointers.
>
> ---
>  gdb/dwarf2/read.c | 29 +++++++++++++++++++++--------
>  1 file changed, 21 insertions(+), 8 deletions(-)
>
> diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
> index 955893c5f0c..287ce7b518c 100644
> --- a/gdb/dwarf2/read.c
> +++ b/gdb/dwarf2/read.c
> @@ -5942,10 +5942,18 @@ find_file_and_directory (struct die_info *die,
> struct dwarf2_cu *cu)
>    if (cu->per_cu->fnd != nullptr)
>      return *cu->per_cu->fnd;
>  
> +  // Ensure that nullptrs become empty strings
> +  const char *name = dwarf2_string_attr(die, DW_AT_name, cu);
> +  const char *comp_dir = dwarf2_string_attr(die, DW_AT_comp_dir, cu);
> +
> +  if (!name)
> +    name = "";
> +  if (!comp_dir)
> +    comp_dir = "";
> +
>    /* Find the filename.  Do not use dwarf2_name here, since the filename
>       is not a source language identifier.  */
> -  file_and_directory res (dwarf2_string_attr (die, DW_AT_name, cu),
> -			  dwarf2_string_attr (die, DW_AT_comp_dir, cu));
> +  file_and_directory res(name, comp_dir);

I suspect this change is going to cause problems.  If you checkout
struct file_and_directory in file-and-dir.h you'll see there are a
couple of methods which check for nullptr as a special value, so masking
all of the nullptr inputs is going to change the behaviour.

>  
>    if (res.get_comp_dir () == nullptr

And just here we have an example of some code which appears to be
checking for nullptr, which is no longer going to work as expected after
this change.

Could you expand on what the DWARF looks like that is triggering this
failure?

We have a DWARF assembler in our testsuite, see gdb.dwarf2/ for some
examples (look for 'Dwarf::assemble'), so we should be able to reproduce
the problematic DWARF and trigger the failure.  I initially figured it
might be as simple as a missing DW_AT_name on a DW_TAG_compile_unit, but
that didn't seem to trigger the issue, so there must be more too it.

If you can show the DWARF in question then we can likely write a test
for this issue, or, if you feel adventurous, you could have a go
yourself.

Thanks,
Andrew




>        && cu->producer_is_gcc_lt_4_3 ()
> @@ -6083,28 +6091,33 @@ read_file_scope (struct die_info *die, struct
> dwarf2_cu *cu)
>  
>    file_and_directory &fnd = find_file_and_directory (die, cu);
>  
> +  // ensure fnd.get_name() and fnd.get_comp_dir() are never null, which
> they sometimes are
> +  const char *fname = fnd.get_name() ? fnd.get_name() : "";
> +  const char *compdir = fnd.get_comp_dir() ? fnd.get_comp_dir() : "";
> +
>    /* GAS supports generating dwarf-5 info starting version 2.35.  Versions
>       2.35-2.37 generate an incorrect CU name attribute: it's relative,
>       implicitly prefixing it with the compilation dir.  Work around this by
>       prefixing it with the source dir instead.  */
> -  if (cu->header.version == 5 && !IS_ABSOLUTE_PATH (fnd.get_name ())
> +  if (cu->header.version == 5 && !IS_ABSOLUTE_PATH (fname)
>        && cu->producer_is_gas_lt_2_38 ())
>      {
>        attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
>        if (attr != nullptr && attr->form_is_unsigned ())
>  	{
>  	  sect_offset line_offset = (sect_offset) attr->as_unsigned ();
> -	  line_header_up lh = dwarf_decode_line_header (line_offset, cu,
> -							fnd.get_comp_dir
> ());
> +	  line_header_up lh = dwarf_decode_line_header (line_offset, cu,
> compdir);
>  	  if (lh->version == 5 && lh->include_dir_at (1) != nullptr)
>  	    {
> -	      std::string dir = lh->include_dir_at (1);
> -	      fnd.set_comp_dir (std::move (dir));
> +          const char* dir_ptr = lh->include_dir_at (1);
> +          std::string dir = dir_ptr ? dir_ptr : compdir;
> +
> +          fnd.set_comp_dir (std::move (dir));
>  	    }
>  	}
>      }
>  
> -  cu->start_compunit_symtab (fnd.get_name (), fnd.intern_comp_dir
> (objfile),
> +  cu->start_compunit_symtab (fname, fnd.intern_comp_dir (objfile),
>  			     lowpc);
>  
>    gdb_assert (per_objfile->sym_cu == nullptr);
> -- 
> 2.48.1


  reply	other threads:[~2025-10-13 17:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-05 15:03 Fixing gdb crash issue when loading an elf file compiled with rust 1.87.0 Dominic Clifton
2025-10-09 17:35 ` [PATCH] read_file_scope - avoid eventual crash when the file or directory name is null Dominic Clifton
2025-10-13 17:14   ` Andrew Burgess [this message]
2025-10-15 22:53     ` Dominic Clifton
2025-11-18  8:38       ` Tom de Vries
2025-11-18 17:14         ` Dominic Clifton
2025-11-27 15:02           ` Tom de Vries
2025-11-27 23:22             ` Dominic Clifton
2025-10-09 17:37 ` Fixing gdb crash issue when loading an elf file compiled with rust 1.87.0 Dominic Clifton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877bwybu6l.fsf@redhat.com \
    --to=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=me@dominicclifton.name \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox