Fossil SCM

Improved robustness on the --args option.

drh 2019-01-21 20:04 trunk
Commit 14c14021a0cc9c38c58c1655bdcb3aab84d11363343b9669d7bab3c6bda77121
2 files changed +10 -2 +23 -14
+10 -2
--- src/blob.c
+++ src/blob.c
@@ -279,16 +279,24 @@
279279
280280
/*
281281
** Append text or data to the end of a blob.
282282
*/
283283
void blob_append(Blob *pBlob, const char *aData, int nData){
284
+ sqlite3_int64 nNew;
284285
assert( aData!=0 || nData==0 );
285286
blob_is_init(pBlob);
286287
if( nData<0 ) nData = strlen(aData);
287288
if( nData==0 ) return;
288
- if( pBlob->nUsed + nData >= pBlob->nAlloc ){
289
- pBlob->xRealloc(pBlob, pBlob->nUsed + nData + pBlob->nAlloc + 100);
289
+ nNew = pBlob->nUsed;
290
+ nNew += nData;
291
+ if( nNew >= pBlob->nAlloc ){
292
+ nNew += pBlob->nAlloc;
293
+ nNew += 100;
294
+ if( nNew>=0x7fff0000 ){
295
+ blob_panic();
296
+ }
297
+ pBlob->xRealloc(pBlob, (int)nNew);
290298
if( pBlob->nUsed + nData >= pBlob->nAlloc ){
291299
blob_panic();
292300
}
293301
}
294302
memcpy(&pBlob->aData[pBlob->nUsed], aData, nData);
295303
--- src/blob.c
+++ src/blob.c
@@ -279,16 +279,24 @@
279
280 /*
281 ** Append text or data to the end of a blob.
282 */
283 void blob_append(Blob *pBlob, const char *aData, int nData){
 
284 assert( aData!=0 || nData==0 );
285 blob_is_init(pBlob);
286 if( nData<0 ) nData = strlen(aData);
287 if( nData==0 ) return;
288 if( pBlob->nUsed + nData >= pBlob->nAlloc ){
289 pBlob->xRealloc(pBlob, pBlob->nUsed + nData + pBlob->nAlloc + 100);
 
 
 
 
 
 
 
290 if( pBlob->nUsed + nData >= pBlob->nAlloc ){
291 blob_panic();
292 }
293 }
294 memcpy(&pBlob->aData[pBlob->nUsed], aData, nData);
295
--- src/blob.c
+++ src/blob.c
@@ -279,16 +279,24 @@
279
280 /*
281 ** Append text or data to the end of a blob.
282 */
283 void blob_append(Blob *pBlob, const char *aData, int nData){
284 sqlite3_int64 nNew;
285 assert( aData!=0 || nData==0 );
286 blob_is_init(pBlob);
287 if( nData<0 ) nData = strlen(aData);
288 if( nData==0 ) return;
289 nNew = pBlob->nUsed;
290 nNew += nData;
291 if( nNew >= pBlob->nAlloc ){
292 nNew += pBlob->nAlloc;
293 nNew += 100;
294 if( nNew>=0x7fff0000 ){
295 blob_panic();
296 }
297 pBlob->xRealloc(pBlob, (int)nNew);
298 if( pBlob->nUsed + nData >= pBlob->nAlloc ){
299 blob_panic();
300 }
301 }
302 memcpy(&pBlob->aData[pBlob->nUsed], aData, nData);
303
+23 -14
--- src/main.c
+++ src/main.c
@@ -378,10 +378,11 @@
378378
Blob file = empty_blob; /* Content of the file */
379379
Blob line = empty_blob; /* One line of the file */
380380
unsigned int nLine; /* Number of lines in the file*/
381381
unsigned int i, j, k; /* Loop counters */
382382
int n; /* Number of bytes in one line */
383
+ unsigned int nArg; /* Number of new arguments */
383384
char *z; /* General use string pointer */
384385
char **newArgv; /* New expanded g.argv under construction */
385386
const char *zFileName; /* input file name */
386387
FILE *inFile; /* input FILE */
387388
#if defined(_WIN32)
@@ -411,34 +412,39 @@
411412
if( fossil_strcmp(z, "args")==0 ) break;
412413
}
413414
if( i>=g.argc-1 ) return;
414415
415416
zFileName = g.argv[i+1];
416
- inFile = (0==strcmp("-",zFileName))
417
- ? stdin
418
- : fossil_fopen(zFileName,"rb");
419
- if(!inFile){
420
- fossil_fatal("Cannot open -args file [%s]", zFileName);
417
+ if( strcmp(zFileName,"-")==0 ){
418
+ inFile = stdin;
419
+ }else if( !file_isfile(zFileName, ExtFILE) ){
420
+ fossil_fatal("Not an ordinary file: \"%s\"", zFileName);
421421
}else{
422
- blob_read_from_channel(&file, inFile, -1);
423
- if(stdin != inFile){
424
- fclose(inFile);
422
+ inFile = fossil_fopen(zFileName,"rb");
423
+ if( inFile==0 ){
424
+ fossil_fatal("Cannot open -args file [%s]", zFileName);
425425
}
426
- inFile = NULL;
427426
}
427
+ blob_read_from_channel(&file, inFile, -1);
428
+ if(stdin != inFile){
429
+ fclose(inFile);
430
+ }
431
+ inFile = NULL;
428432
blob_to_utf8_no_bom(&file, 1);
429433
z = blob_str(&file);
430434
for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
431
- newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
435
+ if( nLine>100000000 ) fossil_fatal("too many command-line arguments");
436
+ nArg = g.argc + nLine*2;
437
+ newArgv = fossil_malloc( sizeof(char*)*nArg );
432438
for(j=0; j<i; j++) newArgv[j] = g.argv[j];
433439
434440
blob_rewind(&file);
435441
while( (n = blob_line(&file, &line))>0 ){
436
- if( n<1 ) continue
437
- /**
438
- ** Reminder: corner-case: a line with 1 byte and no newline.
439
- */;
442
+ if( n<1 ){
443
+ /* Reminder: corner-case: a line with 1 byte and no newline. */
444
+ continue;
445
+ }
440446
z = blob_buffer(&line);
441447
if('\n'==z[n-1]){
442448
z[n-1] = 0;
443449
}
444450
@@ -445,10 +451,13 @@
445451
if((n>1) && ('\r'==z[n-2])){
446452
if(n==2) continue /*empty line*/;
447453
z[n-2] = 0;
448454
}
449455
if(!z[0]) continue;
456
+ if( j>=nArg ){
457
+ fossil_fatal("malformed command-line arguments");
458
+ }
450459
newArgv[j++] = z;
451460
if( z[0]=='-' ){
452461
for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
453462
if( z[k] ){
454463
z[k] = 0;
455464
--- src/main.c
+++ src/main.c
@@ -378,10 +378,11 @@
378 Blob file = empty_blob; /* Content of the file */
379 Blob line = empty_blob; /* One line of the file */
380 unsigned int nLine; /* Number of lines in the file*/
381 unsigned int i, j, k; /* Loop counters */
382 int n; /* Number of bytes in one line */
 
383 char *z; /* General use string pointer */
384 char **newArgv; /* New expanded g.argv under construction */
385 const char *zFileName; /* input file name */
386 FILE *inFile; /* input FILE */
387 #if defined(_WIN32)
@@ -411,34 +412,39 @@
411 if( fossil_strcmp(z, "args")==0 ) break;
412 }
413 if( i>=g.argc-1 ) return;
414
415 zFileName = g.argv[i+1];
416 inFile = (0==strcmp("-",zFileName))
417 ? stdin
418 : fossil_fopen(zFileName,"rb");
419 if(!inFile){
420 fossil_fatal("Cannot open -args file [%s]", zFileName);
421 }else{
422 blob_read_from_channel(&file, inFile, -1);
423 if(stdin != inFile){
424 fclose(inFile);
425 }
426 inFile = NULL;
427 }
 
 
 
 
 
428 blob_to_utf8_no_bom(&file, 1);
429 z = blob_str(&file);
430 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
431 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
 
 
432 for(j=0; j<i; j++) newArgv[j] = g.argv[j];
433
434 blob_rewind(&file);
435 while( (n = blob_line(&file, &line))>0 ){
436 if( n<1 ) continue
437 /**
438 ** Reminder: corner-case: a line with 1 byte and no newline.
439 */;
440 z = blob_buffer(&line);
441 if('\n'==z[n-1]){
442 z[n-1] = 0;
443 }
444
@@ -445,10 +451,13 @@
445 if((n>1) && ('\r'==z[n-2])){
446 if(n==2) continue /*empty line*/;
447 z[n-2] = 0;
448 }
449 if(!z[0]) continue;
 
 
 
450 newArgv[j++] = z;
451 if( z[0]=='-' ){
452 for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
453 if( z[k] ){
454 z[k] = 0;
455
--- src/main.c
+++ src/main.c
@@ -378,10 +378,11 @@
378 Blob file = empty_blob; /* Content of the file */
379 Blob line = empty_blob; /* One line of the file */
380 unsigned int nLine; /* Number of lines in the file*/
381 unsigned int i, j, k; /* Loop counters */
382 int n; /* Number of bytes in one line */
383 unsigned int nArg; /* Number of new arguments */
384 char *z; /* General use string pointer */
385 char **newArgv; /* New expanded g.argv under construction */
386 const char *zFileName; /* input file name */
387 FILE *inFile; /* input FILE */
388 #if defined(_WIN32)
@@ -411,34 +412,39 @@
412 if( fossil_strcmp(z, "args")==0 ) break;
413 }
414 if( i>=g.argc-1 ) return;
415
416 zFileName = g.argv[i+1];
417 if( strcmp(zFileName,"-")==0 ){
418 inFile = stdin;
419 }else if( !file_isfile(zFileName, ExtFILE) ){
420 fossil_fatal("Not an ordinary file: \"%s\"", zFileName);
 
421 }else{
422 inFile = fossil_fopen(zFileName,"rb");
423 if( inFile==0 ){
424 fossil_fatal("Cannot open -args file [%s]", zFileName);
425 }
 
426 }
427 blob_read_from_channel(&file, inFile, -1);
428 if(stdin != inFile){
429 fclose(inFile);
430 }
431 inFile = NULL;
432 blob_to_utf8_no_bom(&file, 1);
433 z = blob_str(&file);
434 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
435 if( nLine>100000000 ) fossil_fatal("too many command-line arguments");
436 nArg = g.argc + nLine*2;
437 newArgv = fossil_malloc( sizeof(char*)*nArg );
438 for(j=0; j<i; j++) newArgv[j] = g.argv[j];
439
440 blob_rewind(&file);
441 while( (n = blob_line(&file, &line))>0 ){
442 if( n<1 ){
443 /* Reminder: corner-case: a line with 1 byte and no newline. */
444 continue;
445 }
446 z = blob_buffer(&line);
447 if('\n'==z[n-1]){
448 z[n-1] = 0;
449 }
450
@@ -445,10 +451,13 @@
451 if((n>1) && ('\r'==z[n-2])){
452 if(n==2) continue /*empty line*/;
453 z[n-2] = 0;
454 }
455 if(!z[0]) continue;
456 if( j>=nArg ){
457 fossil_fatal("malformed command-line arguments");
458 }
459 newArgv[j++] = z;
460 if( z[0]=='-' ){
461 for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
462 if( z[k] ){
463 z[k] = 0;
464

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button