[PATCH] Remove redundant casts of memory allocating functions returning void *

Dmitry V. Levin ldv@altlinux.org
Mon Sep 6 08:00:00 GMT 2021


Return values of functions returning "void *", e.g. calloc, malloc,
realloc, xcalloc, xmalloc, and xrealloc, do not need explicit casts.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
---
 debuginfod/ChangeLog             |  5 +++++
 debuginfod/debuginfod-client.c   |  2 +-
 lib/ChangeLog                    |  6 ++++++
 lib/dynamicsizehash.c            |  2 +-
 lib/dynamicsizehash_concurrent.c |  2 +-
 libasm/ChangeLog                 | 14 ++++++++++++++
 libasm/asm_align.c               |  5 ++---
 libasm/asm_begin.c               |  3 +--
 libasm/asm_fill.c                |  3 +--
 libasm/asm_newabssym.c           |  2 +-
 libasm/asm_newcomsym.c           |  2 +-
 libasm/asm_newscn.c              |  2 +-
 libasm/asm_newscngrp.c           |  2 +-
 libasm/asm_newsubscn.c           |  2 +-
 libasm/asm_newsym.c              |  2 +-
 libasm/disasm_begin.c            |  2 +-
 libdw/ChangeLog                  |  9 +++++++++
 libdw/dwarf_begin_elf.c          |  8 ++++----
 libdw/dwarf_getpubnames.c        |  5 ++---
 libdw/dwarf_getsrclines.c        |  2 +-
 libdwelf/ChangeLog               |  6 ++++++
 libdwelf/dwelf_strtab.c          |  5 ++---
 libdwfl/ChangeLog                |  5 +++++
 libdwfl/linux-pid-attach.c       |  2 +-
 libebl/ChangeLog                 |  4 ++++
 libebl/eblopenbackend.c          |  2 +-
 libelf/ChangeLog                 | 14 ++++++++++++++
 libelf/common.h                  |  2 +-
 libelf/elf32_updatefile.c        |  4 ++--
 libelf/elf_begin.c               |  2 +-
 libelf/elf_getarsym.c            |  5 ++---
 libelf/elf_getdata.c             |  9 ++++-----
 libelf/elf_getscn.c              |  4 ++--
 libelf/elf_newdata.c             |  2 +-
 libelf/elf_newscn.c              | 10 +++++-----
 libelf/elf_readall.c             |  2 +-
 src/ChangeLog                    | 13 +++++++++++++
 src/elflint.c                    |  2 +-
 src/findtextrel.c                |  7 ++-----
 src/nm.c                         |  7 +++----
 src/readelf.c                    | 14 ++++++--------
 src/strip.c                      |  9 ++++-----
 tests/ChangeLog                  |  4 ++++
 tests/elfcopy.c                  |  2 +-
 44 files changed, 141 insertions(+), 75 deletions(-)

diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog
index c5459823..7e221f54 100644
--- a/debuginfod/ChangeLog
+++ b/debuginfod/ChangeLog
@@ -1,3 +1,8 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* debuginfod-client.c (debuginfod_begin): Remove cast of calloc return
+	value.
+
 2021-08-28  Mark Wielaard  <mjw@redhat.com>
 
 	* debuginfod.cxx (parse_opt): Turn the -d arg ":memory:" into
diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c
index 7d4b220f..d41723ce 100644
--- a/debuginfod/debuginfod-client.c
+++ b/debuginfod/debuginfod-client.c
@@ -1359,7 +1359,7 @@ debuginfod_begin (void)
 {
   debuginfod_client *client;
   size_t size = sizeof (struct debuginfod_client);
-  client = (debuginfod_client *) calloc (1, size);
+  client = calloc (1, size);
 
   if (client != NULL)
     {
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 60d32082..563b0b6a 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,9 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* dynamicsizehash.c (INIT(NAME)): Remove cast of calloc return value.
+	* dynamicsizehash_concurrent.c (INIT(NAME)): Remove cast of malloc
+	return value.
+
 2021-08-23  Saleem Abdulrasool  <abdulras@google.com>
 
 	* system.h: Remove inline definition for error and error_message_count
diff --git a/lib/dynamicsizehash.c b/lib/dynamicsizehash.c
index f9406eba..76c86dad 100644
--- a/lib/dynamicsizehash.c
+++ b/lib/dynamicsizehash.c
@@ -184,7 +184,7 @@ INIT(NAME) (NAME *htab, size_t init_size)
 #ifdef ITERATE
   htab->first = NULL;
 #endif
-  htab->table = (void *) calloc ((init_size + 1), sizeof (htab->table[0]));
+  htab->table = calloc ((init_size + 1), sizeof (htab->table[0]));
   if (htab->table == NULL)
     return -1;
 
diff --git a/lib/dynamicsizehash_concurrent.c b/lib/dynamicsizehash_concurrent.c
index 2d53bec6..4e2e2476 100644
--- a/lib/dynamicsizehash_concurrent.c
+++ b/lib/dynamicsizehash_concurrent.c
@@ -355,7 +355,7 @@ INIT(NAME) (NAME *htab, size_t init_size)
 
   pthread_rwlock_init(&htab->resize_rwl, NULL);
 
-  htab->table = (void *) malloc ((init_size + 1) * sizeof (htab->table[0]));
+  htab->table = malloc ((init_size + 1) * sizeof (htab->table[0]));
   if (htab->table == NULL)
       return -1;
 
diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index 85e723e6..c65fd21b 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,3 +1,17 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* asm_align.c (__libasm_ensure_section_space): Remove casts of calloc
+	return values.
+	* asm_begin.c (asm_begin): Remove cast of malloc return value.
+	* asm_fill.c (asm_fill): Likewise.
+	* asm_newabssym.c (asm_newabssym): Likewise.
+	* asm_newcomsym.c (asm_newcomsym): Likewise.
+	* asm_newscn.c (asm_newscn): Likewise.
+	* asm_newscngrp.c (asm_newscngrp): Likewise.
+	* asm_newsubscn.c (asm_newsubscn): Likewise.
+	* asm_newsym.c (asm_newsym): Likewise.
+	* disasm_begin.c (disasm_begin): Likewise.
+
 2021-04-19  Martin Liska  <mliska@suse.cz>
 
 	* libasmP.h (asm_emit_symbol_p): Use startswith.
diff --git a/libasm/asm_align.c b/libasm/asm_align.c
index c8c671b2..3a976756 100644
--- a/libasm/asm_align.c
+++ b/libasm/asm_align.c
@@ -143,8 +143,7 @@ __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
       /* This is the first block.  */
       size = MAX (2 * len, 960);
 
-      asmscn->content = (struct AsmData *) calloc (1, sizeof (struct AsmData)
-						   + size);
+      asmscn->content = calloc (1, sizeof (struct AsmData) + size);
       if (asmscn->content == NULL)
 	return -1;
 
@@ -160,7 +159,7 @@ __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
 
       size = MAX (2 *len, MIN (32768, 2 * asmscn->offset));
 
-      newp = (struct AsmData *) calloc (1, sizeof (struct AsmData) + size);
+      newp = calloc (1, sizeof (struct AsmData) + size);
       if (newp == NULL)
 	return -1;
 
diff --git a/libasm/asm_begin.c b/libasm/asm_begin.c
index 1df2d4ea..a190202c 100644
--- a/libasm/asm_begin.c
+++ b/libasm/asm_begin.c
@@ -138,8 +138,7 @@ asm_begin (const char *fname, Ebl *ebl, bool textp)
      right away.  Instead we create a temporary file in the same
      directory which, if everything goes alright, will replace a
      possibly existing file with the given name.  */
-  AsmCtx_t *result
-    = (AsmCtx_t *) malloc (sizeof (AsmCtx_t) + 2 * fname_len + 9);
+  AsmCtx_t *result = malloc (sizeof (AsmCtx_t) + 2 * fname_len + 9);
   if (result == NULL)
     return NULL;
 
diff --git a/libasm/asm_fill.c b/libasm/asm_fill.c
index 62d9d732..783555ee 100644
--- a/libasm/asm_fill.c
+++ b/libasm/asm_fill.c
@@ -54,8 +54,7 @@ asm_fill (AsmScn_t *asmscn, void *bytes, size_t len)
   else
     {
       /* Allocate appropriate memory.  */
-      pattern = (struct FillPattern *) malloc (sizeof (struct FillPattern)
-					       + len);
+      pattern = malloc (sizeof (struct FillPattern) + len);
       if (pattern == NULL)
 	return -1;
 
diff --git a/libasm/asm_newabssym.c b/libasm/asm_newabssym.c
index 34fef3e3..728d6043 100644
--- a/libasm/asm_newabssym.c
+++ b/libasm/asm_newabssym.c
@@ -71,7 +71,7 @@ asm_newabssym (AsmCtx_t *ctx, const char *name, GElf_Xword size,
 
   rwlock_wrlock (ctx->lock);
 
-  result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
+  result = malloc (sizeof (AsmSym_t));
   if (result == NULL)
     return NULL;
 
diff --git a/libasm/asm_newcomsym.c b/libasm/asm_newcomsym.c
index ee3b6966..750a1380 100644
--- a/libasm/asm_newcomsym.c
+++ b/libasm/asm_newcomsym.c
@@ -71,7 +71,7 @@ asm_newcomsym (AsmCtx_t *ctx, const char *name, GElf_Xword size,
 
   rwlock_wrlock (ctx->lock);
 
-  result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
+  result = malloc (sizeof (AsmSym_t));
   if (result == NULL)
     return NULL;
 
diff --git a/libasm/asm_newscn.c b/libasm/asm_newscn.c
index 7cdf484f..1150015f 100644
--- a/libasm/asm_newscn.c
+++ b/libasm/asm_newscn.c
@@ -181,7 +181,7 @@ asm_newscn (AsmCtx_t *ctx, const char *scnname, GElf_Word type,
   rwlock_wrlock (ctx->lock);
 
   /* This is a new section.  */
-  result = (AsmScn_t *) malloc (sizeof (AsmScn_t) + scnname_len);
+  result = malloc (sizeof (AsmScn_t) + scnname_len);
   if (result != NULL)
     {
       /* Add the name.  */
diff --git a/libasm/asm_newscngrp.c b/libasm/asm_newscngrp.c
index 80757a9a..0ca87fba 100644
--- a/libasm/asm_newscngrp.c
+++ b/libasm/asm_newscngrp.c
@@ -57,7 +57,7 @@ asm_newscngrp (AsmCtx_t *ctx, const char *grpname, AsmSym_t *signature,
       return NULL;
     }
 
-  result = (AsmScnGrp_t *) malloc (sizeof (AsmScnGrp_t) + grpname_len);
+  result = malloc (sizeof (AsmScnGrp_t) + grpname_len);
   if (result == NULL)
     return NULL;
 
diff --git a/libasm/asm_newsubscn.c b/libasm/asm_newsubscn.c
index 906240ac..2f2ba78e 100644
--- a/libasm/asm_newsubscn.c
+++ b/libasm/asm_newsubscn.c
@@ -62,7 +62,7 @@ asm_newsubscn (AsmScn_t *asmscn, unsigned int nr)
       runp = runp->subnext;
     }
 
-  newp = (AsmScn_t *) malloc (sizeof (AsmScn_t));
+  newp = malloc (sizeof (AsmScn_t));
   if (newp == NULL)
     return NULL;
 
diff --git a/libasm/asm_newsym.c b/libasm/asm_newsym.c
index 53891668..a89ee129 100644
--- a/libasm/asm_newsym.c
+++ b/libasm/asm_newsym.c
@@ -73,7 +73,7 @@ asm_newsym (AsmScn_t *asmscn, const char *name, GElf_Xword size,
 
   size_t name_len = strlen (name) + 1;
 
-  result = (AsmSym_t *) malloc (sizeof (AsmSym_t) + name_len);
+  result = malloc (sizeof (AsmSym_t) + name_len);
   if (result == NULL)
     return NULL;
 
diff --git a/libasm/disasm_begin.c b/libasm/disasm_begin.c
index d00852b7..cb10f66e 100644
--- a/libasm/disasm_begin.c
+++ b/libasm/disasm_begin.c
@@ -49,7 +49,7 @@ disasm_begin (Ebl *ebl, Elf *elf, DisasmGetSymCB_t symcb)
       return NULL;
     }
 
-  DisasmCtx_t *ctx = (DisasmCtx_t *) malloc (sizeof (DisasmCtx_t));
+  DisasmCtx_t *ctx = malloc (sizeof (DisasmCtx_t));
   if (ctx == NULL)
     {
       __libasm_seterrno (ASM_E_NOMEM);
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index b5462ef4..768d5c25 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,12 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* dwarf_begin_elf.c (valid_p): Remove casts of malloc return values.
+	(dwarf_begin_elf): Remove cast of calloc return value.
+	* dwarf_getpubnames.c (get_offsets): Remove casts of realloc return
+	values.
+	* dwarf_getsrclines.c (read_srclines): Remove cast of malloc return
+	value.
+
 2021-04-19  Martin Liska  <mliska@suse.cz>
 
 	* dwarf_begin_elf.c (check_section): Use startswith.
diff --git a/libdw/dwarf_begin_elf.c b/libdw/dwarf_begin_elf.c
index 9e944b86..7bde61b3 100644
--- a/libdw/dwarf_begin_elf.c
+++ b/libdw/dwarf_begin_elf.c
@@ -229,7 +229,7 @@ valid_p (Dwarf *result)
      inside the .debug_loc or .debug_loclists section.  */
   if (result != NULL && result->sectiondata[IDX_debug_loc] != NULL)
     {
-      result->fake_loc_cu = (Dwarf_CU *) malloc (sizeof (Dwarf_CU));
+      result->fake_loc_cu = malloc (sizeof (Dwarf_CU));
       if (unlikely (result->fake_loc_cu == NULL))
 	{
 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
@@ -255,7 +255,7 @@ valid_p (Dwarf *result)
 
   if (result != NULL && result->sectiondata[IDX_debug_loclists] != NULL)
     {
-      result->fake_loclists_cu = (Dwarf_CU *) malloc (sizeof (Dwarf_CU));
+      result->fake_loclists_cu = malloc (sizeof (Dwarf_CU));
       if (unlikely (result->fake_loclists_cu == NULL))
 	{
 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
@@ -286,7 +286,7 @@ valid_p (Dwarf *result)
      inside the .debug_addr section, if it exists.  */
   if (result != NULL && result->sectiondata[IDX_debug_addr] != NULL)
     {
-      result->fake_addr_cu = (Dwarf_CU *) malloc (sizeof (Dwarf_CU));
+      result->fake_addr_cu = malloc (sizeof (Dwarf_CU));
       if (unlikely (result->fake_addr_cu == NULL))
 	{
 	  Dwarf_Sig8_Hash_free (&result->sig8_hash);
@@ -415,7 +415,7 @@ dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp)
   assert (sizeof (struct Dwarf) < mem_default_size);
 
   /* Allocate the data structure.  */
-  Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf));
+  Dwarf *result = calloc (1, sizeof (Dwarf));
   if (unlikely (result == NULL)
       || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0))
     {
diff --git a/libdw/dwarf_getpubnames.c b/libdw/dwarf_getpubnames.c
index 25600f33..de726407 100644
--- a/libdw/dwarf_getpubnames.c
+++ b/libdw/dwarf_getpubnames.c
@@ -57,8 +57,7 @@ get_offsets (Dwarf *dbg)
       if (cnt >= allocated)
 	{
 	  allocated = MAX (10, 2 * allocated);
-	  struct pubnames_s *newmem
-	    = (struct pubnames_s *) realloc (mem, allocated * entsize);
+	  struct pubnames_s *newmem = realloc (mem, allocated * entsize);
 	  if (newmem == NULL)
 	    {
 	      __libdw_seterrno (DWARF_E_NOMEM);
@@ -132,7 +131,7 @@ get_offsets (Dwarf *dbg)
       return -1;
     }
 
-  dbg->pubnames_sets = (struct pubnames_s *) realloc (mem, cnt * entsize);
+  dbg->pubnames_sets = realloc (mem, cnt * entsize);
   dbg->pubnames_nsets = cnt;
 
   return 0;
diff --git a/libdw/dwarf_getsrclines.c b/libdw/dwarf_getsrclines.c
index d6a581ad..8fc48e1d 100644
--- a/libdw/dwarf_getsrclines.c
+++ b/libdw/dwarf_getsrclines.c
@@ -372,7 +372,7 @@ read_srclines (Dwarf *dbg,
     {
       if (ndirlist > SIZE_MAX / sizeof (*dirarray))
 	goto no_mem;
-      dirarray = (struct dirlist *) malloc (ndirlist * sizeof (*dirarray));
+      dirarray = malloc (ndirlist * sizeof (*dirarray));
       if (unlikely (dirarray == NULL))
 	{
 	no_mem:
diff --git a/libdwelf/ChangeLog b/libdwelf/ChangeLog
index a0ff9f4f..1abee0e8 100644
--- a/libdwelf/ChangeLog
+++ b/libdwelf/ChangeLog
@@ -1,3 +1,9 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* dwelf_strtab.c (dwelf_strtab_init): Remove cast of calloc return
+	value.
+	(morememory): Remove cast of malloc return value.
+
 2020-12-12  Dmitry V. Levin  <ldv@altlinux.org>
 
 	* libdwelf.h: Fix spelling typos in comments.
diff --git a/libdwelf/dwelf_strtab.c b/libdwelf/dwelf_strtab.c
index c6ae7cdf..5ec8c295 100644
--- a/libdwelf/dwelf_strtab.c
+++ b/libdwelf/dwelf_strtab.c
@@ -91,8 +91,7 @@ dwelf_strtab_init (bool nullstr)
       assert (sizeof (struct memoryblock) < ps - MALLOC_OVERHEAD);
     }
 
-  Dwelf_Strtab *ret
-    = (Dwelf_Strtab *) calloc (1, sizeof (struct Dwelf_Strtab));
+  Dwelf_Strtab *ret = calloc (1, sizeof (struct Dwelf_Strtab));
   if (ret != NULL)
     {
       ret->nullstr = nullstr;
@@ -117,7 +116,7 @@ morememory (Dwelf_Strtab *st, size_t len)
   /* Allocate nearest multiple of pagesize >= len.  */
   len = ((len / ps) + (len % ps != 0)) * ps - MALLOC_OVERHEAD;
 
-  struct memoryblock *newmem = (struct memoryblock *) malloc (len);
+  struct memoryblock *newmem = malloc (len);
   if (newmem == NULL)
     return 1;
 
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 1fce7af2..d35674c7 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,8 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* linux-pid-attach.c (read_cached_memory): Remove cast of malloc
+	return value.
+
 2021-06-09  Omar Sandoval  <osandov@fb.com>
 
 	* link_map.c (read_addrs): Fix potential NULL pointer dereference.
diff --git a/libdwfl/linux-pid-attach.c b/libdwfl/linux-pid-attach.c
index cd534825..09cba07b 100644
--- a/libdwfl/linux-pid-attach.c
+++ b/libdwfl/linux-pid-attach.c
@@ -135,7 +135,7 @@ read_cached_memory (struct __libdwfl_pid_arg *pid_arg,
   if (mem_cache == NULL)
     {
       size_t mem_cache_size = sizeof (struct __libdwfl_remote_mem_cache);
-      mem_cache = (struct __libdwfl_remote_mem_cache *) malloc (mem_cache_size);
+      mem_cache = malloc (mem_cache_size);
       if (mem_cache == NULL)
 	return false;
 
diff --git a/libebl/ChangeLog b/libebl/ChangeLog
index fff66b3e..da690a40 100644
--- a/libebl/ChangeLog
+++ b/libebl/ChangeLog
@@ -1,3 +1,7 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* eblopenbackend.c (openbackend): Remove cast of calloc return value.
+
 2021-04-19  Martin Liska  <mliska@suse.cz>
 
 	* eblobjnotetypename.c (ebl_object_note_type_name): Use startswith.
diff --git a/libebl/eblopenbackend.c b/libebl/eblopenbackend.c
index 71fafed7..0c07296c 100644
--- a/libebl/eblopenbackend.c
+++ b/libebl/eblopenbackend.c
@@ -274,7 +274,7 @@ openbackend (Elf *elf, const char *emulation, GElf_Half machine)
   /* First allocate the data structure for the result.  We do this
      here since this assures that the structure is always large
      enough.  */
-  result = (Ebl *) calloc (1, sizeof (Ebl));
+  result = calloc (1, sizeof (Ebl));
   if (result == NULL)
     {
       // XXX uncomment
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index f521ed3d..041da9b1 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,17 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* common.h (allocate_elf): Remove cast of calloc return value.
+	* elf_newdata.c (elf_newdata): Likewise.
+	* elf_getscn.c (elf_getscn): Remove casts of calloc return values.
+	* elf_newscn.c (elf_newscn): Likewise.
+	* elf32_updatefile.c (__elfw2): Remove casts of malloc return values.
+	* elf_getdata.c (convert_data): Likewise.
+	(__libelf_set_rawdata_wrlock): Remove cast of malloc return value.
+	* elf_begin.c (read_long_names): Remove cast of malloc return value.
+	* elf_readall.c (__libelf_readall): Likewise.
+	* elf_getarsym.c (elf_getarsym): Remove casts of malloc and realloc
+	return values.
+
 2021-07-19  Mark Wielaard  <mark@klomp.org>
 
 	* elf_strptr.c (validate_str): Check last char is zero first before
diff --git a/libelf/common.h b/libelf/common.h
index e41c717d..3718b3fd 100644
--- a/libelf/common.h
+++ b/libelf/common.h
@@ -71,7 +71,7 @@ __attribute__ ((unused))
 allocate_elf (int fildes, void *map_address, int64_t offset, size_t maxsize,
               Elf_Cmd cmd, Elf *parent, Elf_Kind kind, size_t extra)
 {
-  Elf *result = (Elf *) calloc (1, sizeof (Elf) + extra);
+  Elf *result = calloc (1, sizeof (Elf) + extra);
   if (result == NULL)
     __libelf_seterrno (ELF_E_NOMEM);
   else
diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c
index f67e6261..1ff58900 100644
--- a/libelf/elf32_updatefile.c
+++ b/libelf/elf32_updatefile.c
@@ -218,7 +218,7 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
 	return 1;
 
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
-      Elf_Scn **scns = (Elf_Scn **) malloc (shnum * sizeof (Elf_Scn *));
+      Elf_Scn **scns = malloc (shnum * sizeof (Elf_Scn *));
       if (unlikely (scns == NULL))
 	{
 	  __libelf_seterrno (ELF_E_NOMEM);
@@ -688,7 +688,7 @@ __elfw2(LIBELFBITS,updatefile) (Elf *elf, int change_bo, size_t shnum)
 
       /* Get all sections into the array and sort them.  */
       Elf_ScnList *list = &elf->state.ELFW(elf,LIBELFBITS).scns;
-      Elf_Scn **scns = (Elf_Scn **) malloc (shnum * sizeof (Elf_Scn *));
+      Elf_Scn **scns = malloc (shnum * sizeof (Elf_Scn *));
       if (unlikely (scns == NULL))
 	{
 	  free (shdr_data_mem);
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 32648c15..93d1e12f 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -774,7 +774,7 @@ read_long_names (Elf *elf)
   /* Due to the stupid format of the long name table entry (which are not
      NUL terminted) we have to provide an appropriate representation anyhow.
      Therefore we always make a copy which has the appropriate form.  */
-  newp = (char *) malloc (len);
+  newp = malloc (len);
   if (newp != NULL)
     {
       char *runp;
diff --git a/libelf/elf_getarsym.c b/libelf/elf_getarsym.c
index 1f031fca..05ebf6a9 100644
--- a/libelf/elf_getarsym.c
+++ b/libelf/elf_getarsym.c
@@ -198,7 +198,7 @@ elf_getarsym (Elf *elf, size_t *ptr)
 
       /* Now we can allocate the arrays needed to store the index.  */
       size_t ar_sym_len = (n + 1) * sizeof (Elf_Arsym);
-      elf->state.ar.ar_sym = (Elf_Arsym *) malloc (ar_sym_len);
+      elf->state.ar.ar_sym = malloc (ar_sym_len);
       if (elf->state.ar.ar_sym != NULL)
 	{
 	  void *file_data; /* unit32_t[n] or uint64_t[n] */
@@ -216,8 +216,7 @@ elf_getarsym (Elf *elf, size_t *ptr)
 	      file_data = temp_data;
 
 	      ar_sym_len += index_size - n * w;
-	      Elf_Arsym *newp = (Elf_Arsym *) realloc (elf->state.ar.ar_sym,
-						       ar_sym_len);
+	      Elf_Arsym *newp = realloc (elf->state.ar.ar_sym, ar_sym_len);
 	      if (newp == NULL)
 		{
 		  free (elf->state.ar.ar_sym);
diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
index 3cad29de..475c6ded 100644
--- a/libelf/elf_getdata.c
+++ b/libelf/elf_getdata.c
@@ -146,7 +146,7 @@ convert_data (Elf_Scn *scn, int eclass,
 	scn->data_base = scn->rawdata_base;
       else
 	{
-	  scn->data_base = (char *) malloc (size);
+	  scn->data_base = malloc (size);
 	  if (scn->data_base == NULL)
 	    {
 	      __libelf_seterrno (ELF_E_NOMEM);
@@ -161,7 +161,7 @@ convert_data (Elf_Scn *scn, int eclass,
     {
       xfct_t fp;
 
-      scn->data_base = (char *) malloc (size);
+      scn->data_base = malloc (size);
       if (scn->data_base == NULL)
 	{
 	  __libelf_seterrno (ELF_E_NOMEM);
@@ -175,7 +175,7 @@ convert_data (Elf_Scn *scn, int eclass,
 	rawdata_source = scn->rawdata_base;
       else
 	{
-	  rawdata_source = (char *) malloc (size);
+	  rawdata_source = malloc (size);
 	  if (rawdata_source == NULL)
 	    {
 	      __libelf_seterrno (ELF_E_NOMEM);
@@ -328,8 +328,7 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
 
 	  /* We have to read the data from the file.  Allocate the needed
 	     memory.  */
-	  scn->rawdata_base = scn->rawdata.d.d_buf
-	    = (char *) malloc (size);
+	  scn->rawdata_base = scn->rawdata.d.d_buf = malloc (size);
 	  if (scn->rawdata.d.d_buf == NULL)
 	    {
 	      __libelf_seterrno (ELF_E_NOMEM);
diff --git a/libelf/elf_getscn.c b/libelf/elf_getscn.c
index e1fbaaaa..be9c76f0 100644
--- a/libelf/elf_getscn.c
+++ b/libelf/elf_getscn.c
@@ -68,7 +68,7 @@ elf_getscn (Elf *elf, size_t idx)
       Elf_Scn *scn0 = &runp->data[0];
       if (elf->class == ELFCLASS32)
 	{
-	  scn0->shdr.e32 = (Elf32_Shdr *) calloc (1, sizeof (Elf32_Shdr));
+	  scn0->shdr.e32 = calloc (1, sizeof (Elf32_Shdr));
 	  if (scn0->shdr.e32 == NULL)
 	    {
 	      __libelf_seterrno (ELF_E_NOMEM);
@@ -77,7 +77,7 @@ elf_getscn (Elf *elf, size_t idx)
 	}
       else
 	{
-	  scn0->shdr.e64 = (Elf64_Shdr *) calloc (1, sizeof (Elf64_Shdr));
+	  scn0->shdr.e64 = calloc (1, sizeof (Elf64_Shdr));
 	  if (scn0->shdr.e64 == NULL)
 	    {
 	      __libelf_seterrno (ELF_E_NOMEM);
diff --git a/libelf/elf_newdata.c b/libelf/elf_newdata.c
index 896f22cd..0063d599 100644
--- a/libelf/elf_newdata.c
+++ b/libelf/elf_newdata.c
@@ -106,7 +106,7 @@ elf_newdata (Elf_Scn *scn)
 	}
 
       /* Create a new, empty data descriptor.  */
-      result = (Elf_Data_List *) calloc (1, sizeof (Elf_Data_List));
+      result = calloc (1, sizeof (Elf_Data_List));
       if (result == NULL)
 	{
 	  __libelf_seterrno (ELF_E_NOMEM);
diff --git a/libelf/elf_newscn.c b/libelf/elf_newscn.c
index d15a642e..d6bdf153 100644
--- a/libelf/elf_newscn.c
+++ b/libelf/elf_newscn.c
@@ -94,9 +94,9 @@ elf_newscn (Elf *elf)
 	  1
 #endif
 	  )
-      newp = (Elf_ScnList *) calloc (sizeof (Elf_ScnList)
-				     + ((elf->state.elf.scnincr *= 2)
-					* sizeof (Elf_Scn)), 1);
+      newp = calloc (sizeof (Elf_ScnList)
+		     + ((elf->state.elf.scnincr *= 2)
+			* sizeof (Elf_Scn)), 1);
       if (newp == NULL)
 	{
 	  __libelf_seterrno (ELF_E_NOMEM);
@@ -122,7 +122,7 @@ elf_newscn (Elf *elf)
   /* Create a section header for this section.  */
   if (elf->class == ELFCLASS32)
     {
-      result->shdr.e32 = (Elf32_Shdr *) calloc (1, sizeof (Elf32_Shdr));
+      result->shdr.e32 = calloc (1, sizeof (Elf32_Shdr));
       if (result->shdr.e32 == NULL)
 	{
 	  __libelf_seterrno (ELF_E_NOMEM);
@@ -131,7 +131,7 @@ elf_newscn (Elf *elf)
     }
   else
     {
-      result->shdr.e64 = (Elf64_Shdr *) calloc (1, sizeof (Elf64_Shdr));
+      result->shdr.e64 = calloc (1, sizeof (Elf64_Shdr));
       if (result->shdr.e64 == NULL)
 	{
 	  __libelf_seterrno (ELF_E_NOMEM);
diff --git a/libelf/elf_readall.c b/libelf/elf_readall.c
index 384d2512..0a3a233d 100644
--- a/libelf/elf_readall.c
+++ b/libelf/elf_readall.c
@@ -107,7 +107,7 @@ __libelf_readall (Elf *elf)
 	}
 
       /* Allocate all the memory we need.  */
-      mem = (char *) malloc (elf->maximum_size);
+      mem = malloc (elf->maximum_size);
       if (mem != NULL)
 	{
 	  /* Read the file content.  */
diff --git a/src/ChangeLog b/src/ChangeLog
index b729eaa4..c008fb51 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,16 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* elflint.c (check_sections): Remove cast of xcalloc return value.
+	* findtextrel.c (process_file): Remove casts of malloc and realloc
+	return values.
+	* nm.c (get_local_names, show_symbols_sysv, show_symbols): Remove
+	casts of xmalloc return values.
+	* readelf.c (print_hash_info, handle_sysv_hash, handle_sysv_hash64,
+	handle_gnu_hash): Remove cast of xcalloc return value.
+	(print_debug_units): Remove casts of xmalloc return values.
+	* strip.c (handle_elf): Remove casts of xcalloc and xmalloc return
+	values.
+
 2021-08-20  Saleem Abdulrasool  <abdulras@google.com>
 
 	* elfclassify.c: Remove error.h include.
diff --git a/src/elflint.c b/src/elflint.c
index 35b40500..1ce75684 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -3705,7 +3705,7 @@ check_sections (Ebl *ebl, GElf_Ehdr *ehdr)
     return;
 
   /* Allocate array to count references in section groups.  */
-  scnref = (int *) xcalloc (shnum, sizeof (int));
+  scnref = xcalloc (shnum, sizeof (int));
 
   /* Check the zeroth section first.  It must not have any contents
      and the section header must contain nonzero value at most in the
diff --git a/src/findtextrel.c b/src/findtextrel.c
index 220ee909..5d479b51 100644
--- a/src/findtextrel.c
+++ b/src/findtextrel.c
@@ -304,8 +304,7 @@ process_file (const char *fname, bool more_than_one)
   /* Get the address ranges for the loaded segments.  */
   size_t nsegments_max = 10;
   size_t nsegments = 0;
-  struct segments *segments
-    = (struct segments *) malloc (nsegments_max * sizeof (segments[0]));
+  struct segments *segments = malloc (nsegments_max * sizeof (segments[0]));
   if (segments == NULL)
     error (1, errno, _("while reading ELF file"));
 
@@ -334,9 +333,7 @@ process_file (const char *fname, bool more_than_one)
 	    {
 	      nsegments_max *= 2;
 	      segments
-		= (struct segments *) realloc (segments,
-					       nsegments_max
-					       * sizeof (segments[0]));
+		= realloc (segments, nsegments_max * sizeof (segments[0]));
 	      if (segments == NULL)
 		{
 		  error (0, 0, _("\
diff --git a/src/nm.c b/src/nm.c
index b99805e8..2ae29c4d 100644
--- a/src/nm.c
+++ b/src/nm.c
@@ -687,8 +687,7 @@ get_local_names (Dwarf *dbg)
 	      }
 
 	    /* We have all the information.  Create a record.  */
-	    struct local_name *newp
-	      = (struct local_name *) xmalloc (sizeof (*newp));
+	    struct local_name *newp = xmalloc (sizeof (*newp));
 	    newp->name = name;
 	    newp->file = dwarf_filesrc (files, fileidx, NULL, NULL);
 	    newp->lineno = lineno;
@@ -736,7 +735,7 @@ show_symbols_sysv (Ebl *ebl, GElf_Word strndx, const char *fullname,
   bool scnnames_malloced = shnum * sizeof (const char *) > 128 * 1024;
   const char **scnnames;
   if (scnnames_malloced)
-    scnnames = (const char **) xmalloc (sizeof (const char *) * shnum);
+    scnnames = xmalloc (sizeof (const char *) * shnum);
   else
     scnnames = (const char **) alloca (sizeof (const char *) * shnum);
   /* Get the section header string table index.  */
@@ -1340,7 +1339,7 @@ show_symbols (int fd, Ebl *ebl, GElf_Ehdr *ehdr,
   if (nentries * sizeof (GElf_SymX) < MAX_STACK_ALLOC)
     sym_mem = (GElf_SymX *) alloca (nentries * sizeof (GElf_SymX));
   else
-    sym_mem = (GElf_SymX *) xmalloc (nentries * sizeof (GElf_SymX));
+    sym_mem = xmalloc (nentries * sizeof (GElf_SymX));
 
   /* Iterate over all symbols.  */
 #ifdef USE_DEMANGLE
diff --git a/src/readelf.c b/src/readelf.c
index 8191bde2..1e3ad43d 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -3165,7 +3165,7 @@ print_hash_info (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx,
 		 uint_fast32_t maxlength, Elf32_Word nbucket,
 		 uint_fast32_t nsyms, uint32_t *lengths, const char *extrastr)
 {
-  uint32_t *counts = (uint32_t *) xcalloc (maxlength + 1, sizeof (uint32_t));
+  uint32_t *counts = xcalloc (maxlength + 1, sizeof (uint32_t));
 
   for (Elf32_Word cnt = 0; cnt < nbucket; ++cnt)
     ++counts[lengths[cnt]];
@@ -3266,7 +3266,7 @@ handle_sysv_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
   Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[2];
   Elf32_Word *chain = &((Elf32_Word *) data->d_buf)[2 + nbucket];
 
-  uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
+  uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
 
   uint_fast32_t maxlength = 0;
   uint_fast32_t nsyms = 0;
@@ -3332,7 +3332,7 @@ handle_sysv_hash64 (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
   Elf64_Xword *bucket = &((Elf64_Xword *) data->d_buf)[2];
   Elf64_Xword *chain = &((Elf64_Xword *) data->d_buf)[2 + nbucket];
 
-  uint32_t *lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
+  uint32_t *lengths = xcalloc (nbucket, sizeof (uint32_t));
 
   uint_fast32_t maxlength = 0;
   uint_fast32_t nsyms = 0;
@@ -3410,7 +3410,7 @@ handle_gnu_hash (Ebl *ebl, Elf_Scn *scn, GElf_Shdr *shdr, size_t shstrndx)
   if (used_buf > data->d_size)
     goto invalid_data;
 
-  lengths = (uint32_t *) xcalloc (nbucket, sizeof (uint32_t));
+  lengths = xcalloc (nbucket, sizeof (uint32_t));
 
   Elf32_Word *bitmask = &((Elf32_Word *) data->d_buf)[4];
   Elf32_Word *bucket = &((Elf32_Word *) data->d_buf)[4 + bitmask_words];
@@ -7744,7 +7744,7 @@ print_debug_units (Dwfl_Module *dwflmod,
     return;
 
   int maxdies = 20;
-  Dwarf_Die *dies = (Dwarf_Die *) xmalloc (maxdies * sizeof (Dwarf_Die));
+  Dwarf_Die *dies = xmalloc (maxdies * sizeof (Dwarf_Die));
 
   /* New compilation unit.  */
   Dwarf_Half version;
@@ -7916,9 +7916,7 @@ print_debug_units (Dwfl_Module *dwflmod,
 
       /* Make room for the next level's DIE.  */
       if (level + 1 == maxdies)
-	dies = (Dwarf_Die *) xrealloc (dies,
-				       (maxdies += 10)
-				       * sizeof (Dwarf_Die));
+	dies = xrealloc (dies, (maxdies += 10) * sizeof (Dwarf_Die));
 
       int res = dwarf_child (&dies[level], &dies[level + 1]);
       if (res > 0)
diff --git a/src/strip.c b/src/strip.c
index 961b9db5..d5b753d7 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -1062,7 +1062,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 	 the debug file if the file would not contain any
 	 information.  */
       size_t debug_fname_len = strlen (debug_fname);
-      tmp_debug_fname = (char *) xmalloc (debug_fname_len + sizeof (".XXXXXX"));
+      tmp_debug_fname = xmalloc (debug_fname_len + sizeof (".XXXXXX"));
       strcpy (mempcpy (tmp_debug_fname, debug_fname, debug_fname_len),
 	      ".XXXXXX");
 
@@ -1196,8 +1196,7 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
      table.  Maybe some weird tool created an ELF file without one.
      The other one is used for the debug link section.  */
   if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
-    shdr_info = (struct shdr_info *) xcalloc (shnum + 2,
-					      sizeof (struct shdr_info));
+    shdr_info = xcalloc (shnum + 2, sizeof (struct shdr_info));
   else
     {
       shdr_info = (struct shdr_info *) alloca ((shnum + 2)
@@ -1980,8 +1979,8 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
 		  }
 
 		shdr_info[cnt].newsymidx
-		  = (Elf32_Word *) xcalloc (shdr_info[cnt].data->d_size
-					    / elsize, sizeof (Elf32_Word));
+		  = xcalloc (shdr_info[cnt].data->d_size / elsize,
+			     sizeof (Elf32_Word));
 
 		bool last_was_local = true;
 		size_t destidx;
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 178697bb..e0999bac 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,7 @@
+2021-09-06  Dmitry V. Levin  <ldv@altlinux.org>
+
+	* elfcopy.c (copy_elf): Remove cast of malloc return value.
+
 2021-09-03  Mark Wielaard  <mark@klomp.org>
 
 	* run-debuginfod-000-permission.sh: Set DEBUGINFOD_CACHE_PATH
diff --git a/tests/elfcopy.c b/tests/elfcopy.c
index 4542222e..10b2d362 100644
--- a/tests/elfcopy.c
+++ b/tests/elfcopy.c
@@ -194,7 +194,7 @@ copy_elf (const char *in, const char *out, bool use_mmap, bool reverse_offs)
 	  exit (1);
 	}
 
-      offs = (GElf_Off *) malloc (shnum * sizeof (GElf_Off));
+      offs = malloc (shnum * sizeof (GElf_Off));
       if (offs == NULL)
 	{
 	  printf ("couldn't allocate memory for offs\n");
-- 
ldv


More information about the Elfutils-devel mailing list