Fossil

Check-in [fde20bc03c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fixes for reserved names case sensitivity, coding style adjustments, more tests.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | sec2020
Files: files | file ages | folders
SHA3-256: fde20bc03c500809f5a544a3b4764096b987d527aa95bac69f1c929f8b4919f9
User & Date: mistachkin 2020-08-17 22:22:26.992
Context
2020-08-19
01:07
Cherrypick key fixes from the sec2020 branch in order to devise a minimal patch to get us to version 2.12.1. check-in: fe1264d35d user: drh tags: sec2020-2.12-patch
2020-08-17
22:27
Simplify error message. check-in: 1bb0b3a8f3 user: mistachkin tags: sec2020
22:22
Fixes for reserved names case sensitivity, coding style adjustments, more tests. check-in: fde20bc03c user: mistachkin tags: sec2020
20:51
Add tests for the reserved names. check-in: df720b28fc user: mistachkin tags: sec2020
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/file.c.
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463

2464
2465
2466
2467


2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
*/
const char * file_extension(const char *zFileName){
  const char * zExt = zFileName ? strrchr(zFileName, '.') : 0;
  return zExt ? &zExt[1] : 0;
}

/*
** Returns true if the given filename ends with any of fossil's
** checkout database filenames: _FOSSIL_ or .fslckout. Specifically,
** it returns 1 if it's an exact match and 2 if it's the tail match
** on a longer input.
**
** zFilename must, for efficiency's sake, be a
** canonicalized/normalized name, e.g. using only '/' as directory
** separators.
**
** nFilename must be the strlen of zFilename. If it is negative,
** strlen() is used to calculate it.
*/
int filename_is_ckout_db(const char *zFilename, int nFilename){
  const char *zEnd;  /* one-after-the-end of zFilename */
  int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */

  assert(zFilename && "API misuse");
  if(nFilename<0) nFilename = (int)strlen(zFilename);
  if(nFilename<8/*strlen _FOSSIL_*/) return 0;
  zEnd = zFilename + nFilename;
  if(nFilename>=12/*strlen _FOSSIL_-(shm|wal)*/){
    /* Check for (-wal, -shm, -journal) suffixes, with an eye towards
    ** runtime speed. */
    if('-'==zEnd[-4]){
      if(fossil_stricmp("wal", &zEnd[-3])
         && fossil_stricmp("shm", &zEnd[-3])){
        return 0;
      }
      gotSuffix = 4;
    }else if(nFilename>=16/*strlen _FOSSIL_-journal*/ && '-'==zEnd[-8]){
      if(fossil_stricmp("journal",&zEnd[-7])){
        return 0;
      }
      gotSuffix = 8;
    }
    if(gotSuffix){
      assert(4==gotSuffix || 8==gotSuffix);
      zEnd -= gotSuffix;
      nFilename -= gotSuffix;
      gotSuffix = 1;
    }
    assert(nFilename>=8 && "strlen _FOSSIL_");
    assert(gotSuffix==0 || gotSuffix==1);
  }
  switch(zEnd[-1]){
    case '_': {
      return fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8)
        ? 0 : (8==nFilename
               ? 1
               : ('/'==zEnd[-9] ? 2 : gotSuffix));
    }

    case 't': {
      return (nFilename<9
              || '.'!=zEnd[-9]
              || fossil_strnicmp(".fslckout", &zEnd[-9], 9))


        ? 0 : (9==nFilename
               ? 1
               : ('/'==zEnd[-10] ? 2 : gotSuffix));
    }
    default: {
      return 0;
    }
  }
}

/*
** COMMAND: test-is-ckout-db
**
** Usage: %fossil test-is-ckout-db FILENAMES...
**
** Passes each given name to filename_is_ckout_db() and outputs one
** line per file: the result value of that function followed by the
** name.
*/
void test_is_ckout_name_cmd(void){
  int i;

  if(g.argc<3){
    usage("FILENAME_1 [...FILENAME_N]");
  }
  for( i = 2; i < g.argc; ++i ){
    const int check = filename_is_ckout_db(g.argv[i], -1);
    fossil_print("%d %s\n", check, g.argv[i]);
  }
}







|
|
<
|

|
|
<

|
|

|



|
|
|

|


|
|
|



|
|
<
<


|
|




|
|

|
|
|
|
<
|

>
|
|
<
|
>
>
|
<
|

|






|



|



|






|



2406
2407
2408
2409
2410
2411
2412
2413
2414

2415
2416
2417
2418

2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441


2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456

2457
2458
2459
2460
2461

2462
2463
2464
2465

2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
*/
const char * file_extension(const char *zFileName){
  const char * zExt = zFileName ? strrchr(zFileName, '.') : 0;
  return zExt ? &zExt[1] : 0;
}

