public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Avoid signed/unsigned comparison
@ 2017-04-20 14:40 Ulf Hermann
  2017-04-27 22:35 ` Mark Wielaard
  0 siblings, 1 reply; 3+ messages in thread
From: Ulf Hermann @ 2017-04-20 14:40 UTC (permalink / raw)
  To: elfutils-devel

Some compilers implicitly cast the result of uint_fast16_t *
uint_fast16_t to something signed and then complain about the
comparison to (unsigned) size_t.

Casting phnum to size_t is a good idea anyway as 16bit multiplication
can easily overflow and we are not checking for this.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 libdwfl/ChangeLog         | 4 ++++
 libdwfl/elf-from-memory.c | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index cddafe2..c9bd4f0 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,5 +1,9 @@
 2017-04-20  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* elf-from-memory.c: Explicitly cast phnum to size_t.
+
+2017-04-20  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* libdwflP.h: Don't include config.h.
 	* argp-std.c: Include config.h.
 	* cu.c: Likewise.
diff --git a/libdwfl/elf-from-memory.c b/libdwfl/elf-from-memory.c
index dd42e95..12a0a1b 100644
--- a/libdwfl/elf-from-memory.c
+++ b/libdwfl/elf-from-memory.c
@@ -172,7 +172,7 @@ elf_from_remote_memory (GElf_Addr ehdr_vma,
     {
       /* Read in the program headers.  */
 
-      if (initial_bufsize < phnum * phentsize)
+      if (initial_bufsize < (size_t)phnum * phentsize)
 	{
 	  unsigned char *newbuf = realloc (buffer, phnum * phentsize);
 	  if (newbuf == NULL)
-- 
2.1.4

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Avoid signed/unsigned comparison
  2017-04-20 14:40 [PATCH] Avoid signed/unsigned comparison Ulf Hermann
@ 2017-04-27 22:35 ` Mark Wielaard
  2017-04-28 10:35   ` Josh Stone
  0 siblings, 1 reply; 3+ messages in thread
From: Mark Wielaard @ 2017-04-27 22:35 UTC (permalink / raw)
  To: Ulf Hermann; +Cc: elfutils-devel

On Thu, Apr 20, 2017 at 04:40:30PM +0200, Ulf Hermann wrote:
> Some compilers implicitly cast the result of uint_fast16_t *
> uint_fast16_t to something signed and then complain about the
> comparison to (unsigned) size_t.

Really? That is allowed? Using a signed type for uint_fast16_t?

> Casting phnum to size_t is a good idea anyway as 16bit multiplication
> can easily overflow and we are not checking for this.

OK, that seems an ok enough reason.
Applied to master.

Thanks,

Mark

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Avoid signed/unsigned comparison
  2017-04-27 22:35 ` Mark Wielaard
@ 2017-04-28 10:35   ` Josh Stone
  0 siblings, 0 replies; 3+ messages in thread
From: Josh Stone @ 2017-04-28 10:35 UTC (permalink / raw)
  To: elfutils-devel

On 04/27/2017 11:24 AM, Mark Wielaard wrote:
> On Thu, Apr 20, 2017 at 04:40:30PM +0200, Ulf Hermann wrote:
>> Some compilers implicitly cast the result of uint_fast16_t *
>> uint_fast16_t to something signed and then complain about the
>> comparison to (unsigned) size_t.
> 
> Really? That is allowed? Using a signed type for uint_fast16_t?

I think integer promotion (which happens before the operation) may use a
signed int.  It has to preserve the sign of the value itself, but I
think not necessarily the signedness of the resulting type.

Glibc uses "unsigned int"/"unsigned long int" for uint_fast16_t on
32/64-bit platforms, which means you won't get integer promotion.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-04-28  0:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-20 14:40 [PATCH] Avoid signed/unsigned comparison Ulf Hermann
2017-04-27 22:35 ` Mark Wielaard
2017-04-28 10:35   ` Josh Stone

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).