[09/25] include libgen.h in system.h

Message ID 20221020182603.815-10-luoyonggang@gmail.com
State Rejected
Headers
Series Patches for building with mingw/gcc msvc/clang-cl |

Commit Message

Yonggang Luo Oct. 20, 2022, 6:25 p.m. UTC
  basename function are accessed multiple place, but used without include libgen.h

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 lib/system.h                         | 1 +
 libdw/dwarf_getsrc_file.c            | 2 +-
 libdwfl/dwfl_module_getsrc_file.c    | 2 +-
 libdwfl/dwfl_segment_report_module.c | 2 +-
 libdwfl/find-debuginfo.c             | 6 +++---
 libdwfl/link_map.c                   | 2 +-
 src/addr2line.c                      | 4 ++--
 src/nm.c                             | 4 ++--
 src/stack.c                          | 2 +-
 src/strip.c                          | 2 +-
 10 files changed, 14 insertions(+), 13 deletions(-)
  

Comments

Mark Wielaard Oct. 28, 2022, 11:45 a.m. UTC | #1
On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
wrote:
> basename function are accessed multiple place, but used without
> include libgen.h

This is wrong. We use the GNU basename (from string.h with
_GNU_SOURCE), not the POSIX one (from libgen.h).

Cheers,

Mark
  
lilydjwg--- via Elfutils-devel Dec. 16, 2022, 9:22 p.m. UTC | #2
On Fri, Oct 28, 2022 at 7:45 PM Mark Wielaard <mark@klomp.org> wrote:
>
> On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
> wrote:
> > basename function are accessed multiple place, but used without
> > include libgen.h
>
> This is wrong. We use the GNU basename (from string.h with
> _GNU_SOURCE), not the POSIX one (from libgen.h).

Thanks, that informs me, are they the same thing?
obviously mingw lacked of this

>
> Cheers,
>
> Mark



--
         此致
礼
罗勇刚
Yours
    sincerely,
Yonggang Luo
  
lilydjwg--- via Elfutils-devel Dec. 16, 2022, 9:34 p.m. UTC | #3
But still I think the force cast to (char *) is needed

On Sat, Dec 17, 2022 at 5:22 AM 罗勇刚(Yonggang Luo) <luoyonggang@gmail.com>
wrote:
>
>
>
> On Fri, Oct 28, 2022 at 7:45 PM Mark Wielaard <mark@klomp.org> wrote:
> >
> > On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
> > wrote:
> > > basename function are accessed multiple place, but used without
> > > include libgen.h
> >
> > This is wrong. We use the GNU basename (from string.h with
> > _GNU_SOURCE), not the POSIX one (from libgen.h).
>
> Thanks, that informs me, are they the same thing?
> obviously mingw lacked of this
>
> >
> > Cheers,
> >
> > Mark
>
>
>
> --
>          此致
> 礼
> 罗勇刚
> Yours
>     sincerely,
> Yonggang Luo



--
         此致
礼
罗勇刚
Yours
    sincerely,
Yonggang Luo
  
Mark Wielaard Dec. 20, 2022, 3:05 p.m. UTC | #4
On Sat, 2022-12-17 at 05:22 +0800, 罗勇刚(Yonggang Luo) wrote:
> On Fri, Oct 28, 2022 at 7:45 PM Mark Wielaard <mark@klomp.org> wrote:
> > 
> > On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
> > wrote:
> > > basename function are accessed multiple place, but used without
> > > include libgen.h
> > 
> > This is wrong. We use the GNU basename (from string.h with
> > _GNU_SOURCE), not the POSIX one (from libgen.h).
> 
> Thanks, that informs me, are they the same thing?

No, they are subtly different things.
See https://www.man7.org/linux/man-pages/man3/basename.3.html#NOTES
In particular the GNU basename never manipulates its argument (which is
why the cast is wrong).

Cheers,

Mark
  

Patch

diff --git a/lib/system.h b/lib/system.h
index 561d3e03..7132cd6d 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -42,6 +42,7 @@ 
 /* System dependend headers */
 #include <byteswap.h>
 #include <endian.h>
+#include <libgen.h>
 #if HAVE_DECL_MMAP
 #include <sys/mman.h>
 #endif
diff --git a/libdw/dwarf_getsrc_file.c b/libdw/dwarf_getsrc_file.c
index 5289c7da..884fea32 100644
--- a/libdw/dwarf_getsrc_file.c
+++ b/libdw/dwarf_getsrc_file.c
@@ -98,7 +98,7 @@  dwarf_getsrc_file (Dwarf *dbg, const char *fname, int lineno, int column,
 	      /* Match the name with the name the user provided.  */
 	      const char *fname2 = line->files->info[lastfile].name;
 	      if (is_basename)
-		lastmatch = strcmp (basename (fname2), fname) == 0;
+		lastmatch = strcmp (basename ((char *)fname2), fname) == 0;
 	      else
 		lastmatch = strcmp (fname2, fname) == 0;
 	    }
