Hi,

Just noticed this:

#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
...
#define FF(a, b, c, d, x, s, ac) { \
        (a) += F ((b), (c), (d)) + (x) + (u_int32_t)(ac); \
        (a) = ROTATE_LEFT ((a), (s)); \
        (a) += (b); \
        }
...
...
        const char *pp;
...
        for ( i = 0 ; i < 16 ; i++ ) {
                FF (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++);
                temp = d; d = c; c = b; b = a; a = temp;
        }

The invocation of FF macro looks bad.

x[(int)(*pp++)] has a side effect (pp is incremented).
It's dangerous to use such expressions as macro args.
By luck, here it so happens that 5th argument
is not evaluated twice by the macro
(for example, 1st and 2nd are evaluated multiple times).

Cast (int)(*pp) is strange. Was it intended by author
to make it work properly if char is a signed type?
Did he meant (unsigned) cast? But even if so,
it is not working: char -> int promotion happens *before*
the cast:

# cat t.c
int main() {
        signed char pp[] = { 0xff };
        if ((int)(*pp) < 0) printf("boo!\n");
        if ((unsigned)(*pp) > 0x100) printf("BOO!\n");
}
# gcc t.c
t.c: In function 'main':
t.c:3: warning: incompatible implicit declaration of built-in function 'printf'
# ./a.out
boo!
BOO!

The attached patch fixes these things. Please review.

Object code difference is just these two instructions:

@@ -156,14 +156,14 @@
 31 f8                  xor    %edi,%eax
 8d 14 08               lea    (%eax,%ecx,1),%edx
 8b 4c 24 24            mov    0x24(%esp),%ecx
-0f be 01               movsbl (%ecx),%eax
+0f b6 01               movzbl (%ecx),%eax
 03 54 84 2c            add    0x2c(%esp,%eax,4),%edx
 8b 44 24 20            mov    0x20(%esp),%eax
 03 10                  add    (%eax),%edx
 8b 4c 24 28            mov    0x28(%esp),%ecx
 83 e1 03               and    $0x3,%ecx
 8b 44 24 1c            mov    0x1c(%esp),%eax
-0f be 0c 08            movsbl (%eax,%ecx,1),%ecx
+0f b6 0c 08            movzbl (%eax,%ecx,1),%ecx
 d3 c2                  rol    %cl,%edx
 01 f2                  add    %esi,%edx
 ff 44 24 28            incl   0x28(%esp)

--
vda
diff -d -urpN -U5 uClibc.5/libcrypt/md5.c uClibc.6/libcrypt/md5.c
--- uClibc.5/libcrypt/md5.c	2008-06-10 22:37:10.000000000 +0200
+++ uClibc.6/libcrypt/md5.c	2008-06-12 15:42:54.000000000 +0200
@@ -142,14 +142,14 @@ __md5_Decode (u_int32_t *output, const u
 		    (((u_int32_t)input[j+2]) << 16) | (((u_int32_t)input[j+3]) << 24);
 }
 #endif /* i386 */
 
 /* F, G, H and I are basic MD5 functions. */
-#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
-#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
+#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
 #define H(x, y, z) ((x) ^ (y) ^ (z))
-#define I(x, y, z) ((y) ^ ((x) | (~z)))
+#define I(x, y, z) ((y) ^ ((x) | ~(z)))
 
 /* ROTATE_LEFT rotates x left n bits. */
 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
 
 /*
@@ -277,23 +277,23 @@ static void __md5_Final ( unsigned char 
 static void __md5_Transform (u_int32_t state[4], const unsigned char block[64])
 {
 	u_int32_t a, b, c, d, x[16];
 #if MD5_SIZE_OVER_SPEED > 1
 	u_int32_t temp;
-	const char *ps;
+	const unsigned char *ps;
 
-	static const char S[] = {
+	static const unsigned char S[] = {
 		7, 12, 17, 22,
 		5, 9, 14, 20,
 		4, 11, 16, 23,
 		6, 10, 15, 21
 	};
 #endif /* MD5_SIZE_OVER_SPEED > 1 */
 
 #if MD5_SIZE_OVER_SPEED > 0
 	const u_int32_t *pc;