/*
** Returns non-zero if the specified file name ends with any reserved name,
** e.g.: _FOSSIL_ or .fslckout.  Specifically, it returns 1 for exact match

** or 2 for a tail match on a longer file name.
**
** For the sake of efficiency, zFilename must be a canonical name, e.g. an
** absolute path using only forward slash ('/') as a directory separator.

**
** nFilename must be the length of zFilename.  When negative, strlen() will
** be used to calculate it.
*/
int file_is_reserved_name(const char *zFilename, int nFilename){
  const char *zEnd;  /* one-after-the-end of zFilename */
  int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */

  assert( zFilename && "API misuse" );
  if( nFilename<0 ) nFilename = (int)strlen(zFilename);
  if( nFilename<8 ) return 0; /* strlen("_FOSSIL_") */
  zEnd = zFilename + nFilename;
  if( nFilename>=12 ){ /* strlen("_FOSSIL_-(shm|wal)") */
    /* Check for (-wal, -shm, -journal) suffixes, with an eye towards
    ** runtime speed. */
    if( zEnd[-4]=='-' ){
      if( fossil_strnicmp("wal", &zEnd[-3], 3)
       && fossil_strnicmp("shm", &zEnd[-3], 3) ){
        return 0;
      }
      gotSuffix = 4;
    }else if( nFilename>=16 && zEnd[-8]=='-' ){ /*strlen(_FOSSIL_-journal) */
      if( fossil_strnicmp("journal", &zEnd[-7], 7) ) return 0;


      gotSuffix = 8;
    }
    if( gotSuffix ){
      assert( 4==gotSuffix || 8==gotSuffix );
      zEnd -= gotSuffix;
      nFilename -= gotSuffix;
      gotSuffix = 1;
    }
    assert( nFilename>=8 && "strlen(_FOSSIL_)" );
    assert( gotSuffix==0 || gotSuffix==1 );
  }
  switch( zEnd[-1] ){
    case '_':{
      if( fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8) ) return 0;
      if( 8==nFilename ) return 1;

      return zEnd[-9]=='/' ? 2 : gotSuffix;
    }
    case 'T':
    case 't':{
      if( nFilename<9 || zEnd[-9]!='.'

       || fossil_strnicmp(".fslckout", &zEnd[-9], 9) ){
        return 0; 
      }
      if( 9==nFilename ) return 1;

      return zEnd[-10]=='/' ? 2 : gotSuffix;
    }
    default:{
      return 0;
    }
  }
}

/*
** COMMAND: test-is-reserved-name
**
** Usage: %fossil test-is-ckout-db FILENAMES...
**
** Passes each given name to file_is_reserved_name() and outputs one
** line per file: the result value of that function followed by the
** name.
*/
void test_is_reserved_name_cmd(void){
  int i;

  if(g.argc<3){
    usage("FILENAME_1 [...FILENAME_N]");
  }
  for( i = 2; i < g.argc; ++i ){
    const int check = file_is_reserved_name(g.argv[i], -1);
    fossil_print("%d %s\n", check, g.argv[i]);
  }
}
Changes to src/manifest.c.
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
      case 'F': {
        char *zName, *zPerm, *zPriorName;
        zName = next_token(&x,0);
        if( zName==0 ) SYNTAX("missing filename on F-card");
        defossilize(zName);
        if( !file_is_simple_pathname_nonstrict(zName) ){
          SYNTAX("F-card filename is not a simple path");
        }else if( filename_is_ckout_db(zName,-1) ){
          SYNTAX("F-card contains reserved name of a checkout db.");
        }
        zUuid = next_token(&x, &sz);
        if( p->zBaseline==0 || zUuid!=0 ){
          if( zUuid==0 ) SYNTAX("missing hash on F-card");
          if( !hname_validate(zUuid,sz) ){
            SYNTAX("F-card hash invalid");







|







630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
      case 'F': {
        char *zName, *zPerm, *zPriorName;
        zName = next_token(&x,0);
        if( zName==0 ) SYNTAX("missing filename on F-card");
        defossilize(zName);
        if( !file_is_simple_pathname_nonstrict(zName) ){
          SYNTAX("F-card filename is not a simple path");
        }else if( file_is_reserved_name(zName,-1) ){
          SYNTAX("F-card contains reserved name of a checkout db.");
        }
        zUuid = next_token(&x, &sz);
        if( p->zBaseline==0 || zUuid!=0 ){
          if( zUuid==0 ) SYNTAX("missing hash on F-card");
          if( !hname_validate(zUuid,sz) ){
            SYNTAX("F-card hash invalid");
Changes to test/reserved-names.test.
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85




















86
87
88
89
90

foreach reserved_names_test $reserved_names_tests {
  incr testNo

  set reserved_result [lindex $reserved_names_test 0]
  set reserved_name [lindex $reserved_names_test 1]

  fossil test-is-ckout-db $reserved_name

  test reserved-result-$testNo {
    [lindex [normalize_result] 0] eq $reserved_result
  }

  test reserved-name-$testNo {
    [lindex [normalize_result] 1] eq $reserved_name
  }




















}

###############################################################################

test_cleanup







|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

foreach reserved_names_test $reserved_names_tests {
  incr testNo

  set reserved_result [lindex $reserved_names_test 0]
  set reserved_name [lindex $reserved_names_test 1]

  fossil test-is-reserved-name $reserved_name

  test reserved-result-$testNo {
    [lindex [normalize_result] 0] eq $reserved_result
  }

  test reserved-name-$testNo {
    [lindex [normalize_result] 1] eq $reserved_name
  }

  fossil test-is-reserved-name [string toupper $reserved_name]

  test reserved-result-upper-$testNo {
    [lindex [normalize_result] 0] eq $reserved_result
  }

  test reserved-name-upper-$testNo {
    [lindex [normalize_result] 1] eq [string toupper $reserved_name]
  }

  fossil test-is-reserved-name [string tolower $reserved_name]

  test reserved-result-lower-$testNo {
    [lindex [normalize_result] 0] eq $reserved_result
  }

  test reserved-name-lower-$testNo {
    [lindex [normalize_result] 1] eq [string tolower $reserved_name]
  }
}

###############################################################################

test_cleanup