From: drh Date: Tue, 4 Mar 2014 04:04:33 +0000 (+0000) Subject: Fix more instances of assuming 'char' is signed. And, make sure to never shift X-Git-Tag: version-3.8.4~34^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f926d1ea47939fe1c60d04bcf269b4ad6c1f50a1;p=thirdparty%2Fsqlite.git Fix more instances of assuming 'char' is signed. And, make sure to never shift a signed integer. FossilOrigin-Name: f10130ede433a19b3945753f23962871c8d2dcf3 --- diff --git a/manifest b/manifest index eea0f83b79..61023f47d0 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Do\snot\sassume\sthat\s'char'\sis\ssigned.\s\sMake\sit\sexplicit. -D 2014-03-04T00:15:16.875 +C Fix\smore\sinstances\sof\sassuming\s'char'\sis\ssigned.\s\sAnd,\smake\ssure\sto\snever\sshift\na\ssigned\sinteger. +D 2014-03-04T04:04:33.269 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 2ef13430cd359f7b361bb863504e227b25cc7f81 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -281,7 +281,7 @@ F src/vdbe.c 75c3f5d27ab79af214646cf37d7551bc8cec09c2 F src/vdbe.h d189f92468a17a6f04daeec9df3b767f50557b21 F src/vdbeInt.h 9ccca0bc7646c918d065943e44bead4bf5de213d F src/vdbeapi.c 5bc41aaea448a7fc250902c418f1795859be3820 -F src/vdbeaux.c 5adf67ef9cdaa57b393c0a8bf0a29eadbd6f9158 +F src/vdbeaux.c b3bd75b2dfe314fb411c967034314b5316342820 F src/vdbeblob.c d939997de046b8fcc607cfee4248f3d33dbcca50 F src/vdbemem.c 2d7918e4c80546d943414668b1485b2581f58a28 F src/vdbesort.c 46801acb342e5e4c07ba1777fe58880c143abb59 @@ -1154,7 +1154,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01 F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff -P aec5473a750e412eb1e11e17bbafd760db086c86 -R 3fc76904aa4a71ad8f46b000aee0d4b3 +P 979da752e6f8767a61a8efed824ffad9605d0f4c +R 76afae803c9615945e7c1ed11c396a45 U drh -Z f6bc4780db4acc20db7127296b21d314 +Z 4679453e8217252329a19e88ce1ddc0d diff --git a/manifest.uuid b/manifest.uuid index 0b0b678799..b38b734ab9 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -979da752e6f8767a61a8efed824ffad9605d0f4c \ No newline at end of file +f10130ede433a19b3945753f23962871c8d2dcf3 \ No newline at end of file diff --git a/src/vdbeaux.c b/src/vdbeaux.c index 2526d6fc50..776821d398 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -2942,6 +2942,14 @@ u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){ return 0; } +/* Input "x" is a sequence of unsigned characters that represent a +** big-endian integer. Return the equivalent native integer +*/ +#define ONE_BYTE_INT(x) ((i8)(x)[0]) +#define TWO_BYTE_INT(x) (256*(i8)((x)[0])|(x)[1]) +#define THREE_BYTE_INT(x) (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2]) +#define FOUR_BYTE_UINT(x) (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3]) + /* ** Deserialize the data blob pointed to by buf as serial type serial_type ** and store the result in pMem. Return the number of bytes read. @@ -2953,7 +2961,6 @@ u32 sqlite3VdbeSerialGet( ){ u64 x; u32 y; - int i; switch( serial_type ){ case 10: /* Reserved for future use */ case 11: /* Reserved for future use */ @@ -2962,33 +2969,28 @@ u32 sqlite3VdbeSerialGet( break; } case 1: { /* 1-byte signed integer */ - pMem->u.i = (signed char)buf[0]; + pMem->u.i = ONE_BYTE_INT(buf); pMem->flags = MEM_Int; return 1; } case 2: { /* 2-byte signed integer */ - i = 256*(signed char)buf[0] | buf[1]; - pMem->u.i = (i64)i; + pMem->u.i = TWO_BYTE_INT(buf); pMem->flags = MEM_Int; return 2; } case 3: { /* 3-byte signed integer */ - i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2]; - pMem->u.i = (i64)i; + pMem->u.i = THREE_BYTE_INT(buf); pMem->flags = MEM_Int; return 3; } case 4: { /* 4-byte signed integer */ - y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; + y = FOUR_BYTE_UINT(buf); pMem->u.i = (i64)*(int*)&y; pMem->flags = MEM_Int; return 4; } case 5: { /* 6-byte signed integer */ - x = 256*(signed char)buf[0] + buf[1]; - y = ((unsigned)buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; - x = (x<<32) | y; - pMem->u.i = *(i64*)&x; + pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf); pMem->flags = MEM_Int; return 6; } @@ -3006,8 +3008,8 @@ u32 sqlite3VdbeSerialGet( swapMixedEndianFloat(t2); assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); #endif - x = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; - y = ((unsigned)buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; + x = FOUR_BYTE_UINT(buf); + y = FOUR_BYTE_UINT(buf+4); x = (x<<32) | y; if( serial_type==6 ){ pMem->u.i = *(i64*)&x; @@ -3352,26 +3354,27 @@ int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ ** and returns the value. */ static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){ + u32 y; assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) ); switch( serial_type ){ case 0: case 1: - return (char)aKey[0]; + return ONE_BYTE_INT(aKey); case 2: - return ((char)aKey[0] << 8) | aKey[1]; + return TWO_BYTE_INT(aKey); case 3: - return ((char)aKey[0] << 16) | (aKey[1] << 8) | aKey[2]; - case 4: - return ((char)aKey[0]<<24) | (aKey[1]<<16) | (aKey[2]<<8)| aKey[3]; + return THREE_BYTE_INT(aKey); + case 4: { + y = FOUR_BYTE_UINT(aKey); + return (i64)*(int*)&y; + } case 5: { - i64 msw = ((char)aKey[0]<<24)|(aKey[1]<<16)|(aKey[2]<<8)|aKey[3]; - u32 lsw = (aKey[4] << 8) | aKey[5]; - return (i64)( msw << 16 | (u64)lsw ); + return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); } case 6: { - i64 msw = ((char)aKey[0]<<24)|(aKey[1]<<16)|(aKey[2]<<8)|aKey[3]; - u32 lsw = ((unsigned)aKey[4]<<24)|(aKey[5]<<16)|(aKey[6]<<8)|aKey[7]; - return (i64)( msw << 32 | (u64)lsw ); + u64 x = FOUR_BYTE_UINT(aKey); + x = (x<<32) | FOUR_BYTE_UINT(aKey+4); + return (i64)*(i64*)&x; } } @@ -3578,34 +3581,39 @@ static int vdbeRecordCompareInt( const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F]; int serial_type = ((const u8*)pKey1)[1]; int res; + u32 y; + u64 x; i64 v = pPKey2->aMem[0].u.i; i64 lhs; UNUSED_PARAMETER(bSkip); assert( bSkip==0 ); switch( serial_type ){ - case 1: - lhs = (char)(aKey[0]); + case 1: { /* 1-byte signed integer */ + lhs = ONE_BYTE_INT(aKey); break; - case 2: - lhs = 256*(signed char)aKey[0] + aKey[1]; + } + case 2: { /* 2-byte signed integer */ + lhs = TWO_BYTE_INT(aKey); break; - case 3: - lhs = 65536*(char)aKey[0] | (aKey[1]<<8) | aKey[2]; + } + case 3: { /* 3-byte signed integer */ + lhs = THREE_BYTE_INT(aKey); break; - case 4: - lhs = (int)(((u32)aKey[0]<<24) | (aKey[1]<<16) | (aKey[2]<<8)| aKey[3]); + } + case 4: { /* 4-byte signed integer */ + y = FOUR_BYTE_UINT(aKey); + lhs = (i64)*(int*)&y; break; - case 5: { - i64 msw = ((char)aKey[0]<<24)|(aKey[1]<<16)|(aKey[2]<<8)|aKey[3]; - u32 lsw = (aKey[4] << 8) | aKey[5]; - lhs = (i64)( msw << 16 | (u64)lsw ); + } + case 5: { /* 6-byte signed integer */ + lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey); break; } - case 6: { - i64 msw = ((char)aKey[0]<<24)|(aKey[1]<<16)|(aKey[2]<<8)|aKey[3]; - u32 lsw = ((unsigned)aKey[4]<<24)|(aKey[5]<<16)|(aKey[6]<<8)|aKey[7]; - lhs = (i64)( msw << 32 | (u64)lsw ); + case 6: { /* 8-byte signed integer */ + x = FOUR_BYTE_UINT(aKey); + x = (x<<32) | FOUR_BYTE_UINT(aKey+4); + lhs = *(i64*)&x; break; } case 8: