Skip to contentSkip to conversation

Fix compilation warning on 64 bits platforms

PATCH In review Damien Lespiau 3 participants 6 messages May 21, 2007 – May 22, 2007 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix compilation warning on 64 bits platforms

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

---
 linearize.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
linearize.c +1 -1
index d428d92..6c9fae4 100644
@@ -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;
Signature · 1 line
1.5.2.rc3.87.g404fd
#1 lore raw
Quoted 1 line · Damien Lespiau

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

[...]

linearize.c const char *show_instruction(struct instruction *insn) · line 464

Quoted 6 lines
linearize.c +1 -1
@@ -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

#2 lore raw
Quoted 6 lines · Josh Triplett
Quoted 1 line · Damien Lespiau

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

[...]

Quoted 3 lines
(quoted diff) +0 -0
@@ -461,7 +461,7 @@ const char *show_instruction(struct instruction *insn)
 	}
 
Quoted 9 lines

linearize.c const char *show_instruction(struct instruction *insn) · line 464

Quoted 3 lines
linearize.c +1 -1
 	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.

#3 lore raw
Quoted 16 lines · Al Viro
Quoted 6 lines · Josh Triplett
Quoted 1 line · Damien Lespiau

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

[...]

Quoted 3 lines
(quoted diff) +0 -0
@@ -461,7 +461,7 @@ const char *show_instruction(struct instruction *insn)
 	}
 
Quoted 8 lines

linearize.c const char *show_instruction(struct instruction *insn) · line 464

Quoted 3 lines
linearize.c +1 -1
 	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.

Quoted 1 line

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

#4 lore raw
Quoted 2 lines
Quoted 1 line · Al Viro

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.

Signature · 1 line
Damien
#5 lore raw
Quoted 5 lines · Damien Lespiau
Quoted 2 lines
Quoted 1 line · Al Viro

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 <[email protected]>
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.

---
 linearize.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
linearize.c +1 -1
index d428d92..c38dd7d 100644
@@ -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