diff mbox series

[v2,02/12] elf: Compile with -Wextra

Message ID 20210209014457.108301-3-aik@ozlabs.ru
State Accepted
Headers show
Series Compile with -Wextra | expand

Commit Message

Alexey Kardashevskiy Feb. 9, 2021, 1:44 a.m. UTC
-Wextra enables a bunch of rather useful checks which this fixes.

This changes the return value for the case when no ELF headers were found
to avoid (ugly-ish) cast of -1 to unsigned.

Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---
Changes:
v2:
* cast the return value to (int)
* added missing (long)
---
 include/libelf.h   | 6 +++---
 lib/libelf/elf.c   | 2 +-
 lib/libelf/elf32.c | 6 +++---
 lib/libelf/elf64.c | 8 ++++----
 4 files changed, 11 insertions(+), 11 deletions(-)

Comments

Thomas Huth Feb. 10, 2021, 8:08 a.m. UTC | #1
On 09/02/2021 02.44, Alexey Kardashevskiy wrote:
> -Wextra enables a bunch of rather useful checks which this fixes.
> 
> This changes the return value for the case when no ELF headers were found
> to avoid (ugly-ish) cast of -1 to unsigned.
> 
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
> ---
> Changes:
> v2:
> * cast the return value to (int)
> * added missing (long)
> ---
>   include/libelf.h   | 6 +++---
>   lib/libelf/elf.c   | 2 +-
>   lib/libelf/elf32.c | 6 +++---
>   lib/libelf/elf64.c | 8 ++++----
>   4 files changed, 11 insertions(+), 11 deletions(-)
> 
> diff --git a/include/libelf.h b/include/libelf.h
> index 48ff4d7beb68..29a4d049a096 100644
> --- a/include/libelf.h
> +++ b/include/libelf.h
> @@ -96,9 +96,9 @@ void elf_relocate64(void *file_addr, signed long offset);
>   
>   int elf_forth_claim(void *addr, long size);
>   
> -long elf_get_file_size(const void *buffer, const long buffer_size);
> -long elf_get_file_size32(const void *buffer, const long buffer_size);
> -long elf_get_file_size64(const void *buffer, const long buffer_size);
> +long elf_get_file_size(const void *buffer, const unsigned long buffer_size);
> +long elf_get_file_size32(const void *buffer, const unsigned long buffer_size);
> +long elf_get_file_size64(const void *buffer, const unsigned long buffer_size);
>   
>   #ifdef __BIG_ENDIAN__
>   #define elf64_to_cpu(x, ehdr) ((ehdr)->ei_data == ELFDATA2MSB ? (x) : bswap_64(x))
> diff --git a/lib/libelf/elf.c b/lib/libelf/elf.c
> index d3684545aef7..f6a052ef3c9e 100644
> --- a/lib/libelf/elf.c
> +++ b/lib/libelf/elf.c
> @@ -202,7 +202,7 @@ elf_get_base_addr(void *file_addr)
>    * buffer larger than the size of the file
>    * @return  The size of the ELF image or < 0 for error
>    */
> -long elf_get_file_size(const void *buffer, const long buffer_size)
> +long elf_get_file_size(const void *buffer, const unsigned long buffer_size)
>   {
>   	const struct ehdr *ehdr = (const struct ehdr *)buffer;
>   
> diff --git a/lib/libelf/elf32.c b/lib/libelf/elf32.c
> index 64ea386a96da..45b0015b87ab 100644
> --- a/lib/libelf/elf32.c
> +++ b/lib/libelf/elf32.c
> @@ -212,13 +212,13 @@ elf_byteswap_header32(void *file_addr)
>    * file.
>    * @return Return -1 on error, size of file otherwise.
>    */
> -long elf_get_file_size32(const void *buffer, const long buffer_size)
> +long elf_get_file_size32(const void *buffer, const unsigned long buffer_size)
>   {
>   	const struct ehdr32 *ehdr = (const struct ehdr32 *) buffer;
>   	const uint8_t *buffer_end = buffer + buffer_size;
>   	const struct phdr32 *phdr;
>   	const struct shdr32 *shdr;
> -	long elf_size = -1;
> +	unsigned long elf_size = 0;
>   	uint16_t entsize;
>   	unsigned i;
>   
> @@ -258,5 +258,5 @@ long elf_get_file_size32(const void *buffer, const long buffer_size)
>   	if (elf_size > buffer_size)
>   		return -1;
>   
> -	return elf_size;
> +	return (long) elf_size;
>   }
> diff --git a/lib/libelf/elf64.c b/lib/libelf/elf64.c
> index 0f302679f784..3bc40402fb8a 100644
> --- a/lib/libelf/elf64.c
> +++ b/lib/libelf/elf64.c
> @@ -389,7 +389,7 @@ elf_apply_all_rela64(void *file_addr, signed long offset, struct shdr64 *shdrs,
>   	struct rela *relaentry;
>   	struct sym64 *symtabentry;
>   	uint32_t symbolidx;
> -	int i;
> +	unsigned i;
>   
>   	/* If the referenced section has not been allocated, then it has
>   	 * not been loaded and thus does not need to be relocated. */
> @@ -481,13 +481,13 @@ uint32_t elf_get_eflags_64(void *file_addr)
>    * file.
>    * @return Return -1 on error, size of file otherwise.
>    */
> -long elf_get_file_size64(const void *buffer, const long buffer_size)
> +long elf_get_file_size64(const void *buffer, const unsigned long buffer_size)
>   {
>   	const struct ehdr64 *ehdr = (const struct ehdr64 *) buffer;
>   	const uint8_t *buffer_end = buffer + buffer_size;
>   	const struct phdr64 *phdr;
>   	const struct shdr64 *shdr;
> -	long elf_size = -1;
> +	unsigned long elf_size = 0;
>   	uint16_t entsize;
>   	unsigned i;
>   
> @@ -527,5 +527,5 @@ long elf_get_file_size64(const void *buffer, const long buffer_size)
>   	if (elf_size > buffer_size)
>   		return -1;
>   
> -	return elf_size;
> +	return (long) elf_size;
>   }
> 

Reviewed-by: Thomas Huth <thuth@redhat.com>
diff mbox series

Patch

diff --git a/include/libelf.h b/include/libelf.h
index 48ff4d7beb68..29a4d049a096 100644
--- a/include/libelf.h
+++ b/include/libelf.h
@@ -96,9 +96,9 @@  void elf_relocate64(void *file_addr, signed long offset);
 
 int elf_forth_claim(void *addr, long size);
 
-long elf_get_file_size(const void *buffer, const long buffer_size);
-long elf_get_file_size32(const void *buffer, const long buffer_size);
-long elf_get_file_size64(const void *buffer, const long buffer_size);
+long elf_get_file_size(const void *buffer, const unsigned long buffer_size);
+long elf_get_file_size32(const void *buffer, const unsigned long buffer_size);
+long elf_get_file_size64(const void *buffer, const unsigned long buffer_size);
 
 #ifdef __BIG_ENDIAN__
 #define elf64_to_cpu(x, ehdr) ((ehdr)->ei_data == ELFDATA2MSB ? (x) : bswap_64(x))
diff --git a/lib/libelf/elf.c b/lib/libelf/elf.c
index d3684545aef7..f6a052ef3c9e 100644
--- a/lib/libelf/elf.c
+++ b/lib/libelf/elf.c
@@ -202,7 +202,7 @@  elf_get_base_addr(void *file_addr)
  * buffer larger than the size of the file
  * @return  The size of the ELF image or < 0 for error
  */
-long elf_get_file_size(const void *buffer, const long buffer_size)
+long elf_get_file_size(const void *buffer, const unsigned long buffer_size)
 {
 	const struct ehdr *ehdr = (const struct ehdr *)buffer;
 
diff --git a/lib/libelf/elf32.c b/lib/libelf/elf32.c
index 64ea386a96da..45b0015b87ab 100644
--- a/lib/libelf/elf32.c
+++ b/lib/libelf/elf32.c
@@ -212,13 +212,13 @@  elf_byteswap_header32(void *file_addr)
  * file.
  * @return Return -1 on error, size of file otherwise.
  */
-long elf_get_file_size32(const void *buffer, const long buffer_size)
+long elf_get_file_size32(const void *buffer, const unsigned long buffer_size)
 {
 	const struct ehdr32 *ehdr = (const struct ehdr32 *) buffer;
 	const uint8_t *buffer_end = buffer + buffer_size;
 	const struct phdr32 *phdr;
 	const struct shdr32 *shdr;
-	long elf_size = -1;
+	unsigned long elf_size = 0;
 	uint16_t entsize;
 	unsigned i;
 
@@ -258,5 +258,5 @@  long elf_get_file_size32(const void *buffer, const long buffer_size)
 	if (elf_size > buffer_size)
 		return -1;
 
-	return elf_size;
+	return (long) elf_size;
 }
diff --git a/lib/libelf/elf64.c b/lib/libelf/elf64.c
index 0f302679f784..3bc40402fb8a 100644
--- a/lib/libelf/elf64.c
+++ b/lib/libelf/elf64.c
@@ -389,7 +389,7 @@  elf_apply_all_rela64(void *file_addr, signed long offset, struct shdr64 *shdrs,
 	struct rela *relaentry;
 	struct sym64 *symtabentry;
 	uint32_t symbolidx;
-	int i;
+	unsigned i;
 
 	/* If the referenced section has not been allocated, then it has
 	 * not been loaded and thus does not need to be relocated. */
@@ -481,13 +481,13 @@  uint32_t elf_get_eflags_64(void *file_addr)
  * file.
  * @return Return -1 on error, size of file otherwise.
  */
-long elf_get_file_size64(const void *buffer, const long buffer_size)
+long elf_get_file_size64(const void *buffer, const unsigned long buffer_size)
 {
 	const struct ehdr64 *ehdr = (const struct ehdr64 *) buffer;
 	const uint8_t *buffer_end = buffer + buffer_size;
 	const struct phdr64 *phdr;
 	const struct shdr64 *shdr;
-	long elf_size = -1;
+	unsigned long elf_size = 0;
 	uint16_t entsize;
 	unsigned i;
 
@@ -527,5 +527,5 @@  long elf_get_file_size64(const void *buffer, const long buffer_size)
 	if (elf_size > buffer_size)
 		return -1;
 
-	return elf_size;
+	return (long) elf_size;
 }