diff --git a/libdwfl/dwfl_module_getsrc_file.c b/libdwfl/dwfl_module_getsrc_file.c
index cea2ba41..6daf29d6 100644
--- a/libdwfl/dwfl_module_getsrc_file.c
+++ b/libdwfl/dwfl_module_getsrc_file.c
@@ -103,7 +103,7 @@  dwfl_module_getsrc_file (Dwfl_Module *mod,
 		{
 		  /* Match the name with the name the user provided.  */
 		  lastfile = file;
-		  lastmatch = !strcmp (is_basename ? basename (file) : file,
+		  lastmatch = !strcmp (is_basename ? basename ((char *)file) : file,
 				       fname);
 		}
 	    }
diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
index 287fc002..aa228254 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -736,7 +736,7 @@  dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
 	      bias += fixup;
 	      if (module->name[0] != '\0')
 		{
-		  name = basename (module->name);
+		  name = basename ((char *)module->name);
 		  name_is_final = true;
 		}
 	      break;
diff --git a/libdwfl/find-debuginfo.c b/libdwfl/find-debuginfo.c
index 7f7ab632..a9b7be3d 100644
--- a/libdwfl/find-debuginfo.c
+++ b/libdwfl/find-debuginfo.c
@@ -164,7 +164,7 @@  find_debuginfo_in_path (Dwfl_Module *mod, const char *file_name,
 {
   bool cancheck = debuglink_crc != (GElf_Word) 0;
 
-  const char *file_basename = file_name == NULL ? NULL : basename (file_name);
+  const char *file_basename = file_name == NULL ? NULL : basename ((char *)file_name);
   char *localname = NULL;
 
   /* We invent a debuglink .debug name if NULL, but then want to try the
@@ -278,7 +278,7 @@  find_debuginfo_in_path (Dwfl_Module *mod, const char *file_name,
 	  else
 	    {
 	      subdir = NULL;
-	      file = basename (debuglink_file);
+	      file = basename ((char *)debuglink_file);
 	    }
 	  try_file_basename = debuglink_null;
 	  break;
@@ -306,7 +306,7 @@  find_debuginfo_in_path (Dwfl_Module *mod, const char *file_name,
 	    if (mod->dw != NULL && (p[0] == '\0' || p[0] == '/'))
 	      {
 		fd = try_open (&main_stat, dir, ".dwz",
-			       basename (file), &fname);
+			       basename ((char *)file), &fname);
 		if (fd < 0)
 		  {
 		    if (errno != ENOENT && errno != ENOTDIR)
diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
index 7ec7eca1..403d4ee5 100644
--- a/libdwfl/link_map.c
+++ b/libdwfl/link_map.c
@@ -469,7 +469,7 @@  report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
 		      if (r_debug_info_module == NULL)
 			{
 			  // XXX hook for sysroot
-			  mod = __libdwfl_report_elf (dwfl, basename (name),
+			  mod = __libdwfl_report_elf (dwfl, basename ((char *)name),
 						      name, fd, elf, base,
 						      true, true);
 			  if (mod != NULL)
diff --git a/src/addr2line.c b/src/addr2line.c
index 7768b266..3abf1d7a 100644
--- a/src/addr2line.c
+++ b/src/addr2line.c
@@ -381,7 +381,7 @@  print_dwarf_function (Dwfl_Module *mod, Dwarf_Addr addr)
 		  if (file == NULL)
 		    file = "???";
 		  else if (only_basenames)
-		    file = basename (file);
+		    file = basename ((char *)file);
 		  else if (use_comp_dir && file[0] != '/')
 		    {
 		      const char *const *dirs;
@@ -564,7 +564,7 @@  print_src (const char *src, int lineno, int linecol, Dwarf_Die *cu)
   const char *comp_dir_sep = "";
 
   if (only_basenames)
-    src = basename (src);
+    src = basename ((char *)src);
   else if (use_comp_dir && src[0] != '/')
     {
       Dwarf_Attribute attr;
diff --git a/src/nm.c b/src/nm.c
index b46c1fd7..717ec0f6 100644
--- a/src/nm.c
+++ b/src/nm.c
@@ -1417,7 +1417,7 @@  show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr,
 			  int lineno;
 			  (void) dwarf_lineno (line, &lineno);
 			  const char *file = dwarf_linesrc (line, NULL, NULL);
-			  file = (file != NULL) ? basename (file) : "???";
+			  file = (file != NULL) ? basename ((char *)file) : "???";
 			  int n;
 			  n = obstack_printf (&whereob, "%s:%d%c", file,
 					      lineno, '\0');
@@ -1448,7 +1448,7 @@  show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr,
 		{
 		  /* We found the line.  */
 		  int n = obstack_printf (&whereob, "%s:%" PRIu64 "%c",
-					  basename ((*found)->file),
+					  basename ((char *)(*found)->file),
 					  (*found)->lineno,
 					  '\0');
 		  sym_mem[nentries_used].where = obstack_finish (&whereob);
diff --git a/src/stack.c b/src/stack.c
index 534aa93c..82413772 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -152,7 +152,7 @@  module_callback (Dwfl_Module *mod, void **userdata __attribute__((unused)),
 
   int width = get_addr_width (mod);
   printf ("0x%0*" PRIx64 "-0x%0*" PRIx64 " %s\n",
-	  width, start, width, end, basename (name));
+	  width, start, width, end, basename ((char *)name));
 
   const unsigned char *id;
   GElf_Addr id_vaddr;
diff --git a/src/strip.c b/src/strip.c
index 2a2cc801..6ac987fd 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -1800,7 +1800,7 @@  handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 		      elf_errmsg (-1));
 	}
 
-      char *debug_basename = basename (debug_fname_embed ?: debug_fname);
+      char *debug_basename = basename ((char *)(debug_fname_embed ?: debug_fname));
       off_t crc_offset = strlen (debug_basename) + 1;
       /* Align to 4 byte boundary */
       crc_offset = ((crc_offset - 1) & ~3) + 4;