Skip to contentSkip to conversation

make size_t better approximate the reality

PATCH In review Al Viro 2 participants 2 messages Jul 1, 2007 – Jul 9, 2007 Sparse checker

lore
Share & tools
lore

Al Viro

author
#0 lore raw

PATCH 1/1 make size_t better approximate the reality

From: Al Viro <[email protected]>
Date: Tue, 26 Jun 2007 17:41:27 -0400
Subject: [PATCH] make size_t better approximate the reality

Instead of "always unsigned long" go for "unsigned int unless -m64 is given, unsigned long otherwise". Add an option (-msize-long) forcing to unsigned long regardless. Make __SIZE_TYPE__ expansion match that.

The thing is, addition of checks on comparisons make for very unhappy min() on (kernel) size_t and sizeof(something) on the targets where the former is unsigned int. Which is to say, more than half of them... AFAICS, the only place needing explicit -msize-long in CHECK_FLAGS is s390 (it's using unsigned long both for 31- and 64-bit).

---
 lib.c    |   10 +++++++++-
 target.c |    4 ++--
 2 files changed, 11 insertions(+), 3 deletions(-)
lib.c +9 -1
index 7fea474..efba3d4 100644
@@ -325,6 +325,11 @@ static char **handle_switch_m(char *arg, char **next)
 		max_int_alignment = 8;
 		bits_in_pointer = 64;
 		pointer_alignment = 8;
+		size_t_ctype = &ulong_ctype;
+		ssize_t_ctype = &long_ctype;
+	} else if (!strcmp(arg, "msize-long")) {
+		size_t_ctype = &ulong_ctype;
+		ssize_t_ctype = &long_ctype;
 	}
 	return next;
 }
@@ -599,7 +604,10 @@ void create_builtin_stream(void)
 	// it is "long unsigned int".  In either case we can probably
 	// get away with this.  We need the #weak_define as cgcc will define
 	// the right __SIZE_TYPE__.
-	add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
+	if (size_t_ctype == &ulong_ctype)
+		add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
+	else
+		add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");
 	add_pre_buffer("#weak_define __STDC__ 1\n");
 
 	add_pre_buffer("#define __builtin_stdarg_start(a,b) ((a) = (__builtin_va_list)(&(b)))\n");
target.c +2 -2
index 22e948e..bf1bb8f 100644
@@ -3,8 +3,8 @@
 #include "symbol.h"
 #include "target.h"
 
-struct symbol *size_t_ctype = &ulong_ctype;
-struct symbol *ssize_t_ctype = &long_ctype;
+struct symbol *size_t_ctype = &uint_ctype;
+struct symbol *ssize_t_ctype = &int_ctype;
 
 /*
  * For "__attribute__((aligned))"
Signature · 1 line
1.5.0-rc2.GIT
#1 lore raw
Quoted 10 lines · Al Viro

Instead of "always unsigned long" go for "unsigned int unless -m64 is given, unsigned long otherwise". Add an option (-msize-long) forcing to unsigned long regardless. Make __SIZE_TYPE__ expansion match that.

The thing is, addition of checks on comparisons make for very unhappy min() on (kernel) size_t and sizeof(something) on the targets where the former is unsigned int. Which is to say, more than half of them... AFAICS, the only place needing explicit -msize-long in CHECK_FLAGS is s390 (it's using unsigned long both for 31- and 64-bit).

Seems reasonable. One comment, though:

lib.c void create_builtin_stream(void) · line 610

Quoted 9 lines
lib.c +4 -1
@@ -599,7 +604,10 @@ void create_builtin_stream(void)
 	// it is "long unsigned int".  In either case we can probably
 	// get away with this.  We need the #weak_define as cgcc will define
 	// the right __SIZE_TYPE__.
-	add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
+	if (size_t_ctype == &ulong_ctype)
+		add_pre_buffer("#weak_define __SIZE_TYPE__ long unsigned int\n");
+	else
+		add_pre_buffer("#weak_define __SIZE_TYPE__ unsigned int\n");

I think the comment above this needs updating for this change, as it describes the situation created by the code you've removed, not the new situation. Perhaps the comment should just go away?

In any case, I've applied the patch anyway.

- Josh Triplett