Fossil SCM
Improved robustness on the --args option.
Commit
14c14021a0cc9c38c58c1655bdcb3aab84d11363343b9669d7bab3c6bda77121
Parent
810842f18fffe83…
2 files changed
+10
-2
+23
-14
+10
-2
| --- src/blob.c | ||
| +++ src/blob.c | ||
| @@ -279,16 +279,24 @@ | ||
| 279 | 279 | |
| 280 | 280 | /* |
| 281 | 281 | ** Append text or data to the end of a blob. |
| 282 | 282 | */ |
| 283 | 283 | void blob_append(Blob *pBlob, const char *aData, int nData){ |
| 284 | + sqlite3_int64 nNew; | |
| 284 | 285 | assert( aData!=0 || nData==0 ); |
| 285 | 286 | blob_is_init(pBlob); |
| 286 | 287 | if( nData<0 ) nData = strlen(aData); |
| 287 | 288 | 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); | |
| 290 | 298 | if( pBlob->nUsed + nData >= pBlob->nAlloc ){ |
| 291 | 299 | blob_panic(); |
| 292 | 300 | } |
| 293 | 301 | } |
| 294 | 302 | memcpy(&pBlob->aData[pBlob->nUsed], aData, nData); |
| 295 | 303 |
| --- 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 @@ | ||
| 378 | 378 | Blob file = empty_blob; /* Content of the file */ |
| 379 | 379 | Blob line = empty_blob; /* One line of the file */ |
| 380 | 380 | unsigned int nLine; /* Number of lines in the file*/ |
| 381 | 381 | unsigned int i, j, k; /* Loop counters */ |
| 382 | 382 | int n; /* Number of bytes in one line */ |
| 383 | + unsigned int nArg; /* Number of new arguments */ | |
| 383 | 384 | char *z; /* General use string pointer */ |
| 384 | 385 | char **newArgv; /* New expanded g.argv under construction */ |
| 385 | 386 | const char *zFileName; /* input file name */ |
| 386 | 387 | FILE *inFile; /* input FILE */ |
| 387 | 388 | #if defined(_WIN32) |
| @@ -411,34 +412,39 @@ | ||
| 411 | 412 | if( fossil_strcmp(z, "args")==0 ) break; |
| 412 | 413 | } |
| 413 | 414 | if( i>=g.argc-1 ) return; |
| 414 | 415 | |
| 415 | 416 | 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); | |
| 421 | 421 | }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); | |
| 425 | 425 | } |
| 426 | - inFile = NULL; | |
| 427 | 426 | } |
| 427 | + blob_read_from_channel(&file, inFile, -1); | |
| 428 | + if(stdin != inFile){ | |
| 429 | + fclose(inFile); | |
| 430 | + } | |
| 431 | + inFile = NULL; | |
| 428 | 432 | blob_to_utf8_no_bom(&file, 1); |
| 429 | 433 | z = blob_str(&file); |
| 430 | 434 | 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 ); | |
| 432 | 438 | for(j=0; j<i; j++) newArgv[j] = g.argv[j]; |
| 433 | 439 | |
| 434 | 440 | blob_rewind(&file); |
| 435 | 441 | 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 | + } | |
| 440 | 446 | z = blob_buffer(&line); |
| 441 | 447 | if('\n'==z[n-1]){ |
| 442 | 448 | z[n-1] = 0; |
| 443 | 449 | } |
| 444 | 450 | |
| @@ -445,10 +451,13 @@ | ||
| 445 | 451 | if((n>1) && ('\r'==z[n-2])){ |
| 446 | 452 | if(n==2) continue /*empty line*/; |
| 447 | 453 | z[n-2] = 0; |
| 448 | 454 | } |
| 449 | 455 | if(!z[0]) continue; |
| 456 | + if( j>=nArg ){ | |
| 457 | + fossil_fatal("malformed command-line arguments"); | |
| 458 | + } | |
| 450 | 459 | newArgv[j++] = z; |
| 451 | 460 | if( z[0]=='-' ){ |
| 452 | 461 | for(k=1; z[k] && !fossil_isspace(z[k]); k++){} |
| 453 | 462 | if( z[k] ){ |
| 454 | 463 | z[k] = 0; |
| 455 | 464 |
| --- 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 |