-	const char *pp;
+	const unsigned char *pp;
 	int i;
 
 	static const u_int32_t C[] = {
 								/* round 1 */
 		0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
@@ -315,11 +315,11 @@ static void __md5_Transform (u_int32_t s
 		0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
 		0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
 		0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
 	};
 
-	static const char P[] = {
+	static const unsigned char P[] = {
 		0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
 		1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
 		5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
 		0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9  /* 4 */
 	};
@@ -348,75 +348,75 @@ static void __md5_Transform (u_int32_t s
 				break;
 			case 3:
 				temp += I(b,c,d);
 				break;
 		}
-		temp += x[(int)(*pp++)] + *pc++;
+		temp += x[*pp++] + *pc++;
 		temp = ROTATE_LEFT(temp, ps[i&3]);
 		temp += b;
 		a = d; d = c; c = b; b = temp;
 	}
 #elif MD5_SIZE_OVER_SPEED > 1
 	pc = C; pp = P; ps = S;
 
 	/* Round 1 */
 	for ( i = 0 ; i < 16 ; i++ ) {
-		FF (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++);
+		FF (a, b, c, d, x[*pp], ps[i&0x3], *pc); pp++; pc++;
 		temp = d; d = c; c = b; b = a; a = temp;
 	}
 
 	/* Round 2 */
 	ps += 4;
 	for ( ; i < 32 ; i++ ) {
-		GG (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++);
+		GG (a, b, c, d, x[*pp], ps[i&0x3], *pc); pp++; pc++;
 		temp = d; d = c; c = b; b = a; a = temp;
 	}
 	/* Round 3 */
 	ps += 4;
 	for ( ; i < 48 ; i++ ) {
-		HH (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++);
+		HH (a, b, c, d, x[*pp], ps[i&0x3], *pc); pp++; pc++;
 		temp = d; d = c; c = b; b = a; a = temp;
 	}
 
 	/* Round 4 */
 	ps += 4;
 	for ( ; i < 64 ; i++ ) {
-		II (a, b, c, d, x[(int)(*pp++)], ps[i&0x3], *pc++);
+		II (a, b, c, d, x[*pp], ps[i&0x3], *pc); pp++; pc++;
 		temp = d; d = c; c = b; b = a; a = temp;
 	}
 #elif MD5_SIZE_OVER_SPEED > 0
 	pc = C; pp = P;
 
 	/* Round 1 */
 	for ( i = 0 ; i < 4 ; i++ ) {
-		FF (a, b, c, d, x[(int)(*pp++)],  7, *pc++);
-		FF (d, a, b, c, x[(int)(*pp++)], 12, *pc++);
-		FF (c, d, a, b, x[(int)(*pp++)], 17, *pc++);
-		FF (b, c, d, a, x[(int)(*pp++)], 22, *pc++);
+		FF (a, b, c, d, x[*pp],  7, *pc); pp++; pc++;
+		FF (d, a, b, c, x[*pp], 12, *pc); pp++; pc++;
+		FF (c, d, a, b, x[*pp], 17, *pc); pp++; pc++;
+		FF (b, c, d, a, x[*pp], 22, *pc); pp++; pc++;
 	}
 
 	/* Round 2 */
 	for ( i = 0 ; i < 4 ; i++ ) {
-		GG (a, b, c, d, x[(int)(*pp++)],  5, *pc++);
-		GG (d, a, b, c, x[(int)(*pp++)],  9, *pc++);
-		GG (c, d, a, b, x[(int)(*pp++)], 14, *pc++);
-		GG (b, c, d, a, x[(int)(*pp++)], 20, *pc++);
+		GG (a, b, c, d, x[*pp],  5, *pc); pp++; pc++;
+		GG (d, a, b, c, x[*pp],  9, *pc); pp++; pc++;
+		GG (c, d, a, b, x[*pp], 14, *pc); pp++; pc++;
+		GG (b, c, d, a, x[*pp], 20, *pc); pp++; pc++;
 	}
 	/* Round 3 */
 	for ( i = 0 ; i < 4 ; i++ ) {
-		HH (a, b, c, d, x[(int)(*pp++)],  4, *pc++);
-		HH (d, a, b, c, x[(int)(*pp++)], 11, *pc++);
-		HH (c, d, a, b, x[(int)(*pp++)], 16, *pc++);
-		HH (b, c, d, a, x[(int)(*pp++)], 23, *pc++);
+		HH (a, b, c, d, x[*pp],  4, *pc); pp++; pc++;
+		HH (d, a, b, c, x[*pp], 11, *pc); pp++; pc++;
+		HH (c, d, a, b, x[*pp], 16, *pc); pp++; pc++;
+		HH (b, c, d, a, x[*pp], 23, *pc); pp++; pc++;
 	}
 
 	/* Round 4 */
 	for ( i = 0 ; i < 4 ; i++ ) {
-		II (a, b, c, d, x[(int)(*pp++)],  6, *pc++);
-		II (d, a, b, c, x[(int)(*pp++)], 10, *pc++);
-		II (c, d, a, b, x[(int)(*pp++)], 15, *pc++);
-		II (b, c, d, a, x[(int)(*pp++)], 21, *pc++);
+		II (a, b, c, d, x[*pp],  6, *pc); pp++; pc++;
+		II (d, a, b, c, x[*pp], 10, *pc); pp++; pc++;
+		II (c, d, a, b, x[*pp], 15, *pc); pp++; pc++;
+		II (b, c, d, a, x[*pp], 21, *pc); pp++; pc++;
 	}
 #else
 	/* Round 1 */
 #define S11 7
 #define S12 12
_______________________________________________
uClibc mailing list
uClibc@uclibc.org
http://busybox.net/cgi-bin/mailman/listinfo/uclibc

Reply via email to