Skip to contentSkip to conversation

Fix itest endianess problem

PATCH New ito at mlb.co.jp 1 participant 1 message Apr 4, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix itest endianess problem

Hello

Command 'itest' has 2 bugs.
1. It does not work on big endian machine.
and itest.b may cause unaligned access.

2. itest.l does not work on mips CPU.
on mips,

    result of (1 << 32) -1 is 0.
    (gcc 3.3.4 binutil 2.14.90.0.6 )
     I think, In C language, shifting more than 32bit is undefined.

Please consider to merge
Patch is attched.
--------
Hiroshi Ito
Media Lab. Inc.,
URL http://www.mlb.co.jp ( Sorry, Japanese only. ) TEL +81-3-5294-7255 FAX +81-3-5294-7256 -------------- next part --------------

common/cmd_itest.c +29 -9
index 8ad134f..20dada9 100644
@@ -70,17 +70,37 @@ extern int cmd_get_data_size(char* arg, 
 
 static long evalexp(char *s, int w)
 {
-	long l, *p;
-
+	long l;
+	void *p;
+	switch (w) {
+	case 1:
 	/* if the parameter starts with a * then assume is a pointer to the value we want */
-	if (s[0] == '*') {
-		p = (long *)simple_strtoul(&s[1], NULL, 16);
-		l = *p;
-	} else {
-		l = simple_strtoul(s, NULL, 16);
+		if (s[0] == '*') {
+			p = (void *)simple_strtoul(&s[1], NULL, 16);
+			l = *(unsigned char*)p;
+		} else {
+			l = simple_strtoul(s, NULL, 16) & 0xFF;
+		}
+		break;
+	case 2:
+		if (s[0] == '*') {
+			p = (void *)simple_strtoul(&s[1], NULL, 16);
+			l = *(unsigned short*)p;
+		} else {
+			l = simple_strtoul(s, NULL, 16) & 0xFFFF;
+		}
+		break;
+	case 4:
+	default:
+		if (s[0] == '*') {
+			p = (void *)simple_strtoul(&s[1], NULL, 16);
+			l = *(unsigned int*)p;
+		} else {
+			l = simple_strtoul(s, NULL, 16);
+		}
+		break;
 	}
-
-	return (l & ((1 << (w * 8)) - 1));
+	return l;
 }
 
 static char * evalstr(char *s)