public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] gprofng: fix SIGSEGV when processing unusual dwarf
@ 2023-02-08  9:05 Vladimir Mezentsev
  0 siblings, 0 replies; only message in thread
From: Vladimir Mezentsev @ 2023-02-08  9:05 UTC (permalink / raw)
  To: bfd-cvs, gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e02841b095a7797aaae1b064fb4b7acc2eb8d900

commit e02841b095a7797aaae1b064fb4b7acc2eb8d900
Author: Vladimir Mezentsev <vladimir.mezentsev@oracle.com>
Date:   Tue Feb 7 14:58:25 2023 -0800

    gprofng: fix SIGSEGV when processing unusual dwarf
    
    gprofng/ChangeLog
    2023-02-07  Vladimir Mezentsev  <vladimir.mezentsev@oracle.com>
    
            PR gprofng/30093
            * src/Dwarf.cc: add nullptr check.
            * src/DwarfLib.cc: Likewise.

Diff:
---
 gprofng/src/Dwarf.cc    | 16 ++++++++--------
 gprofng/src/DwarfLib.cc | 20 ++++++++++++++------
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/gprofng/src/Dwarf.cc b/gprofng/src/Dwarf.cc
index 1b33ae8f243..5485be7f796 100644
--- a/gprofng/src/Dwarf.cc
+++ b/gprofng/src/Dwarf.cc
@@ -606,12 +606,15 @@ Dwarf::archive_Dwarf (LoadObject *lo)
 	{
 	  mod->hdrOffset = dwrCUs->size ();
 	  DwrLineRegs *lineReg = dwrCU->get_dwrLineReg ();
-	  dwrCU->srcFiles = new Vector<SourceFile *> (VecSize (lineReg->file_names));
-	  for (long i = 0, sz = VecSize (lineReg->file_names); i < sz; i++)
+	  if (lineReg != NULL)
 	    {
-	      char *fname = lineReg->getPath (i + 1);
-	      SourceFile *sf = mod->findSource (fname, true);
-	      dwrCU->srcFiles->append (sf);
+	      dwrCU->srcFiles = new Vector<SourceFile *> (VecSize (lineReg->file_names));
+	      for (long i = 0, sz = VecSize (lineReg->file_names); i < sz; i++)
+		{
+		  char *fname = lineReg->getPath (i + 1);
+		  SourceFile *sf = mod->findSource (fname, true);
+		  dwrCU->srcFiles->append (sf);
+		}
 	    }
 
 	  Dwarf_cnt ctx;
@@ -986,9 +989,6 @@ DwrCU::append_Function (Dwarf_cnt *ctx)
       if (lineno > 0)
 	{
 	  func->setLineFirst (lineno);
-	  if (dwrLineReg == NULL)
-	    dwrLineReg = new DwrLineRegs (new DwrSec (dwarf->debug_lineSec,
-						   stmt_list_offset), comp_dir);
 	  int fileno = ((int) Dwarf_data (DW_AT_decl_file)) - 1;
 	  SourceFile *sf = ((fileno >= 0) && (fileno < VecSize (srcFiles))) ? srcFiles->get (fileno)
 		  : module->getMainSrc ();
diff --git a/gprofng/src/DwarfLib.cc b/gprofng/src/DwarfLib.cc
index 4f86a78d1c8..e7130a7a438 100644
--- a/gprofng/src/DwarfLib.cc
+++ b/gprofng/src/DwarfLib.cc
@@ -31,6 +31,7 @@
 #include "DbeArray.h"
 #include "DbeSession.h"
 
+#define NO_STMT_LIST ((uint64_t) -1)
 #define CASE_S(x)   case x: s = (char *) #x; break
 
 static char *
@@ -1557,8 +1558,11 @@ DwrLineRegs::getPath (int fn)
   if (*dir != '/')
     { // not absolute
       char *s = include_directories->fetch (0);
-      sb.append (s);
-      sb.append ('/');
+      if (s != NULL && *s != 0)
+	{
+	  sb.append (s);
+	  sb.append ('/');
+	}
     }
   sb.append (dir);
   sb.append ('/');
@@ -1590,7 +1594,7 @@ DwrCU::DwrCU (Dwarf *_dwarf)
   abbrevTable = NULL;
   dwrInlinedSubrs = NULL;
   srcFiles = NULL;
-  stmt_list_offset = 0;
+  stmt_list_offset = NO_STMT_LIST;
   dwrLineReg = NULL;
   isMemop = false;
   isGNU = false;
@@ -1857,7 +1861,9 @@ DwrCU::parse_cu_header (LoadObject *lo)
   char *name = Dwarf_string (DW_AT_name);
   if (name == NULL)
     name = NTXT ("UnnamedUnit");
-  stmt_list_offset = Dwarf_data (DW_AT_stmt_list);
+  int64_t v;
+  if (read_data_attr(DW_AT_stmt_list, &v) == DW_DLV_OK)
+    stmt_list_offset = v;
   comp_dir = dbe_strdup (Dwarf_string (DW_AT_comp_dir));
   char *dir_name = comp_dir ? StrChr (comp_dir, ':') : NULL;
   char *orig_name = Dwarf_string (DW_AT_SUN_original_name);
@@ -2073,6 +2079,8 @@ DwrCU::map_dwarf_lines (Module *mod)
 					Stabs::is_fortran (mod->lang_code));
 	}
     }
+  if (lineReg == NULL)
+    return;
   Vector<DwrLine *> *lines = lineReg->get_lines ();
 
   Include *includes = new Include;
@@ -2083,7 +2091,7 @@ DwrCU::map_dwarf_lines (Module *mod)
   for (long i = 0, sz = VecSize (lines); i < sz; i++)
     {
       DwrLine *dwrLine = lines->get (i);
-      char *filename = dwrLineReg->getPath (dwrLine->file);
+      char *filename = lineReg->getPath (dwrLine->file);
       if (filename == NULL)
 	continue;
       uint64_t pc = dwrLine->address;
@@ -2123,7 +2131,7 @@ DwrCU::map_dwarf_lines (Module *mod)
 DwrLineRegs *
 DwrCU::get_dwrLineReg ()
 {
-  if (dwrLineReg == NULL)
+  if (dwrLineReg == NULL && stmt_list_offset != NO_STMT_LIST)
     dwrLineReg = new DwrLineRegs (new DwrSec (dwarf->debug_lineSec,
 					      stmt_list_offset), comp_dir);
   return dwrLineReg;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-02-08  9:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-08  9:05 [binutils-gdb] gprofng: fix SIGSEGV when processing unusual dwarf Vladimir Mezentsev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).