linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Fix compilation warning on 64 bits platforms
@ 2007-05-21  0:04 Damien Lespiau
  2007-05-21  0:28 ` Josh Triplett
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Lespiau @ 2007-05-21  0:04 UTC (permalink / raw)
  To: linux-sparse; +Cc: Damien Lespiau

Fix: format '%d' expects type 'int', but argument 2 has type 'long int'

Signed-off-by: Damien Lespiau <damien.lespiau@gmail.com>
---
 linearize.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/linearize.c b/linearize.c
index d428d92..6c9fae4 100644
--- a/linearize.c
+++ b/linearize.c
@@ -461,7 +461,7 @@ const char *show_instruction(struct instruction *insn)
 	}
 
 	if (buf >= buffer + sizeof(buffer))
-		die("instruction buffer overflowed %d\n", buf - buffer);
+		die("instruction buffer overflowed %d\n", (int)(buf - buffer));
 	do { --buf; } while (*buf == ' ');
 	*++buf = 0;
 	return buffer;
-- 
1.5.2.rc3.87.g404fd

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

* Re: [PATCH] Fix compilation warning on 64 bits platforms
  2007-05-21  0:04 [PATCH] Fix compilation warning on 64 bits platforms Damien Lespiau
@ 2007-05-21  0:28 ` Josh Triplett
  2007-05-21  0:33   ` Al Viro
  0 siblings, 1 reply; 6+ messages in thread
From: Josh Triplett @ 2007-05-21  0:28 UTC (permalink / raw)
  To: Damien Lespiau; +Cc: linux-sparse

[-- Attachment #1: Type: text/plain, Size: 1244 bytes --]

Damien Lespiau wrote:
> Fix: format '%d' expects type 'int', but argument 2 has type 'long int'
[...]
> @@ -461,7 +461,7 @@ const char *show_instruction(struct instruction *insn)
>  	}
>  
>  	if (buf >= buffer + sizeof(buffer))
> -		die("instruction buffer overflowed %d\n", buf - buffer);
> +		die("instruction buffer overflowed %d\n", (int)(buf - buffer));

A cast doesn't seem like the right fix.  The difference between two pointers
has type ptrdiff_t.  sizeof(ptrdiff_t) == 8 on 64-bit platforms, leading to
the legitimate warning you saw.  This cast would truncate the difference to 32
bits.  glibc supplies a "t" length modifier for ptrdiff_t, but I don't think
sparse can't portably use that.  I don't think we can portably use %llu
either, even though we use long long.  On the other hand, sparse already seems
to use %llu.

Obviously 32-bit overflow seems unlikely here, but I don't like using a cast
to shut GCC up when it has a legitimate complaint; I'd prefer to have the
right fix.

The ideal fix, so we don't have to worry about printf format-string
portability: change this function to cleanly *prevent* instruction buffer
overflows rather than detecting them after the fact. :)

- Josh Triplett



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

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

* Re: [PATCH] Fix compilation warning on 64 bits platforms
  2007-05-21  0:28 ` Josh Triplett
@ 2007-05-21  0:33   ` Al Viro
  2007-05-21  0:36     ` Josh Triplett
  0 siblings, 1 reply; 6+ messages in thread
From: Al Viro @ 2007-05-21  0:33 UTC (permalink / raw)
  To: Josh Triplett; +Cc: Damien Lespiau, linux-sparse

On Sun, May 20, 2007 at 05:28:37PM -0700, Josh Triplett wrote:
> Damien Lespiau wrote:
> > Fix: format '%d' expects type 'int', but argument 2 has type 'long int'
> [...]
> > @@ -461,7 +461,7 @@ const char *show_instruction(struct instruction *insn)
> >  	}
> >  
> >  	if (buf >= buffer + sizeof(buffer))
> > -		die("instruction buffer overflowed %d\n", buf - buffer);
> > +		die("instruction buffer overflowed %d\n", (int)(buf - buffer));
> 
> A cast doesn't seem like the right fix.  The difference between two pointers
> has type ptrdiff_t.  sizeof(ptrdiff_t) == 8 on 64-bit platforms, leading to
> the legitimate warning you saw.  This cast would truncate the difference to 32
> bits.  glibc supplies a "t" length modifier for ptrdiff_t, but I don't think
> sparse can't portably use that.

Not just glibc; it's in C99.  So yes, I'd say we should use %td here.

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

