Come up with startswith function.
authorMartin Liska <mliska@suse.cz>
Mon, 19 Apr 2021 12:33:36 +0000 (14:33 +0200)
committerMark Wielaard <mark@klomp.org>
Wed, 12 May 2021 09:56:57 +0000 (11:56 +0200)
New function in system.h that returns true if a string has a given
prefix, false otherwise.  Use it in place of strncmp.

Signed-off-by: Martin Liška <mliska@suse.cz>
33 files changed:
backends/ChangeLog
backends/aarch64_symbol.c
backends/arm_symbol.c
debuginfod/ChangeLog
debuginfod/debuginfod-client.c
debuginfod/debuginfod.cxx
lib/ChangeLog
lib/system.h
libasm/ChangeLog
libasm/libasmP.h
libdw/ChangeLog
libdw/dwarf_begin_elf.c
libdwfl/ChangeLog
libdwfl/dwfl_frame.c
libdwfl/dwfl_module_getdwarf.c
libdwfl/linux-kernel-modules.c
libdwfl/linux-pid-attach.c
libdwfl/relocate.c
libebl/ChangeLog
libebl/eblobjnotetypename.c
libebl/eblopenbackend.c
src/ChangeLog
src/elfclassify.c
src/elfcompress.c
src/nm.c
src/readelf.c
src/strip.c
tests/ChangeLog
tests/dwelf_elf_e_machine_string.c
tests/dwelfgnucompressed.c
tests/elfgetchdr.c
tests/elfputzdata.c
tests/vdsosyms.c

index eb15c81..ac0e318 100644 (file)
@@ -1,3 +1,8 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * aarch64_symbol.c (aarch64_data_marker_symbol): Use startswith.
+       * arm_symbol.c (arm_data_marker_symbol): Likewise.
+
 2021-02-01  Érico Nogueira  <ericonr@disroot.org>
 
        * ppc_initreg.c: Also include <asm/ptrace.h>.
index 464a569..15e0805 100644 (file)
@@ -30,6 +30,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include <elf.h>
 #include <stddef.h>
 #include <string.h>
@@ -104,7 +106,7 @@ aarch64_data_marker_symbol (const GElf_Sym *sym, const char *sname)
   return (sym != NULL && sname != NULL
          && sym->st_size == 0 && GELF_ST_BIND (sym->st_info) == STB_LOCAL
          && GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
-         && (strcmp (sname, "$d") == 0 || strncmp (sname, "$d.", 3) == 0));
+         && (strcmp (sname, "$d") == 0 || startswith (sname, "$d.")));
 }
 
 const char *
index c8e1d7f..a733cff 100644 (file)
@@ -30,6 +30,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include <elf.h>
 #include <stddef.h>
 #include <string.h>
@@ -154,5 +156,5 @@ arm_data_marker_symbol (const GElf_Sym *sym, const char *sname)
   return (sym != NULL && sname != NULL
           && sym->st_size == 0 && GELF_ST_BIND (sym->st_info) == STB_LOCAL
           && GELF_ST_TYPE (sym->st_info) == STT_NOTYPE
-          && (strcmp (sname, "$d") == 0 || strncmp (sname, "$d.", 3) == 0));
+          && (strcmp (sname, "$d") == 0 || startswith (sname, "$d.")));
 }
index 97f598f..249385b 100644 (file)
@@ -1,3 +1,9 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * debuginfod-client.c (debuginfod_query_server): Use startswith.
+       (debuginfod_add_http_header): Likewise.
+       * debuginfod.cxx: Likewise.
+
 2021-05-04  Alice Zhang <alizhang@redhat.com>
 
        * debuginfod-client.c (cache_miss_default_s): New static time_t,
