/ Check-in [e6bb7506]
Login

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

Overview
Comment:Fix a memory leak that could follow an IO error in the zipfile extension. And add other tests.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e6bb750697c3c7ceb5ce41d216e8ef6a1d556822a3b55e0a007b4a03e194a7d9
User & Date: dan 2018-02-01 15:19:54
Context
2018-02-01
15:57
New assert() statements to help ensure that no other errors similar to [343634942dd54ab57b7] ever appear in the code. check-in: 5a70af1e user: drh tags: trunk
15:19
Fix a memory leak that could follow an IO error in the zipfile extension. And add other tests. check-in: e6bb7506 user: dan tags: trunk
01:13
When an index is based on a text representation of a numeric column in the original table, make sure the indexed value uses the canonical text representation of the numeric value in the table. Proposed fix for ticket [343634942dd54ab57b70]. check-in: 88e2ce91 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/misc/zipfile.c.

  2013   2013   void zipfileFinal(sqlite3_context *pCtx){
  2014   2014     ZipfileCtx *p;
  2015   2015     ZipfileEOCD eocd;
  2016   2016     int nZip;
  2017   2017     u8 *aZip;
  2018   2018   
  2019   2019     p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
  2020         -  if( p==0 || p->nEntry==0 ) return;
  2021         -
  2022         -  memset(&eocd, 0, sizeof(eocd));
  2023         -  eocd.nEntry = p->nEntry;
  2024         -  eocd.nEntryTotal = p->nEntry;
  2025         -  eocd.nSize = p->cds.n;
  2026         -  eocd.iOffset = p->body.n;
         2020  +  if( p==0 ) return;
         2021  +  if( p->nEntry>0 ){
         2022  +    memset(&eocd, 0, sizeof(eocd));
         2023  +    eocd.nEntry = p->nEntry;
         2024  +    eocd.nEntryTotal = p->nEntry;
         2025  +    eocd.nSize = p->cds.n;
         2026  +    eocd.iOffset = p->body.n;
  2027   2027   
  2028         -  nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
  2029         -  aZip = (u8*)sqlite3_malloc(nZip);
  2030         -  if( aZip==0 ){
  2031         -    sqlite3_result_error_nomem(pCtx);
  2032         -  }else{
  2033         -    memcpy(aZip, p->body.a, p->body.n);
  2034         -    memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
  2035         -    zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
  2036         -    sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
         2028  +    nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
         2029  +    aZip = (u8*)sqlite3_malloc(nZip);
         2030  +    if( aZip==0 ){
         2031  +      sqlite3_result_error_nomem(pCtx);
         2032  +    }else{
         2033  +      memcpy(aZip, p->body.a, p->body.n);
         2034  +      memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
         2035  +      zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
         2036  +      sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
         2037  +    }
  2037   2038     }
  2038   2039   
  2039   2040     sqlite3_free(p->body.a);
  2040   2041     sqlite3_free(p->cds.a);
  2041   2042   }
  2042   2043   
  2043   2044   

Changes to test/zipfile.test.

   419    419       }
   420    420     } [list {*}{
   421    421       test_unzip/a.txt 946684800
   422    422       test_unzip/b.txt 1000000000 
   423    423       test_unzip/c.txt 1111111000 
   424    424     }]
   425    425   
   426         -  do_test 6.1 {
          426  +  do_execsql_test 6.1 {
          427  +    SELECT name, mtime, data FROM zipfile('test1.zip')
          428  +  } {
          429  +    a.txt 946684800   abc
          430  +    b.txt 1000000000  abc
          431  +    c.txt 1111111000  abc
          432  +  }
          433  +
          434  +  do_test 6.2 {
   427    435       forcedelete test_unzip
   428    436       file mkdir test_unzip
   429    437       exec unzip -d test_unzip test2.zip
   430    438   
   431    439       db eval {
   432    440         SELECT name, mtime 
   433    441         FROM fsdir('test_unzip') WHERE name!='test_unzip'
................................................................................
   434    442         ORDER BY name
   435    443       }
   436    444     } [list {*}{
   437    445       test_unzip/a.txt 946684800
   438    446       test_unzip/b.txt 1000000000 
   439    447       test_unzip/c.txt 1111111000 
   440    448     }]
          449  +
          450  +  do_execsql_test 6.3 {
          451  +    SELECT name, mtime, data FROM zipfile('test2.zip')
          452  +  } {
          453  +    a.txt 946684800   abc
          454  +    b.txt 1000000000  abc
          455  +    c.txt 1111111000  abc
          456  +  }
   441    457   }
   442    458   }
          459  +
          460  +#-------------------------------------------------------------------------
          461  +forcedelete test.zip
          462  +do_test 7.0 {
          463  +  execsql {
          464  +    WITH c(name,data) AS (
          465  +        SELECT '1', randomblob(1000000) UNION ALL
          466  +        SELECT '2', randomblob(1000000) UNION ALL
          467  +        SELECT '3', randomblob(1000000) 
          468  +    )
          469  +    SELECT writefile('test.zip', zipfile(name, data) ) FROM c;
          470  +  }
          471  +
          472  +  list [catch {
          473  +    db eval { SELECT name, data FROM zipfile('test.zip') } {
          474  +      if {$name==2} { close [open test.zip w+] }
          475  +    }
          476  +  } msg] $msg
          477  +} {1 {error in fread()}}
   443    478   
   444    479   
   445    480   finish_test
   446    481   

Changes to test/zipfilefault.test.

    52     52   reset_db
    53     53   load_static_extension db zipfile
    54     54   do_execsql_test 3.0 {
    55     55     CREATE VIRTUAL TABLE setup USING zipfile('test.zip');
    56     56     INSERT INTO setup(name, data) VALUES('a.txt', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa');
    57     57   }
    58     58   
    59         -do_faultsim_test 2 -faults oom* -body {
           59  +do_faultsim_test 3 -faults oom* -body {
    60     60     execsql { SELECT name,data FROM zipfile('test.zip') }
    61     61   } -test {
    62     62     faultsim_test_result {0 {a.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaa}} 
    63     63   }
           64  +
           65  +do_faultsim_test 4 -faults oom* -body {
           66  +  execsql {
           67  +    WITH c(n, d) AS (
           68  +      SELECT 1, 'aaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbb'
           69  +    )
           70  +    SELECT name, data FROM zipfile(
           71  +      (SELECT zipfile(n, d) FROM c)
           72  +    );
           73  +  }
           74  +} -test {
           75  +  faultsim_test_result {0 {1 aaaaaaaaaaabbbbbbbbbbaaaaaaaaaabbbbbbbbbb}}
           76  +}
    64     77   
    65     78   
    66     79   finish_test
    67     80