* Re: [PATCH] Fix compilation warning on 64 bits platforms
  2007-05-21  0:33   ` Al Viro
@ 2007-05-21  0:36     ` Josh Triplett
  2007-05-22 18:06       ` Damien Lespiau
  0 siblings, 1 reply; 6+ messages in thread
From: Josh Triplett @ 2007-05-21  0:36 UTC (permalink / raw)
  To: Al Viro; +Cc: Damien Lespiau, linux-sparse

[-- Attachment #1: Type: text/plain, Size: 973 bytes --]

Al Viro wrote:
> On Sun, May 20, 2007 at 05:28:37PM -0700, Josh Triplett wrote:
>> Damien Lespiau wrote:
>>> Fix: format '%d' expects type 'int', but argument 2 has type 'long int'
>> [...]
>>> @@ -461,7 +461,7 @@ const char *show_instruction(struct instruction *insn)
>>>  	}
>>>  
>>>  	if (buf >= buffer + sizeof(buffer))
>>> -		die("instruction buffer overflowed %d\n", buf - buffer);
>>> +		die("instruction buffer overflowed %d\n", (int)(buf - buffer));
>> A cast doesn't seem like the right fix.  The difference between two pointers
>> has type ptrdiff_t.  sizeof(ptrdiff_t) == 8 on 64-bit platforms, leading to
>> the legitimate warning you saw.  This cast would truncate the difference to 32
>> bits.  glibc supplies a "t" length modifier for ptrdiff_t, but I don't think
>> sparse can't portably use that.
> 
> Not just glibc; it's in C99.  So yes, I'd say we should use %td here.

Ah, perfect.  Yes, we should use %td.

- Josh Triplett



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

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

* Re: [PATCH] Fix compilation warning on 64 bits platforms
  2007-05-21  0:36     ` Josh Triplett
@ 2007-05-22 18:06       ` Damien Lespiau
  2007-05-22 21:20         ` Josh Triplett
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Lespiau @ 2007-05-22 18:06 UTC (permalink / raw)
  To: Josh Triplett; +Cc: Al Viro, linux-sparse

> Al Viro wrote:
> > Not just glibc; it's in C99.  So yes, I'd say we should use %td here.

Much better than my ugly cast. I feel stupid to "sign" a one letter patch
that is not mine, but I guess you expect me to resend it.

-- 
Damien

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

* Re: [PATCH] Fix compilation warning on 64 bits platforms
  2007-05-22 18:06       ` Damien Lespiau
@ 2007-05-22 21:20         ` Josh Triplett
  0 siblings, 0 replies; 6+ messages in thread
From: Josh Triplett @ 2007-05-22 21:20 UTC (permalink / raw)
  To: Damien Lespiau; +Cc: Al Viro, linux-sparse

[-- Attachment #1: Type: text/plain, Size: 1205 bytes --]

Damien Lespiau wrote:
>> Al Viro wrote:
>>> Not just glibc; it's in C99.  So yes, I'd say we should use %td here.
> 
> Much better than my ugly cast. I feel stupid to "sign" a one letter patch
> that is not mine, but I guess you expect me to resend it.

Don't worry about it.  I went ahead and made the change:

From 3195f83203cfd6d05cd56829c02c16b5dc5c274d Mon Sep 17 00:00:00 2001
From: Josh Triplett <josh@freedesktop.org>
Date: Tue, 22 May 2007 14:18:28 -0700
Subject: [PATCH] Use %td when printing a ptrdiff_t to avoid problems on 64-bit platforms

Thanks to Damien Lespiau for reporting the problem.

Signed-off-by: Josh Triplett <josh@freedesktop.org>
---
 linearize.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/linearize.c b/linearize.c
index d428d92..c38dd7d 100644
--- a/linearize.c
+++ b/linearize.c
@@ -461,7 +461,7 @@ const char *show_instruction(struct instruction *insn)
 	}
 
 	if (buf >= buffer + sizeof(buffer))
-		die("instruction buffer overflowed %d\n", buf - buffer);
+		die("instruction buffer overflowed %td\n", buf - buffer);
 	do { --buf; } while (*buf == ' ');
 	*++buf = 0;
 	return buffer;

- Josh Triplett


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

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

end of thread, other threads:[~2007-05-22 21:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-21  0:04 [PATCH] Fix compilation warning on 64 bits platforms Damien Lespiau
2007-05-21  0:28 ` Josh Triplett
2007-05-21  0:33   ` Al Viro
2007-05-21  0:36     ` Josh Triplett
2007-05-22 18:06       ` Damien Lespiau
2007-05-22 21:20         ` Josh Triplett

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 NNTP newsgroup(s).