mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-08 03:22:21 +03:00
Never use strlen(). Use our own internal sqlite3Strlen30() which is
guaranteed to never overflow an integer. Additional explicit casts to avoid nuisance warning messages. (CVS 6007) FossilOrigin-Name: c872d554930ecf221ac2be5f886d5d67bb35288c
This commit is contained in:
62
src/shell.c
62
src/shell.c
@@ -12,7 +12,7 @@
|
||||
** This file contains code to implement the "sqlite" command line
|
||||
** utility for accessing SQLite databases.
|
||||
**
|
||||
** $Id: shell.c,v 1.191 2008/12/08 18:27:31 drh Exp $
|
||||
** $Id: shell.c,v 1.192 2008/12/10 19:26:24 drh Exp $
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -362,6 +362,16 @@ static const char *modeDescr[] = {
|
||||
*/
|
||||
#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
|
||||
|
||||
/*
|
||||
** Compute a string length that is limited to what can be stored in
|
||||
** lower 30 bits of a 32-bit signed integer.
|
||||
*/
|
||||
int sqlite3Strlen30(const char *z){
|
||||
const char *z2 = z;
|
||||
while( *z2 ){ z2++; }
|
||||
return 0x3fffffff & (int)(z2 - z);
|
||||
}
|
||||
|
||||
/*
|
||||
** Output the given string as a quoted string using SQL quoting conventions.
|
||||
*/
|
||||
@@ -477,7 +487,7 @@ static void output_csv(struct callback_data *p, const char *z, int bSep){
|
||||
fprintf(out,"%s",p->nullvalue);
|
||||
}else{
|
||||
int i;
|
||||
int nSep = strlen(p->separator);
|
||||
int nSep = sqlite3Strlen30(p->separator);
|
||||
for(i=0; z[i]; i++){
|
||||
if( needCsvQuote[((unsigned char*)z)[i]]
|
||||
|| (z[i]==p->separator[0] &&
|
||||
@@ -525,7 +535,7 @@ static int callback(void *pArg, int nArg, char **azArg, char **azCol){
|
||||
int w = 5;
|
||||
if( azArg==0 ) break;
|
||||
for(i=0; i<nArg; i++){
|
||||
int len = strlen(azCol[i] ? azCol[i] : "");
|
||||
int len = sqlite3Strlen30(azCol[i] ? azCol[i] : "");
|
||||
if( len>w ) w = len;
|
||||
}
|
||||
if( p->cnt++>0 ) fprintf(p->out,"\n");
|
||||
@@ -546,9 +556,9 @@ static int callback(void *pArg, int nArg, char **azArg, char **azCol){
|
||||
w = 0;
|
||||
}
|
||||
if( w<=0 ){
|
||||
w = strlen(azCol[i] ? azCol[i] : "");
|
||||
w = sqlite3Strlen30(azCol[i] ? azCol[i] : "");
|
||||
if( w<10 ) w = 10;
|
||||
n = strlen(azArg && azArg[i] ? azArg[i] : p->nullvalue);
|
||||
n = sqlite3Strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
|
||||
if( w<n ) w = n;
|
||||
}
|
||||
if( i<ArraySize(p->actualWidth) ){
|
||||
@@ -580,8 +590,9 @@ static int callback(void *pArg, int nArg, char **azArg, char **azCol){
|
||||
}else{
|
||||
w = 10;
|
||||
}
|
||||
if( p->mode==MODE_Explain && azArg[i] && strlen(azArg[i])>(unsigned)w ){
|
||||
w = strlen(azArg[i]);
|
||||
if( p->mode==MODE_Explain && azArg[i] &&
|
||||
sqlite3Strlen30(azArg[i])>w ){
|
||||
w = sqlite3Strlen30(azArg[i]);
|
||||
}
|
||||
fprintf(p->out,"%-*.*s%s",w,w,
|
||||
azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " ");
|
||||
@@ -728,8 +739,8 @@ static void set_table_name(struct callback_data *p, const char *zName){
|
||||
static char *appendText(char *zIn, char const *zAppend, char quote){
|
||||
int len;
|
||||
int i;
|
||||
int nAppend = strlen(zAppend);
|
||||
int nIn = (zIn?strlen(zIn):0);
|
||||
int nAppend = sqlite3Strlen30(zAppend);
|
||||
int nIn = (zIn?sqlite3Strlen30(zIn):0);
|
||||
|
||||
len = nAppend+nIn+1;
|
||||
if( quote ){
|
||||
@@ -896,7 +907,7 @@ static int run_schema_dump_query(
|
||||
rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
|
||||
if( rc==SQLITE_CORRUPT ){
|
||||
char *zQ2;
|
||||
int len = strlen(zQuery);
|
||||
int len = sqlite3Strlen30(zQuery);
|
||||
if( pzErrMsg ) sqlite3_free(*pzErrMsg);
|
||||
zQ2 = malloc( len+100 );
|
||||
if( zQ2==0 ) return rc;
|
||||
@@ -1070,7 +1081,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
|
||||
/* Process the input line.
|
||||
*/
|
||||
if( nArg==0 ) return rc;
|
||||
n = strlen(azArg[0]);
|
||||
n = sqlite3Strlen30(azArg[0]);
|
||||
c = azArg[0][0];
|
||||
if( c=='b' && n>1 && strncmp(azArg[0], "bail", n)==0 && nArg>1 ){
|
||||
bail_on_error = booleanValue(azArg[1]);
|
||||
@@ -1208,14 +1219,14 @@ static int do_meta_command(char *zLine, struct callback_data *p){
|
||||
int lineno = 0; /* Line number of input file */
|
||||
|
||||
open_db(p);
|
||||
nSep = strlen(p->separator);
|
||||
nSep = sqlite3Strlen30(p->separator);
|
||||
if( nSep==0 ){
|
||||
fprintf(stderr, "non-null separator required for import\n");
|
||||
return 0;
|
||||
}
|
||||
zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
|
||||
if( zSql==0 ) return 0;
|
||||
nByte = strlen(zSql);
|
||||
nByte = sqlite3Strlen30(zSql);
|
||||
rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
|
||||
sqlite3_free(zSql);
|
||||
if( rc ){
|
||||
@@ -1230,7 +1241,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
|
||||
zSql = malloc( nByte + 20 + nCol*2 );
|
||||
if( zSql==0 ) return 0;
|
||||
sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
|
||||
j = strlen(zSql);
|
||||
j = sqlite3Strlen30(zSql);
|
||||
for(i=1; i<nCol; i++){
|
||||
zSql[j++] = ',';
|
||||
zSql[j++] = '?';
|
||||
@@ -1363,7 +1374,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
|
||||
#endif
|
||||
|
||||
if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg>=2 ){
|
||||
int n2 = strlen(azArg[1]);
|
||||
int n2 = sqlite3Strlen30(azArg[1]);
|
||||
if( strncmp(azArg[1],"line",n2)==0
|
||||
||
|
||||
strncmp(azArg[1],"lines",n2)==0 ){
|
||||
@@ -1521,7 +1532,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
|
||||
output_c_string(p->out, p->nullvalue);
|
||||
fprintf(p->out, "\n");
|
||||
fprintf(p->out,"%9.9s: %s\n","output",
|
||||
strlen(p->outfile) ? p->outfile : "stdout");
|
||||
sqlite3Strlen30(p->outfile) ? p->outfile : "stdout");
|
||||
fprintf(p->out,"%9.9s: ", "separator");
|
||||
output_c_string(p->out, p->separator);
|
||||
fprintf(p->out, "\n");
|
||||
@@ -1570,7 +1581,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
|
||||
int nPrintCol, nPrintRow;
|
||||
for(i=1; i<=nRow; i++){
|
||||
if( azResult[i]==0 ) continue;
|
||||
len = strlen(azResult[i]);
|
||||
len = sqlite3Strlen30(azResult[i]);
|
||||
if( len>maxlen ) maxlen = len;
|
||||
}
|
||||
nPrintCol = 80/(maxlen+2);
|
||||
@@ -1717,7 +1728,7 @@ static int process_input(struct callback_data *p, FILE *in){
|
||||
int i;
|
||||
for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
|
||||
if( zLine[i]!=0 ){
|
||||
nSql = strlen(zLine);
|
||||
nSql = sqlite3Strlen30(zLine);
|
||||
zSql = malloc( nSql+1 );
|
||||
if( zSql==0 ){
|
||||
fprintf(stderr, "out of memory\n");
|
||||
@@ -1727,7 +1738,7 @@ static int process_input(struct callback_data *p, FILE *in){
|
||||
startline = lineno;
|
||||
}
|
||||
}else{
|
||||
int len = strlen(zLine);
|
||||
int len = sqlite3Strlen30(zLine);
|
||||
zSql = realloc( zSql, nSql + len + 2 );
|
||||
if( zSql==0 ){
|
||||
fprintf(stderr,"%s: out of memory!\n", Argv0);
|
||||
@@ -1814,7 +1825,7 @@ static char *find_home_dir(void){
|
||||
zDrive = getenv("HOMEDRIVE");
|
||||
zPath = getenv("HOMEPATH");
|
||||
if( zDrive && zPath ){
|
||||
n = strlen(zDrive) + strlen(zPath) + 1;
|
||||
n = sqlite3Strlen30(zDrive) + sqlite3Strlen30(zPath) + 1;
|
||||
home_dir = malloc( n );
|
||||
if( home_dir==0 ) return 0;
|
||||
sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
|
||||
@@ -1827,7 +1838,7 @@ static char *find_home_dir(void){
|
||||
#endif /* !_WIN32_WCE */
|
||||
|
||||
if( home_dir ){
|
||||
int n = strlen(home_dir) + 1;
|
||||
int n = sqlite3Strlen30(home_dir) + 1;
|
||||
char *z = malloc( n );
|
||||
if( z ) memcpy(z, home_dir, n);
|
||||
home_dir = z;
|
||||
@@ -1858,7 +1869,7 @@ static void process_sqliterc(
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
nBuf = strlen(home_dir) + 16;
|
||||
nBuf = sqlite3Strlen30(home_dir) + 16;
|
||||
zBuf = malloc( nBuf );
|
||||
if( zBuf==0 ){
|
||||
fprintf(stderr,"%s: out of memory!\n", Argv0);
|
||||
@@ -2081,8 +2092,11 @@ int main(int argc, char **argv){
|
||||
sqlite3_libversion()
|
||||
);
|
||||
zHome = find_home_dir();
|
||||
if( zHome && (zHistory = malloc(nHistory = strlen(zHome)+20))!=0 ){
|
||||
sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
|
||||
if( zHome ){
|
||||
nHistory = sqlite3Strlen30(zHome) + 20;
|
||||
if( (zHistory = malloc(nHistory))!=0 ){
|
||||
sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
|
||||
}
|
||||
}
|
||||
#if defined(HAVE_READLINE) && HAVE_READLINE==1
|
||||
if( zHistory ) read_history(zHistory);
|
||||
|
||||
Reference in New Issue
Block a user