1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-10-22 22:13:04 +03:00

When converting from a BLOB value in the tointeger() and toreal() SQL functions, make sure that endianness of the machine does not matter.

FossilOrigin-Name: 94c4cdc50d2753c859e494d53cebd09edd2e5663
This commit is contained in:
mistachkin
2013-09-06 20:30:53 +00:00
parent aeddf19e60
commit 9a5b271908
4 changed files with 114 additions and 121 deletions

View File

@@ -966,8 +966,8 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
}
/*
** tointeger(X): If X is any value (integer, double, or string) that can
** be losslessly converted into an integer, then make the conversion and
** tointeger(X): If X is any value (integer, double, blob, or string) that
** can be losslessly converted into an integer, then make the conversion and
** return the result. Otherwise, return NULL.
*/
static void tointegerFunc(
@@ -996,7 +996,16 @@ static void tointegerFunc(
int nBlob = sqlite3_value_bytes(argv[0]);
if( nBlob==sizeof(i64) ){
i64 iVal;
memcpy(&iVal, zBlob, sizeof(i64));
if( SQLITE_BIGENDIAN ){
int i;
unsigned char *zBlobRev = contextMalloc(context, nBlob);
if( !zBlobRev ) break;
for(i=0; i<nBlob; i++) zBlobRev[i] = zBlob[nBlob-1-i];
memcpy(&iVal, zBlobRev, sizeof(i64));
sqlite3_free(zBlobRev);
}else{
memcpy(&iVal, zBlob, sizeof(i64));
}
sqlite3_result_int64(context, iVal);
}
}
@@ -1023,8 +1032,9 @@ static void tointegerFunc(
}
/*
** toreal(X): If X can be losslessly converted into a real number, then
** do so and return that real number. Otherwise return NULL.
** toreal(X): If X is any value (integer, double, blob, or string) that can
** be losslessly converted into a real number, then do so and return that
** real number. Otherwise return NULL.
*/
#if defined(_MSC_VER)
#pragma optimize("", off)
@@ -1055,7 +1065,16 @@ static void torealFunc(
int nBlob = sqlite3_value_bytes(argv[0]);
if( nBlob==sizeof(double) ){
double rVal;
memcpy(&rVal, zBlob, sizeof(double));
if( SQLITE_LITTLEENDIAN ){
int i;
unsigned char *zBlobRev = contextMalloc(context, nBlob);
if( !zBlobRev ) break;
for(i=0; i<nBlob; i++) zBlobRev[i] = zBlob[nBlob-1-i];
memcpy(&rVal, zBlobRev, sizeof(double));
sqlite3_free(zBlobRev);
}else{
memcpy(&rVal, zBlob, sizeof(double));
}
sqlite3_result_double(context, rVal);
}
}