From 021e5043e524b1cb28a929ef902548a987c16e65 Mon Sep 17 00:00:00 2001
From: Andy Polyakov <appro@openssl.org>
Date: Tue, 8 Jul 2014 22:24:44 +0200
Subject: [PATCH] Please Clang's sanitizer.

PR: #3424,#3423,#3422
---
 crypto/cast/cast_lcl.h      |  2 ++
 crypto/md32_common.h        | 20 +++++++++----------
 crypto/modes/cbc128.c       |  2 +-
 crypto/modes/modes_lcl.h    |  2 ++
 crypto/rc4/rc4_enc.c        |  2 +-
 engines/ccgost/gost89.c     | 16 ++++++++--------
 engines/ccgost/gost_crypt.c |  4 ++--
 ssl/heartbeat_test.c        | 38 ++++++++++++++++++++-----------------
 8 files changed, 47 insertions(+), 39 deletions(-)

diff --git a/crypto/cast/cast_lcl.h b/crypto/cast/cast_lcl.h
index e756021a33..a2cf8faba9 100644
--- a/crypto/cast/cast_lcl.h
+++ b/crypto/cast/cast_lcl.h
@@ -154,6 +154,8 @@
 
 #if defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)
 #define ROTL(a,n)     (_lrotl(a,n))
+#elif defined(PEDANTIC)
+#define ROTL(a,n)     ((((a)<<(n))&0xffffffffL)|((a)>>((32-(n))&31)))
 #else
 #define ROTL(a,n)     ((((a)<<(n))&0xffffffffL)|((a)>>(32-(n))))
 #endif
diff --git a/crypto/md32_common.h b/crypto/md32_common.h
index 147a7a00c3..1b84104354 100644
--- a/crypto/md32_common.h
+++ b/crypto/md32_common.h
@@ -235,10 +235,10 @@
 #   endif
 #  endif
 # endif
-#endif
-#if defined(__s390__) || defined(__s390x__)
-# define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
-# define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
+# if defined(__s390__) || defined(__s390x__)
+#  define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, (l))
+#  define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, (l))
+# endif
 #endif
 
 #ifndef HOST_c2l
@@ -269,12 +269,12 @@
 				   (c)+=4; (l);				})
 #  endif
 # endif
-#endif
-#if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
-# ifndef B_ENDIAN
-   /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
-#  define HOST_c2l(c,l)	((l)=*((const unsigned int *)(c)), (c)+=4, l)
-#  define HOST_l2c(l,c)	(*((unsigned int *)(c))=(l), (c)+=4, l)
+# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
+#  ifndef B_ENDIAN
+    /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
+#   define HOST_c2l(c,l)	((l)=*((const unsigned int *)(c)), (c)+=4, l)
+#   define HOST_l2c(l,c)	(*((unsigned int *)(c))=(l), (c)+=4, l)
+#  endif
 # endif
 #endif
 
diff --git a/crypto/modes/cbc128.c b/crypto/modes/cbc128.c
index 0e54f75470..8c9bdea4aa 100644
--- a/crypto/modes/cbc128.c
+++ b/crypto/modes/cbc128.c
@@ -59,7 +59,7 @@
 #endif
 #include <assert.h>
 
-#ifndef STRICT_ALIGNMENT
+#if !defined(STRICT_ALIGNMENT) && !defined(PEDANTIC)
 #  define STRICT_ALIGNMENT 0
 #endif
 
diff --git a/crypto/modes/modes_lcl.h b/crypto/modes/modes_lcl.h
index 35d4d9fa53..2d7015aea6 100644
--- a/crypto/modes/modes_lcl.h
+++ b/crypto/modes/modes_lcl.h
@@ -26,6 +26,7 @@ typedef unsigned int u32;
 typedef unsigned char u8;
 
 #define STRICT_ALIGNMENT 1
+#ifndef PEDANTIC
 #if defined(__i386)	|| defined(__i386__)	|| \
     defined(__x86_64)	|| defined(__x86_64__)	|| \
     defined(_M_IX86)	|| defined(_M_AMD64)	|| defined(_M_X64) || \
@@ -33,6 +34,7 @@ typedef unsigned char u8;
     defined(__s390__)	|| defined(__s390x__)
 # undef STRICT_ALIGNMENT
 #endif
+#endif
 
 #if !defined(PEDANTIC) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
 #if defined(__GNUC__) && __GNUC__>=2
diff --git a/crypto/rc4/rc4_enc.c b/crypto/rc4/rc4_enc.c
index 8c4fc6c7a3..0cc5ac471f 100644
--- a/crypto/rc4/rc4_enc.c
+++ b/crypto/rc4/rc4_enc.c
@@ -78,7 +78,7 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
         y=key->y;     
         d=key->data; 
 
-#if defined(RC4_CHUNK)
+#if defined(RC4_CHUNK) && !defined(PEDANTIC)
 	/*
 	 * The original reason for implementing this(*) was the fact that
 	 * pre-21164a Alpha CPUs don't have byte load/store instructions
diff --git a/engines/ccgost/gost89.c b/engines/ccgost/gost89.c
index b0568c6b3c..c1474cb652 100644
--- a/engines/ccgost/gost89.c
+++ b/engines/ccgost/gost89.c
@@ -120,7 +120,7 @@ static void kboxinit(gost_ctx *c, const gost_subst_block *b)
 	
 	for (i = 0; i < 256; i++)
 		{
-		c->k87[i] = (b->k8[i>>4] <<4 | b->k7 [i &15])<<24;
+		c->k87[i] = (word32)(b->k8[i>>4] <<4 | b->k7 [i &15])<<24;
 		c->k65[i] = (b->k6[i>>4] << 4 | b->k5 [i &15])<<16;
 		c->k43[i] = (b->k4[i>>4] <<4  | b->k3 [i &15])<<8;
 		c->k21[i] = b->k2[i>>4] <<4  | b->k1 [i &15];
@@ -140,8 +140,8 @@ static word32 f(gost_ctx *c,word32 x)
 void gostcrypt(gost_ctx *c, const byte *in, byte *out)
 	{ 
 	register word32 n1, n2; /* As named in the GOST */ 
-	n1 = in[0]|(in[1]<<8)|(in[2]<<16)|(in[3]<<24); 
-	n2 = in[4]|(in[5]<<8)|(in[6]<<16)|(in[7]<<24); 
+	n1 = in[0]|(in[1]<<8)|(in[2]<<16)|((word32)in[3]<<24); 
+	n2 = in[4]|(in[5]<<8)|(in[6]<<16)|((word32)in[7]<<24); 
 	/* Instead of swapping halves, swap names each round */ 
  	 
 	n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); 
@@ -173,8 +173,8 @@ void gostcrypt(gost_ctx *c, const byte *in, byte *out)
 void gostdecrypt(gost_ctx *c, const byte *in,byte *out)
 	{ 
 	register word32 n1, n2; /* As named in the GOST */ 
-	n1 = in[0]|(in[1]<<8)|(in[2]<<16)|(in[3]<<24); 
-	n2 = in[4]|(in[5]<<8)|(in[6]<<16)|(in[7]<<24); 
+	n1 = in[0]|(in[1]<<8)|(in[2]<<16)|((word32)in[3]<<24); 
+	n2 = in[4]|(in[5]<<8)|(in[6]<<16)|((word32)in[7]<<24); 
 	
 	n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); 
 	n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]); 
@@ -275,7 +275,7 @@ void gost_key(gost_ctx *c, const byte *k)
 	int i,j; 
 	for(i=0,j=0;i<8;i++,j+=4)
 		{
-		c->k[i]=k[j]|(k[j+1]<<8)|(k[j+2]<<16)|(k[j+3]<<24);
+		c->k[i]=k[j]|(k[j+1]<<8)|(k[j+2]<<16)|((word32)k[j+3]<<24);
 		}		
 	} 
 
@@ -323,8 +323,8 @@ void mac_block(gost_ctx *c,byte *buffer,const  byte *block)
 		{
 		buffer[i]^=block[i];
 		}	  
-	n1 = buffer[0]|(buffer[1]<<8)|(buffer[2]<<16)|(buffer[3]<<24); 
-	n2 = buffer[4]|(buffer[5]<<8)|(buffer[6]<<16)|(buffer[7]<<24); 
+	n1 = buffer[0]|(buffer[1]<<8)|(buffer[2]<<16)|((word32)buffer[3]<<24); 
+	n2 = buffer[4]|(buffer[5]<<8)|(buffer[6]<<16)|((word32)buffer[7]<<24); 
 	/* Instead of swapping halves, swap names each round */ 
  	 
 	n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]); 
diff --git a/engines/ccgost/gost_crypt.c b/engines/ccgost/gost_crypt.c
index 52aef15acf..15ab02aabb 100644
--- a/engines/ccgost/gost_crypt.c
+++ b/engines/ccgost/gost_crypt.c
@@ -241,13 +241,13 @@ static void gost_cnt_next (void *ctx, unsigned char *iv, unsigned char *buf)
 		{
 		memcpy(buf1,iv,8);
 		}	
-	g = buf1[0]|(buf1[1]<<8)|(buf1[2]<<16)|(buf1[3]<<24);
+	g = buf1[0]|(buf1[1]<<8)|(buf1[2]<<16)|((word32)buf1[3]<<24);
 	g += 0x01010101;
 	buf1[0]=(unsigned char)(g&0xff);
 	buf1[1]=(unsigned char)((g>>8)&0xff);
 	buf1[2]=(unsigned char)((g>>16)&0xff);
 	buf1[3]=(unsigned char)((g>>24)&0xff);
-	g = buf1[4]|(buf1[5]<<8)|(buf1[6]<<16)|(buf1[7]<<24);
+	g = buf1[4]|(buf1[5]<<8)|(buf1[6]<<16)|((word32)buf1[7]<<24);
 	go = g;
 	g += 0x01010104;
 	if (go > g)      /*  overflow*/
diff --git a/ssl/heartbeat_test.c b/ssl/heartbeat_test.c
index a0a3690096..287fc16825 100644
--- a/ssl/heartbeat_test.c
+++ b/ssl/heartbeat_test.c
@@ -273,7 +273,8 @@ static int test_dtls1_not_bleeding()
 	{
 	SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
 	/* Three-byte pad at the beginning for type and payload length */
-	unsigned char payload_buf[] = "   Not bleeding, sixteen spaces of padding"
+	unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
+		"   Not bleeding, sixteen spaces of padding"
 		"                ";
 	const int payload_buf_len = honest_payload_size(payload_buf);
 
@@ -292,9 +293,9 @@ static int test_dtls1_not_bleeding_empty_payload()
 	SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
 	/* Three-byte pad at the beginning for type and payload length, plus a NUL
 	 * at the end */
-	unsigned char payload_buf[4 + MIN_PADDING_SIZE];
-	memset(payload_buf, ' ', sizeof(payload_buf));
-	payload_buf[sizeof(payload_buf) - 1] = '\0';
+	unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
+	memset(payload_buf, ' ', MIN_PADDING_SIZE+3);
+	payload_buf[MIN_PADDING_SIZE+3] = '\0';
 	payload_buf_len = honest_payload_size(payload_buf);
 
 	fixture.payload = &payload_buf[0];
@@ -309,7 +310,8 @@ static int test_dtls1_heartbleed()
 	{
 	SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
 	/* Three-byte pad at the beginning for type and payload length */
-	unsigned char payload_buf[] = "   HEARTBLEED                ";
+	unsigned char payload_buf[4+MAX_PRINTABLE_CHARACTERS] =
+		"   HEARTBLEED                ";
 
 	fixture.payload = &payload_buf[0];
 	fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
@@ -324,9 +326,9 @@ static int test_dtls1_heartbleed_empty_payload()
 	SETUP_HEARTBEAT_TEST_FIXTURE(dtls);
 	/* Excluding the NUL at the end, one byte short of type + payload length +
 	 * minimum padding */
-	unsigned char payload_buf[MIN_PADDING_SIZE + 3];
-	memset(payload_buf, ' ', sizeof(payload_buf));
-	payload_buf[sizeof(payload_buf) - 1] = '\0';
+	unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
+	memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
+	payload_buf[MIN_PADDING_SIZE+2] = '\0';
 
 	fixture.payload = &payload_buf[0];
 	fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
@@ -357,8 +359,9 @@ static int test_tls1_not_bleeding()
 	{
 	SETUP_HEARTBEAT_TEST_FIXTURE(tls);
 	/* Three-byte pad at the beginning for type and payload length */
-	unsigned char payload_buf[] = "   Not bleeding, sixteen spaces of padding"
-					"                ";
+	unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
+			"   Not bleeding, sixteen spaces of padding"
+			"                ";
 	const int payload_buf_len = honest_payload_size(payload_buf);
 
 	fixture.payload = &payload_buf[0];
@@ -376,9 +379,9 @@ static int test_tls1_not_bleeding_empty_payload()
 	SETUP_HEARTBEAT_TEST_FIXTURE(tls);
 	/* Three-byte pad at the beginning for type and payload length, plus a NUL
 	 * at the end */
-	unsigned char payload_buf[4 + MIN_PADDING_SIZE];
-	memset(payload_buf, ' ', sizeof(payload_buf));
-	payload_buf[sizeof(payload_buf) - 1] = '\0';
+	unsigned char payload_buf[4 + MAX_PRINTABLE_CHARACTERS];
+	memset(payload_buf, ' ', MIN_PADDING_SIZE+3);
+	payload_buf[MIN_PADDING_SIZE+3] = '\0';
 	payload_buf_len = honest_payload_size(payload_buf);
 
 	fixture.payload = &payload_buf[0];
@@ -393,7 +396,8 @@ static int test_tls1_heartbleed()
 	{
 	SETUP_HEARTBEAT_TEST_FIXTURE(tls);
 	/* Three-byte pad at the beginning for type and payload length */
-	unsigned char payload_buf[] = "   HEARTBLEED                ";
+	unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS+4] =
+			"   HEARTBLEED                ";
 
 	fixture.payload = &payload_buf[0];
 	fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
@@ -408,9 +412,9 @@ static int test_tls1_heartbleed_empty_payload()
 	SETUP_HEARTBEAT_TEST_FIXTURE(tls);
 	/* Excluding the NUL at the end, one byte short of type + payload length +
 	 * minimum padding */
-	unsigned char payload_buf[MIN_PADDING_SIZE + 3];
-	memset(payload_buf, ' ', sizeof(payload_buf));
-	payload_buf[sizeof(payload_buf) - 1] = '\0';
+	unsigned char payload_buf[MAX_PRINTABLE_CHARACTERS + 4];
+	memset(payload_buf, ' ', MIN_PADDING_SIZE+2);
+	payload_buf[MIN_PADDING_SIZE+2] = '\0';
 
 	fixture.payload = &payload_buf[0];
 	fixture.sent_payload_len = MAX_PRINTABLE_CHARACTERS;
-- 
2.40.0