index 4fa047f..cb51c26 100644 (file)
@@ -1054,7 +1054,7 @@ debuginfod_query_server (debuginfod_client *c,
                                                     &scheme);
                   if(ok3 == CURLE_OK && scheme)
                     {
-                      if (strncmp (scheme, "HTTP", 4) == 0)
+                      if (startswith (scheme, "HTTP"))
                         if (resp_code == 200)
                           {
                             verified_handle = msg->easy_handle;
@@ -1318,7 +1318,7 @@ int debuginfod_add_http_header (debuginfod_client *client, const char* header)
 
   /* Track if User-Agent: is being set.  If so, signal not to add the
      default one. */
-  if (strncmp (header, "User-Agent:", 11) == 0)
+  if (startswith (header, "User-Agent:"))
     client->user_agent_set_p = 1;
 
   client->headers = temp;
index 0e6fd13..e0948ea 100644 (file)
@@ -37,6 +37,7 @@ extern "C" {
 
 #include "debuginfod.h"
 #include <dwarf.h>
+#include <system.h>
 
 #include <argp.h>
 #ifdef __GNUC__
@@ -2323,15 +2324,15 @@ elf_classify (int fd, bool &executable_p, bool &debuginfo_p, string &buildid, se
           const char *section_name = elf_strptr (elf, shstrndx, shdr->sh_name);
           if (section_name == NULL)
             break;
-          if (strncmp(section_name, ".debug_line", 11) == 0 ||
-              strncmp(section_name, ".zdebug_line", 12) == 0)
+          if (startswith (section_name, ".debug_line") ||
+              startswith (section_name, ".zdebug_line"))
             {
               debuginfo_p = true;
               dwarf_extract_source_paths (elf, debug_sourcefiles);
               break; // expecting only one .*debug_line, so no need to look for others
             }
-          else if (strncmp(section_name, ".debug_", 7) == 0 ||
-                   strncmp(section_name, ".zdebug_", 8) == 0)
+          else if (startswith (section_name, ".debug_") ||
+                   startswith (section_name, ".zdebug_"))
             {
               debuginfo_p = true;
               // NB: don't break; need to parse .debug_line for sources
index 371e213..dd3ebca 100644 (file)
@@ -1,3 +1,10 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * system.h (startswith): New function.
+       (pwrite_retry): Cast to char *.
+       (write_retry): Likewise.
+       (pread_retry): Likewise.
+
 2021-02-05  Mark Wielaard  <mark@klomp.org>
 
        * printversion.c (print_version): Update copyright year.
index 1c478e1..cdf18ed 100644 (file)
@@ -37,6 +37,7 @@
 #include <endian.h>
 #include <byteswap.h>
 #include <unistd.h>
+#include <string.h>
 
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 # define LE32(n)       (n)
     ((void *) ((char *) memcpy (dest, src, n) + (size_t) n))
 #endif
 
+/* Return TRUE if the start of STR matches PREFIX, FALSE otherwise.  */
+
+static inline int
+startswith (const char *str, const char *prefix)
+{
+  return strncmp (str, prefix, strlen (prefix)) == 0;
+}
+
 /* A special gettext function we use if the strings are too short.  */
 #define sgettext(Str) \
   ({ const char *__res = strrchr (_(Str), '|');                              \
@@ -104,7 +113,7 @@ pwrite_retry (int fd, const void *buf, size_t len, off_t off)
 
   do
     {
-      ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, buf + recvd, len - recvd,
+      ssize_t ret = TEMP_FAILURE_RETRY (pwrite (fd, ((char *)buf) + recvd, len - recvd,
                                                off + recvd));
       if (ret <= 0)
        return ret < 0 ? ret : recvd;
@@ -123,7 +132,7 @@ write_retry (int fd, const void *buf, size_t len)
 
   do
     {
-      ssize_t ret = TEMP_FAILURE_RETRY (write (fd, buf + recvd, len - recvd));
+      ssize_t ret = TEMP_FAILURE_RETRY (write (fd, ((char *)buf) + recvd, len - recvd));
       if (ret <= 0)
        return ret < 0 ? ret : recvd;
 
@@ -141,7 +150,7 @@ pread_retry (int fd, void *buf, size_t len, off_t off)
 
   do
     {
-      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, buf + recvd, len - recvd,
+      ssize_t ret = TEMP_FAILURE_RETRY (pread (fd, ((char *)buf) + recvd, len - recvd,
                                               off + recvd));
       if (ret <= 0)
        return ret < 0 ? ret : recvd;
index 98ac331..85e723e 100644 (file)
@@ -1,3 +1,7 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * libasmP.h (asm_emit_symbol_p): Use startswith.
+
 2020-12-16  Dmitry V. Levin  <ldv@altlinux.org>
 
        * libasmP.h (_): Remove.
index 8b72f32..5b5fb77 100644 (file)
@@ -302,6 +302,6 @@ extern int __disasm_cb_internal (DisasmCtx_t *ctx, const uint8_t **startp,
 // XXX The second part should probably be controlled by an option which
 // isn't implemented yet
 // XXX Also, the format will change with the backend.
-#define asm_emit_symbol_p(name) (strncmp (name, ".L", 2) != 0)
+#define asm_emit_symbol_p(name) (!startswith (name, ".L"))
 
 #endif /* libasmP.h */
index aa3e5ee..b5462ef 100644 (file)
@@ -1,3 +1,7 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * dwarf_begin_elf.c (check_section): Use startswith.
+
 2021-05-01  Mark Wielaard  <mark@klomp.org>
 
        * libdw_form.c (__libdw_form_val_compute_len): Check indirect
index 757ac4f..9e944b8 100644 (file)
@@ -30,6 +30,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include <assert.h>
 #include <stdbool.h>
 #include <stddef.h>
@@ -138,7 +140,7 @@ check_section (Dwarf *result, size_t shstrndx, Elf_Scn *scn, bool inscngrp)
          break;
        }
       else if (scnlen > 14 /* .gnu.debuglto_ prefix. */
-              && strncmp (scnname, ".gnu.debuglto_", 14) == 0
+              && startswith (scnname, ".gnu.debuglto_")
               && strcmp (&scnname[14], dwarf_scnnames[cnt]) == 0)
        break;
     }
index d107e78..fedf65a 100644 (file)
@@ -1,3 +1,13 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * dwfl_frame.c (dwfl_attach_state): Use startswith.
+       * dwfl_module_getdwarf.c (find_symtab): Likewise.
+       * linux-kernel-modules.c: Likewise.
+       * linux-pid-attach.c (linux_proc_pid_is_stopped): Likewise.
+       (dwfl_linux_proc_attach): Likewise.
+       * relocate.c (resolve_symbol): Likewise.
+       (relocate_section): Likewise.
+
 2021-02-01  Érico Nogueira  <ericonr@disroot.org>
 
        * dwfl_error.c (strerror_r): Only use the GNU version when available.
index 5bbf850..77e0c5c 100644 (file)
@@ -30,6 +30,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include "libdwflP.h"
 #include <unistd.h>
 
@@ -172,7 +174,7 @@ dwfl_attach_state (Dwfl *dwfl, Elf *elf, pid_t pid,
             is called from dwfl_linux_proc_attach with elf == NULL.
             __libdwfl_module_getebl will call __libdwfl_getelf which
             will call the find_elf callback.  */
-         if (strncmp (mod->name, "[vdso: ", 7) == 0
+         if (startswith (mod->name, "[vdso: ")
              || strcmp (strrchr (mod->name, ' ') ?: "",
                         " (deleted)") == 0)
            continue;
index 2f3dd0d..6f07605 100644 (file)
@@ -1162,7 +1162,7 @@ find_symtab (Dwfl_Module *mod)
   if (sname == NULL)
     goto elferr;
 
-  if (strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
+  if (startswith (sname, ".zdebug"))
     /* Try to uncompress, but it might already have been, an error
        might just indicate, already uncompressed.  */
     elf_compress_gnu (symstrscn, 0, 0);
@@ -1245,7 +1245,7 @@ find_symtab (Dwfl_Module *mod)
       if (sname == NULL)
        goto elferr;
 
-      if (strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
+      if (startswith (sname, ".zdebug"))
        /* Try to uncompress, but it might already have been, an error
           might just indicate, already uncompressed.  */
        elf_compress_gnu (aux_strscn, 0, 0);
index 6edb27f..c0f8dfa 100644 (file)
@@ -924,7 +924,7 @@ dwfl_linux_kernel_module_section_address
 
          if (!strcmp (secname, ".modinfo")
              || !strcmp (secname, ".data.percpu")
-             || !strncmp (secname, ".exit", 5))
+             || startswith (secname, ".exit"))
            {
              *addr = (Dwarf_Addr) -1l;
              return DWARF_CB_OK;
@@ -935,7 +935,7 @@ dwfl_linux_kernel_module_section_address
             behavior, and this cruft leaks out into the /sys information.
             The file name for ".init*" may actually look like "_init*".  */
 
-         const bool is_init = !strncmp (secname, ".init", 5);
+         const bool is_init = startswith (secname, ".init");
          if (is_init)
            {
              if (asprintf (&sysfile, SECADDRDIRFMT "_%s",
index fdf5c9b..cd53482 100644 (file)
@@ -30,6 +30,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include "libelfP.h"
 #include "libdwflP.h"
 #include <sys/types.h>
@@ -59,7 +61,7 @@ linux_proc_pid_is_stopped (pid_t pid)
 
   have_state = false;
   while (fgets (buffer, sizeof (buffer), procfile) != NULL)
-    if (strncmp (buffer, "State:", 6) == 0)
+    if (startswith (buffer, "State:"))
       {
        have_state = true;
        break;
@@ -407,7 +409,7 @@ dwfl_linux_proc_attach (Dwfl *dwfl, pid_t pid, bool assume_ptrace_stopped)
   char *line = NULL;
   size_t linelen = 0;
   while (getline (&line, &linelen, procfile) >= 0)
-    if (strncmp (line, "Tgid:", 5) == 0)
+    if (startswith (line, "Tgid:"))
       {
        errno = 0;
        char *endptr;
index 88b5211..0497bd4 100644 (file)
@@ -30,6 +30,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include "libelfP.h"
 #include "libdwflP.h"
 
@@ -237,7 +239,7 @@ resolve_symbol (Dwfl_Module *referer, struct reloc_symtab_cache *symtab,
            return DWFL_E_LIBELF;
 
          /* If the section is already decompressed, that isn't an error.  */
-         if (strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
+         if (startswith (sname, ".zdebug"))
            elf_compress_gnu (scn, 0, 0);
 
          if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
@@ -518,7 +520,7 @@ relocate_section (Dwfl_Module *mod, Elf *relocated, const GElf_Ehdr *ehdr,
        Nothing to do here.  */
     return DWFL_E_NOERROR;
 
-  if (strncmp (tname, ".zdebug", strlen ("zdebug")) == 0)
+  if (startswith (tname, ".zdebug"))
     elf_compress_gnu (tscn, 0, 0);
 
   if ((tshdr->sh_flags & SHF_COMPRESSED) != 0)
@@ -539,7 +541,7 @@ relocate_section (Dwfl_Module *mod, Elf *relocated, const GElf_Ehdr *ehdr,
   if (sname == NULL)
     return DWFL_E_LIBELF;
 
-  if (strncmp (sname, ".zdebug", strlen ("zdebug")) == 0)
+  if (startswith (sname, ".zdebug"))
     elf_compress_gnu (scn, 0, 0);
 
   if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
index 33208f0..fff66b3 100644 (file)
@@ -1,3 +1,8 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * eblobjnotetypename.c (ebl_object_note_type_name): Use startswith.
+       * eblopenbackend.c (default_debugscn_p): Likewise.
+
 2020-12-16  Dmitry V. Levin  <ldv@altlinux.org>
 
        * libeblP.h (_): Remove.
index 9daddcd..4662906 100644 (file)
@@ -31,6 +31,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include <inttypes.h>
 #include <stdio.h>
 #include <string.h>
@@ -79,8 +81,7 @@ ebl_object_note_type_name (Ebl *ebl, const char *name, uint32_t type,
            }
        }
 
-      if (strncmp (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX,
-                  strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX)) == 0)
+      if (startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX))
        {
          /* GNU Build Attribute notes (ab)use the owner name to store
             most of their data.  Don't decode everything here.  Just
index a8af165..71fafed 100644 (file)
@@ -616,9 +616,9 @@ default_debugscn_p (const char *name)
                                   / sizeof (dwarf_scn_names[0]));
   for (size_t cnt = 0; cnt < ndwarf_scn_names; ++cnt)
     if (strcmp (name, dwarf_scn_names[cnt]) == 0
-       || (strncmp (name, ".zdebug", strlen (".zdebug")) == 0
+       || (startswith (name, ".zdebug")
            && strcmp (&name[2], &dwarf_scn_names[cnt][1]) == 0)
-       || (strncmp (name, ".gnu.debuglto_", strlen (".gnu.debuglto_")) == 0
+       || (startswith (name, ".gnu.debuglto_")
            && strcmp (&name[14], dwarf_scn_names[cnt]) == 0))
       return true;
 
index 0cea28e..c5ecc05 100644 (file)
@@ -1,3 +1,14 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * elfclassify.c (run_classify): Use startswith.
+       * elfcompress.c (process_file): Likewise.
+       * nm.c (show_symbols_sysv): Likewise.
+       * readelf.c (print_debug): Likewise.
+       (handle_notes_data): Likewise.
+       (dump_data_section): Likewise.
+       (print_string_section): Likewise.
+       * strip.c (remove_debug_relocations): Likewise.
+
 2021-04-03  Mark Wielaard  <mark@klomp.org>
 
        * nm.c (show_symbols): close dwfl_fd if dwfl_begin fails.
index ae626bb..fe7eeee 100644 (file)
@@ -16,6 +16,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
+#include <system.h>
 
 #include <argp.h>
 #include <error.h>
@@ -335,11 +336,8 @@ run_classify (void)
                     stderr);
            has_bits_alloc = true;
          }
-        const char *debug_prefix = ".debug_";
-        const char *zdebug_prefix = ".zdebug_";
-        if (strncmp (section_name, debug_prefix, strlen (debug_prefix)) == 0
-           || strncmp (section_name, zdebug_prefix,
-                       strlen (zdebug_prefix)) == 0)
+        if (startswith (section_name, ".debug_")
+           || startswith (section_name, ".zdebug_"))
           {
             if (verbose > 1 && !has_debug_sections)
               fputs ("debug: .debug_* section found\n", stderr);
index c5ba6c3..d5bc330 100644 (file)
@@ -448,7 +448,7 @@ process_file (const char *fname)
        {
          if (!force && type == T_DECOMPRESS
              && (shdr->sh_flags & SHF_COMPRESSED) == 0
-             && strncmp (sname, ".zdebug", strlen (".zdebug")) != 0)
+             && !startswith (sname, ".zdebug"))
            {
              if (verbose > 0)
                printf ("[%zd] %s already decompressed\n", ndx, sname);
@@ -460,7 +460,7 @@ process_file (const char *fname)
                printf ("[%zd] %s already compressed\n", ndx, sname);
            }
          else if (!force && type == T_COMPRESS_GNU
-                  && strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
+                  && startswith (sname, ".zdebug"))
            {
              if (verbose > 0)
                printf ("[%zd] %s already GNU compressed\n", ndx, sname);
@@ -472,11 +472,9 @@ process_file (const char *fname)
              /* Check if we might want to change this section name.  */
              if (! adjust_names
                  && ((type != T_COMPRESS_GNU
-                      && strncmp (sname, ".zdebug",
-                                  strlen (".zdebug")) == 0)
+                      && startswith (sname, ".zdebug"))
                      || (type == T_COMPRESS_GNU
-                         && strncmp (sname, ".debug",
-                                     strlen (".debug")) == 0)))
+                         && startswith (sname, ".debug"))))
                adjust_names = true;
 
              /* We need a buffer this large if we change the names.  */
@@ -696,7 +694,7 @@ process_file (const char *fname)
                                        false, false, verbose > 0) < 0)
                    goto cleanup;
                }
-             else if (strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
+             else if (startswith (sname, ".zdebug"))
                {
                  snamebuf[0] = '.';
                  strcpy (&snamebuf[1], &sname[2]);
@@ -710,7 +708,7 @@ process_file (const char *fname)
              break;
 
            case T_COMPRESS_GNU:
-             if (strncmp (sname, ".debug", strlen (".debug")) == 0)
+             if (startswith (sname, ".debug"))
                {
                  if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
                    {
@@ -757,7 +755,7 @@ process_file (const char *fname)
                }
              else if (verbose >= 0)
                {
-                 if (strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
+                 if (startswith (sname, ".zdebug"))
                    printf ("[%zd] %s unchanged, already GNU compressed",
                            ndx, sname);
                  else
@@ -769,7 +767,7 @@ process_file (const char *fname)
            case T_COMPRESS_ZLIB:
              if ((shdr->sh_flags & SHF_COMPRESSED) == 0)
                {
-                 if (strncmp (sname, ".zdebug", strlen (".zdebug")) == 0)
+                 if (startswith (sname, ".zdebug"))
                    {
                      /* First decompress to recompress zlib style.
                         Don't report even when verbose.  */
@@ -900,7 +898,7 @@ process_file (const char *fname)
                      symtab_size = size;
                      symtab_compressed = T_COMPRESS_ZLIB;
                    }
-                 else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
+                 else if (startswith (name, ".zdebug"))
                    {
                      /* Don't report the (internal) uncompression.  */
                      if (compress_section (newscn, size, sname, NULL, ndx,
@@ -1046,7 +1044,7 @@ process_file (const char *fname)
          shstrtab_size = shdr->sh_size;
          if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
            shstrtab_compressed = T_COMPRESS_ZLIB;
-         else if (strncmp (shstrtab_name, ".zdebug", strlen (".zdebug")) == 0)
+         else if (startswith (shstrtab_name, ".zdebug"))
            shstrtab_compressed = T_COMPRESS_GNU;
        }
 
@@ -1187,8 +1185,7 @@ process_file (const char *fname)
                  symtab_size = shdr->sh_size;
                  if ((shdr->sh_flags & SHF_COMPRESSED) != 0)
                    symtab_compressed = T_COMPRESS_ZLIB;
-                 else if (strncmp (symtab_name, ".zdebug",
-                                   strlen (".zdebug")) == 0)
+                 else if (startswith (symtab_name, ".zdebug"))
                    symtab_compressed = T_COMPRESS_GNU;
                }
 
index dc2186d..b99805e 100644 (file)
--- a/src/nm.c
+++ b/src/nm.c
@@ -858,7 +858,7 @@ show_symbols_sysv (Ebl *ebl, GElf_Word strndx, const char *fullname,
       bind = ebl_symbol_binding_name (ebl,
                                      GELF_ST_BIND (syms[cnt].sym.st_info),
                                      symbindbuf, sizeof (symbindbuf));
-      if (bind != NULL && strncmp (bind, "GNU_", strlen ("GNU_")) == 0)
+      if (bind != NULL && startswith (bind, "GNU_"))
        bind += strlen ("GNU_");
       printf ("%-*s|%s|%-6s|%-8s|%s|%*s|%s\n",
              longest_name, symstr, addressbuf, bind,
index b974045..9b47262 100644 (file)
@@ -1335,7 +1335,7 @@ There are %zd section headers, starting at offset %#" PRIx64 ":\n\
                       _("bad compression header for section %zd: %s"),
                       elf_ndxscn (scn), elf_errmsg (-1));
            }
-         else if (strncmp(".zdebug", sname, strlen (".zdebug")) == 0)
+         else if (startswith (sname, ".zdebug"))
            {
              ssize_t size;
              if ((size = dwelf_scn_gnu_compressed_size (scn)) >= 0)
@@ -11451,7 +11451,7 @@ print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
                          || (scnlen == dbglen + 5
                              && strstr (name, ".dwo") == name + dbglen + 1)))
                  || (scnlen > 14 /* .gnu.debuglto_ prefix. */
-                     && strncmp (name, ".gnu.debuglto_", 14) == 0
+                     && startswith (name, ".gnu.debuglto_")
                      && strcmp (&name[14], debug_sections[n].name) == 0)
 )
                {
@@ -12455,8 +12455,7 @@ handle_notes_data (Ebl *ebl, const GElf_Ehdr *ehdr,
         into the owner name field.  Extract just the owner name
         prefix here, then use the rest later as data.  */
       bool is_gnu_build_attr
-       = strncmp (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX,
-                  strlen (ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX)) == 0;
+       = startswith (name, ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX);
       const char *print_name = (is_gnu_build_attr
                                ? ELF_NOTE_GNU_BUILD_ATTRIBUTE_PREFIX : name);
       size_t print_namesz = (is_gnu_build_attr
@@ -12636,7 +12635,7 @@ dump_data_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
                        _("Couldn't uncompress section"),
                        elf_ndxscn (scn));
            }
-         else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
+         else if (startswith (name, ".zdebug"))
            {
              if (elf_compress_gnu (scn, 0, 0) < 0)
                printf ("WARNING: %s [%zd]\n",
@@ -12687,7 +12686,7 @@ print_string_section (Elf_Scn *scn, const GElf_Shdr *shdr, const char *name)
                        _("Couldn't uncompress section"),
                        elf_ndxscn (scn));
            }
-         else if (strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
+         else if (startswith (name, ".zdebug"))
            {
              if (elf_compress_gnu (scn, 0, 0) < 0)
                printf ("WARNING: %s [%zd]\n",
index 7a5d4e4..70fc8c0 100644 (file)
@@ -607,7 +607,7 @@ remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr,
          GElf_Chdr tchdr;
          int tcompress_type = 0;
          bool is_gnu_compressed = false;
-         if (strncmp (tname, ".zdebug", strlen ("zdebug")) == 0)
+         if (startswith (tname, ".zdebug"))
            {
              is_gnu_compressed = true;
              if (elf_compress_gnu (tscn, 0, 0) != 1)
index 35fb2b2..4951e14 100644 (file)
@@ -1,3 +1,11 @@
+2021-04-19  Martin Liska  <mliska@suse.cz>
+
+       * dwelf_elf_e_machine_string.c (main): Use startswith.
+       * dwelfgnucompressed.c (main): Likewise.
+       * elfgetchdr.c (main): Likewise.
+       * elfputzdata.c (main): Likewise.
+       * vdsosyms.c (module_callback): Likewise.
+
 2021-05-04  Alice Zhang  <alizhang@redhat.com>
 
        * run-debuginfod-find.sh: Added tests for negative cache files.
index afad105..30599c3 100644 (file)
@@ -19,6 +19,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include <assert.h>
 #include <errno.h>
 #include <inttypes.h>
@@ -41,7 +43,7 @@ main (int argc, char **argv)
       const char *machine;
 
       errno = 0;
-      if (strncmp ("0x", argv[i], 2) == 0)
+      if (startswith (argv[i], "0x"))
        val = strtol (&argv[i][2], NULL, 16);
       else
        val = strtol (argv[i], NULL, 10);
index 0132271..447f3d5 100644 (file)
@@ -18,6 +18,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include <assert.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -82,7 +84,7 @@ main (int argc, char *argv[])
              break;
            }
 
-         if (strncmp(".zdebug", sname, strlen (".zdebug")) == 0)
+         if (startswith (sname, ".zdebug"))
            {
              ssize_t size;
              if ((size = dwelf_scn_gnu_compressed_size (scn)) == -1)
index 44ba178..171c4df 100644 (file)
@@ -18,6 +18,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include <assert.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -99,7 +101,7 @@ main (int argc, char *argv[])
                }
 
              /* This duplicates what the dwelfgnucompressed testcase does.  */
-             if (strncmp(".zdebug", sname, strlen (".zdebug")) == 0)
+             if (startswith (sname, ".zdebug"))
                {
                  ssize_t size;
                  if ((size = dwelf_scn_gnu_compressed_size (scn)) == -1)
index 0d9c020..0ff363f 100644 (file)
@@ -18,6 +18,8 @@
 # include <config.h>
 #endif
 
+#include <system.h>
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -83,7 +85,7 @@ main (int argc, char *argv[])
              printf ("Cannot compress %zd %s\n", idx, name);
            }
          else if ((shdr->sh_flags & SHF_COMPRESSED) != 0
-                  || strncmp (name, ".zdebug", strlen (".zdebug")) == 0)
+                  || startswith (name, ".zdebug"))
            {
              printf ("Already compressed %zd %s\n", idx, name);
            }
index 83ab034..ff1a18a 100644 (file)
@@ -43,7 +43,7 @@ module_callback (Dwfl_Module *mod, void **userdata __attribute__((unused)),
 {
   /* We can only recognize the vdso by inspecting the "magic name".  */
   printf ("module name: %s\n", name);
-  if (strncmp ("[vdso: ", name, 7) == 0)
+  if (startswith (name, "[vdso: "))
     {
       vdso_syms = dwfl_module_getsymtab (mod);
       printf ("vdso syms: %d\n", vdso_syms);