Skip to content

Commit

Permalink
Fix uses of ctype functions (ex: isspace()) on signed characters in test
Browse files Browse the repository at this point in the history
programs and in some obscure extensions.  No changes to the core.
  • Loading branch information
D. Richard Hipp committed Oct 29, 2015
1 parent bff1cff commit 282852e
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 86 deletions.
6 changes: 3 additions & 3 deletions autoconf/tea/win/nmakehlp.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,8 @@ SubstituteFile(
sp = fopen(substitutions, "rt");
if (sp != NULL) {
while (fgets(szBuffer, cbBuffer, sp) != NULL) {
char *ks, *ke, *vs, *ve;
ks = szBuffer;
unsigned char *ks, *ke, *vs, *ve;
ks = (unsigned char*)szBuffer;
while (ks && *ks && isspace(*ks)) ++ks;
ke = ks;
while (ke && *ke && !isspace(*ke)) ++ke;
Expand All @@ -613,7 +613,7 @@ SubstituteFile(
ve = vs;
while (ve && *ve && !(*ve == '\r' || *ve == '\n')) ++ve;
*ke = 0, *ve = 0;
list_insert(&substPtr, ks, vs);
list_insert(&substPtr, (char*)ks, (char*)vs);
}
fclose(sp);
}
Expand Down
6 changes: 3 additions & 3 deletions ext/fts1/fts1.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,13 @@ static int getVarint32(const char *p, int *pi){
*/
/* TODO(shess) Is __isascii() a portable version of (c&0x80)==0? */
static int safe_isspace(char c){
return (c&0x80)==0 ? isspace(c) : 0;
return (c&0x80)==0 ? isspace((unsigned char)c) : 0;
}
static int safe_tolower(char c){
return (c&0x80)==0 ? tolower(c) : c;
return (c&0x80)==0 ? tolower((unsigned char)c) : c;
}
static int safe_isalnum(char c){
return (c&0x80)==0 ? isalnum(c) : 0;
return (c&0x80)==0 ? isalnum((unsigned char)c) : 0;
}

typedef enum DocListType {
Expand Down
2 changes: 1 addition & 1 deletion ext/fts1/simple_tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static int simpleNext(
** case-insensitivity.
*/
char ch = c->pCurrent[ii];
c->zToken[ii] = (unsigned char)ch<0x80 ? tolower(ch) : ch;
c->zToken[ii] = (unsigned char)ch<0x80 ? tolower((unsigned char)ch):ch;
}
c->zToken[n] = '\0';
*ppToken = c->zToken;
Expand Down
4 changes: 2 additions & 2 deletions ext/misc/amatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,10 @@ static const char *amatchValueOfKey(const char *zKey, const char *zStr){
int i;
if( nStr<nKey+1 ) return 0;
if( memcmp(zStr, zKey, nKey)!=0 ) return 0;
for(i=nKey; isspace(zStr[i]); i++){}
for(i=nKey; isspace((unsigned char)zStr[i]); i++){}
if( zStr[i]!='=' ) return 0;
i++;
while( isspace(zStr[i]) ){ i++; }
while( isspace((unsigned char)zStr[i]) ){ i++; }
return zStr+i;
}

Expand Down
4 changes: 2 additions & 2 deletions ext/misc/closure.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,10 @@ static const char *closureValueOfKey(const char *zKey, const char *zStr){
int i;
if( nStr<nKey+1 ) return 0;
if( memcmp(zStr, zKey, nKey)!=0 ) return 0;
for(i=nKey; isspace(zStr[i]); i++){}
for(i=nKey; isspace((unsigned char)zStr[i]); i++){}
if( zStr[i]!='=' ) return 0;
i++;
while( isspace(zStr[i]) ){ i++; }
while( isspace((unsigned char)zStr[i]) ){ i++; }
return zStr+i;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/misc/spellfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ static char *spellfix1Dequote(const char *zIn){
char *zOut;
int i, j;
char c;
while( isspace(zIn[0]) ) zIn++;
while( isspace((unsigned char)zIn[0]) ) zIn++;
zOut = sqlite3_mprintf("%s", zIn);
if( zOut==0 ) return 0;
i = (int)strlen(zOut);
Expand Down
41 changes: 22 additions & 19 deletions mptest/mptest.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
#include <assert.h>
#include <ctype.h>

#define ISSPACE(X) isspace((unsigned char)(X))
#define ISDIGIT(X) isdigit((unsigned char)(X))

/* The suffix to append to the child command lines, if any */
#if defined(_WIN32)
# define GETPID (int)GetCurrentProcessId
Expand Down Expand Up @@ -187,10 +190,10 @@ int strglob(const char *zGlob, const char *z){
}
if( c2==0 || (seen ^ invert)==0 ) return 0;
}else if( c=='#' ){
if( (z[0]=='-' || z[0]=='+') && isdigit(z[1]) ) z++;
if( !isdigit(z[0]) ) return 0;
if( (z[0]=='-' || z[0]=='+') && ISDIGIT(z[1]) ) z++;
if( !ISDIGIT(z[0]) ) return 0;
z++;
while( isdigit(z[0]) ){ z++; }
while( ISDIGIT(z[0]) ){ z++; }
}else{
if( c!=(*(z++)) ) return 0;
}
Expand Down Expand Up @@ -289,7 +292,7 @@ static void logMessage(const char *zFormat, ...){
*/
static int clipLength(const char *z){
int n = (int)strlen(z);
while( n>0 && isspace(z[n-1]) ){ n--; }
while( n>0 && ISSPACE(z[n-1]) ){ n--; }
return n;
}

Expand Down Expand Up @@ -444,7 +447,7 @@ static void stringAppendTerm(String *p, const char *z){
stringAppend(p, "nil", 3);
return;
}
for(i=0; z[i] && !isspace(z[i]); i++){}
for(i=0; z[i] && !ISSPACE(z[i]); i++){}
if( i>0 && z[i]==0 ){
stringAppend(p, z, i);
return;
Expand Down Expand Up @@ -699,7 +702,7 @@ static char *readFile(const char *zFilename){
*/
static int tokenLength(const char *z, int *pnLine){
int n = 0;
if( isspace(z[0]) || (z[0]=='/' && z[1]=='*') ){
if( ISSPACE(z[0]) || (z[0]=='/' && z[1]=='*') ){
int inC = 0;
int c;
if( z[0]=='/' ){
Expand All @@ -708,7 +711,7 @@ static int tokenLength(const char *z, int *pnLine){
}
while( (c = z[n++])!=0 ){
if( c=='\n' ) (*pnLine)++;
if( isspace(c) ) continue;
if( ISSPACE(c) ) continue;
if( inC && c=='*' && z[n]=='/' ){
n++;
inC = 0;
Expand All @@ -734,7 +737,7 @@ static int tokenLength(const char *z, int *pnLine){
}
}else{
int c;
for(n=1; (c = z[n])!=0 && !isspace(c) && c!='"' && c!='\'' && c!=';'; n++){}
for(n=1; (c = z[n])!=0 && !ISSPACE(c) && c!='"' && c!='\'' && c!=';'; n++){}
}
return n;
}
Expand All @@ -748,7 +751,7 @@ static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){
zOut[0] = 0;
return 0;
}
for(i=0; i<nIn && i<nOut-1 && !isspace(zIn[i]); i++){ zOut[i] = zIn[i]; }
for(i=0; i<nIn && i<nOut-1 && !ISSPACE(zIn[i]); i++){ zOut[i] = zIn[i]; }
zOut[i] = 0;
return i;
}
Expand All @@ -758,7 +761,7 @@ static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){
*/
static int findEnd(const char *z, int *pnLine){
int n = 0;
while( z[n] && (strncmp(z+n,"--end",5) || !isspace(z[n+5])) ){
while( z[n] && (strncmp(z+n,"--end",5) || !ISSPACE(z[n+5])) ){
n += tokenLength(z+n, pnLine);
}
return n;
Expand All @@ -773,12 +776,12 @@ static int findEndif(const char *z, int stopAtElse, int *pnLine){
int n = 0;
while( z[n] ){
int len = tokenLength(z+n, pnLine);
if( (strncmp(z+n,"--endif",7)==0 && isspace(z[n+7]))
|| (stopAtElse && strncmp(z+n,"--else",6)==0 && isspace(z[n+6]))
if( (strncmp(z+n,"--endif",7)==0 && ISSPACE(z[n+7]))
|| (stopAtElse && strncmp(z+n,"--else",6)==0 && ISSPACE(z[n+6]))
){
return n+len;
}
if( strncmp(z+n,"--if",4)==0 && isspace(z[n+4]) ){
if( strncmp(z+n,"--if",4)==0 && ISSPACE(z[n+4]) ){
int skip = findEndif(z+n+len, 0, pnLine);
n += skip + len;
}else{
Expand Down Expand Up @@ -888,7 +891,7 @@ static void runScript(
while( (c = zScript[ii])!=0 ){
prevLine = lineno;
len = tokenLength(zScript+ii, &lineno);
if( isspace(c) || (c=='/' && zScript[ii+1]=='*') ){
if( ISSPACE(c) || (c=='/' && zScript[ii+1]=='*') ){
ii += len;
continue;
}
Expand All @@ -909,7 +912,7 @@ static void runScript(
if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii);
n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd));
for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){
while( n<len-2 && isspace(zScript[ii+2+n]) ){ n++; }
while( n<len-2 && ISSPACE(zScript[ii+2+n]) ){ n++; }
if( n>=len-2 ) break;
n += extractToken(zScript+ii+2+n, len-2-n,
azArg[nArg], sizeof(azArg[nArg]));
Expand Down Expand Up @@ -976,7 +979,7 @@ static void runScript(
if( strcmp(zCmd, "match")==0 ){
int jj;
char *zAns = zScript+ii;
for(jj=7; jj<len-1 && isspace(zAns[jj]); jj++){}
for(jj=7; jj<len-1 && ISSPACE(zAns[jj]); jj++){}
zAns += jj;
if( len-jj-1!=sResult.n || strncmp(sResult.z, zAns, len-jj-1) ){
errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]",
Expand All @@ -998,7 +1001,7 @@ static void runScript(
char *zAns = zScript+ii;
char *zCopy;
int isGlob = (zCmd[0]=='g');
for(jj=9-3*isGlob; jj<len-1 && isspace(zAns[jj]); jj++){}
for(jj=9-3*isGlob; jj<len-1 && ISSPACE(zAns[jj]); jj++){}
zAns += jj;
zCopy = sqlite3_mprintf("%.*s", len-jj-1, zAns);
if( (sqlite3_strglob(zCopy, sResult.z)==0)^isGlob ){
Expand Down Expand Up @@ -1050,7 +1053,7 @@ static void runScript(
*/
if( strcmp(zCmd, "print")==0 ){
int jj;
for(jj=7; jj<len && isspace(zScript[ii+jj]); jj++){}
for(jj=7; jj<len && ISSPACE(zScript[ii+jj]); jj++){}
logMessage("%.*s", len-jj, zScript+ii+jj);
}else

Expand All @@ -1062,7 +1065,7 @@ static void runScript(
if( strcmp(zCmd, "if")==0 ){
int jj, rc;
sqlite3_stmt *pStmt;
for(jj=4; jj<len && isspace(zScript[ii+jj]); jj++){}
for(jj=4; jj<len && ISSPACE(zScript[ii+jj]); jj++){}
pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj);
rc = sqlite3_step(pStmt);
if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){
Expand Down
9 changes: 6 additions & 3 deletions test/fuzzcheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@
#include <stdarg.h>
#include <ctype.h>
#include "sqlite3.h"
#define ISSPACE(X) isspace((unsigned char)(X))
#define ISDIGIT(X) isdigit((unsigned char)(X))


#ifdef __unix__
# include <signal.h>
Expand Down Expand Up @@ -633,9 +636,9 @@ static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){
if( runFlags & SQL_TRACE ){
const char *z = zSql;
int n;
while( z<zMore && isspace(z[0]) ) z++;
while( z<zMore && ISSPACE(z[0]) ) z++;
n = (int)(zMore - z);
while( n>0 && isspace(z[n-1]) ) n--;
while( n>0 && ISSPACE(z[n-1]) ) n--;
if( n==0 ) break;
if( pStmt==0 ){
printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db));
Expand Down Expand Up @@ -757,7 +760,7 @@ static int integerValue(const char *zArg){
zArg++;
}
}else{
while( isdigit(zArg[0]) ){
while( ISDIGIT(zArg[0]) ){
v = v*10 + zArg[0] - '0';
zArg++;
}
Expand Down
6 changes: 4 additions & 2 deletions test/speedtest1.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static const char zHelp[] =
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#define ISSPACE(X) isspace((unsigned char)(X))
#define ISDIGIT(X) isdigit((unsigned char)(X))

#if SQLITE_VERSION_NUMBER<3005000
# define sqlite3_int64 sqlite_int64
Expand Down Expand Up @@ -315,7 +317,7 @@ void speedtest1_final(void){
/* Print an SQL statement to standard output */
static void printSql(const char *zSql){
int n = (int)strlen(zSql);
while( n>0 && (zSql[n-1]==';' || isspace(zSql[n-1])) ){ n--; }
while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ){ n--; }
if( g.bExplain ) printf("EXPLAIN ");
printf("%.*s;\n", n, zSql);
if( g.bExplain
Expand Down Expand Up @@ -414,7 +416,7 @@ void speedtest1_run(void){
/* The sqlite3_trace() callback function */
static void traceCallback(void *NotUsed, const char *zSql){
int n = (int)strlen(zSql);
while( n>0 && (zSql[n-1]==';' || isspace(zSql[n-1])) ) n--;
while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ) n--;
fprintf(stderr,"%.*s;\n", n, zSql);
}

Expand Down
5 changes: 3 additions & 2 deletions test/wordcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include "sqlite3.h"
#define ISALPHA(X) isalpha((unsigned char)(X))

/* Return the current wall-clock time */
static sqlite3_int64 realTime(void){
Expand Down Expand Up @@ -392,8 +393,8 @@ int main(int argc, char **argv){
/* Process the input file */
while( fgets(zInput, sizeof(zInput), in) ){
for(i=0; zInput[i]; i++){
if( !isalpha(zInput[i]) ) continue;
for(j=i+1; isalpha(zInput[j]); j++){}
if( !ISALPHA(zInput[i]) ) continue;
for(j=i+1; ISALPHA(zInput[j]); j++){}

/* Found a new word at zInput[i] that is j-i bytes long.
** Process it into the wordcount table. */
Expand Down
3 changes: 2 additions & 1 deletion tool/fuzzershell.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include <stdarg.h>
#include <ctype.h>
#include "sqlite3.h"
#define ISDIGIT(X) isdigit((unsigned char)(X))

/*
** All global variables are gathered into the "g" singleton.
Expand Down Expand Up @@ -383,7 +384,7 @@ static int integerValue(const char *zArg){
zArg++;
}
}else{
while( isdigit(zArg[0]) ){
while( ISDIGIT(zArg[0]) ){
v = v*10 + zArg[0] - '0';
zArg++;
}
Expand Down
Loading

0 comments on commit 282852e

Please sign in to comment.