]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix a problem in the zipfile module causing it to generate incorrect
authordan <dan@noemail.net>
Mon, 15 Jan 2018 19:00:35 +0000 (19:00 +0000)
committerdan <dan@noemail.net>
Mon, 15 Jan 2018 19:00:35 +0000 (19:00 +0000)
checksums. Remove the ability to insert compressed data into a zip archive.

FossilOrigin-Name: b0b7d0363acf38c2178e2d3041d8ce2a0de061a51caa64670dbf539ee6d4356b

ext/misc/zipfile.c
manifest
manifest.uuid
test/zipfile.test

index 27f55f6c14735630ea6c34ff2a426acb75b89470..f17b3735fb92f457f7a25d7b134fd1d6347813fb 100644 (file)
@@ -805,9 +805,6 @@ static int zipfileColumn(
 ){
   ZipfileCsr *pCsr = (ZipfileCsr*)cur;
   int rc = SQLITE_OK;
-  if( i>=3 && sqlite3_vtab_nochange(ctx) ){
-    return SQLITE_OK;
-  }
   switch( i ){
     case 0:   /* name */
       sqlite3_result_text(ctx, pCsr->cds.zFile, -1, SQLITE_TRANSIENT);
@@ -826,10 +823,13 @@ static int zipfileColumn(
       break;
     }
     case 3: { /* sz */
-      sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
+      if( sqlite3_vtab_nochange(ctx)==0 ){
+        sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
+      }
       break;
     }
     case 4:   /* rawdata */
+      if( sqlite3_vtab_nochange(ctx) ) break;
     case 5: { /* data */
       if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){
         int sz = pCsr->cds.szCompressed;
@@ -872,7 +872,7 @@ static int zipfileColumn(
       break;
   }
 
-  return SQLITE_OK;
+  return rc;
 }
 
 /*
@@ -908,7 +908,8 @@ static int zipfileReadEOCD(
   fseek(pFile, 0, SEEK_END);
   szFile = (i64)ftell(pFile);
   if( szFile==0 ){
-    return SQLITE_EMPTY;
+    memset(pEOCD, 0, sizeof(ZipfileEOCD));
+    return SQLITE_OK;
   }
   nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
   iOff = szFile - nRead;
@@ -986,11 +987,12 @@ static int zipfileFilter(
     }else{
       rc = zipfileReadEOCD(pTab, pCsr->pFile, &pCsr->eocd);
       if( rc==SQLITE_OK ){
-        pCsr->iNextOff = pCsr->eocd.iOffset;
-        rc = zipfileNext(cur);
-      }else if( rc==SQLITE_EMPTY ){
-        rc = SQLITE_OK;
-        pCsr->bEof = 1;
+        if( pCsr->eocd.nEntry==0 ){
+          pCsr->bEof = 1;
+        }else{
+          pCsr->iNextOff = pCsr->eocd.iOffset;
+          rc = zipfileNext(cur);
+        }
       }
     }
   }else{
@@ -1067,7 +1069,7 @@ static int zipfileLoadDirectory(ZipfileTab *pTab){
   int rc;
 
   rc = zipfileReadEOCD(pTab, pTab->pWriteFd, &eocd);
-  if( rc==SQLITE_OK ){
+  if( rc==SQLITE_OK && eocd.nEntry>0 ){
     int i;
     int iOff = 0;
     u8 *aBuf = sqlite3_malloc(eocd.nSize);
@@ -1112,8 +1114,6 @@ static int zipfileLoadDirectory(ZipfileTab *pTab){
     }
 
     sqlite3_free(aBuf);
-  }else if( rc==SQLITE_EMPTY ){
-    rc = SQLITE_OK;
   }
 
   return rc;
@@ -1294,6 +1294,7 @@ static int zipfileUpdate(
   ZipfileCDS cds;                 /* New Central Directory Structure entry */
   ZipfileEntry *pOld = 0;
   int bIsDir = 0;
+  u32 iCrc32 = 0;
 
   assert( pTab->zFile );
   assert( pTab->pWriteFd );
@@ -1312,42 +1313,18 @@ static int zipfileUpdate(
     if( nVal==1 ) return SQLITE_OK;
   }
 
-  if( sqlite3_value_nochange(apVal[5]) && sqlite3_value_nochange(apVal[6])
-   && sqlite3_value_nochange(apVal[7]) && sqlite3_value_nochange(apVal[8])
+  /* Check that "sz" and "rawdata" are both NULL: */
+  if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
+   || sqlite3_value_type(apVal[6])!=SQLITE_NULL
   ){
-    /* Reuse the data from the existing entry. */
-    FILE *pFile = pTab->pWriteFd;
-    zipfileReadCDS(pOld->aCdsEntry, &cds);
-
-    bIsDir = ((cds.iExternalAttr>>16) & S_IFDIR) ? 1 : 0;
-    sz = cds.szUncompressed;
-    iMethod = cds.iCompression;
-    if( sz>0 ){
-      char **pzErr = &pTab->base.zErrMsg;
-      ZipfileLFH lfh;
-      rc = zipfileReadLFH(pFile, cds.iOffset, pTab->aBuffer, &lfh, pzErr);
-      if( rc==SQLITE_OK ){
-        nData = lfh.szCompressed;
-        pData = pFree = sqlite3_malloc(nData);
-        if( pFree==NULL ){
-          rc = SQLITE_NOMEM;
-        }else{
-          i64 iRead = cds.iOffset + ZIPFILE_LFH_FIXED_SZ + lfh.nFile+lfh.nExtra;
-          rc = zipfileReadData(pFile, pFree, nData, iRead, pzErr);
-        }
-      }
-    }
-  }else{
-    int mNull;
-    mNull = (sqlite3_value_type(apVal[5])==SQLITE_NULL ? 0x0 : 0x8)  /* sz */
-      + (sqlite3_value_type(apVal[6])==SQLITE_NULL ? 0x0 : 0x4)  /* rawdata */
-      + (sqlite3_value_type(apVal[7])==SQLITE_NULL ? 0x0 : 0x2)  /* data */
-      + (sqlite3_value_type(apVal[8])==SQLITE_NULL ? 0x0 : 0x1); /* method */
-    if( mNull==0x00 ){     
-      /* All four are NULL - this must be a directory */
+    rc = SQLITE_CONSTRAINT;
+  }
+
+  if( rc==SQLITE_OK ){
+    if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
+      /* data=NULL. A directory */
       bIsDir = 1;
-    }
-    else if( mNull==0x2 || mNull==0x3 ){
+    }else{
       /* Value specified for "data", and possibly "method". This must be
       ** a regular file or a symlink. */
       const u8 *aIn = sqlite3_value_blob(apVal[7]);
@@ -1370,25 +1347,9 @@ static int zipfileUpdate(
             nData = nCmp;
           }
         }
+        iCrc32 = crc32(0, aIn, nIn);
       }
     }
-    else if( mNull==0x0D ){
-      /* Values specified for "sz", "rawdata" and "method". In other words,
-      ** pre-compressed data is being inserted.  */
-      pData = sqlite3_value_blob(apVal[6]);
-      nData = sqlite3_value_bytes(apVal[6]);
-      sz = sqlite3_value_int(apVal[5]);
-      iMethod = sqlite3_value_int(apVal[8]);
-      if( iMethod<0 || iMethod>65535 ){
-        pTab->base.zErrMsg = sqlite3_mprintf(
-            "zipfile: invalid compression method: %d", iMethod
-            );
-        rc = SQLITE_ERROR;
-      }
-    }
-    else{
-      rc = SQLITE_CONSTRAINT;
-    }
   }
 
   if( rc==SQLITE_OK ){
@@ -1445,7 +1406,7 @@ static int zipfileUpdate(
     cds.flags = ZIPFILE_NEWENTRY_FLAGS;
     cds.iCompression = (u16)iMethod;
     zipfileMtimeToDos(&cds, (u32)mTime);
-    cds.crc32 = crc32(0, pData, nData);
+    cds.crc32 = iCrc32;
     cds.szCompressed = nData;
     cds.szUncompressed = (u32)sz;
     cds.iExternalAttr = (mode<<16);
index f7654f5e02014b5e691342d6a2c84063b6664e3f..47646bc8debaa6d28df2ed3387f35c8a5fe310cb 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\szipfile\sproblem\swith\sextracting\szero\slength\sfiles\scompressed\susing\ndeflate.
-D 2018-01-15T15:49:46.517
+C Fix\sa\sproblem\sin\sthe\szipfile\smodule\scausing\sit\sto\sgenerate\sincorrect\nchecksums.\sRemove\sthe\sability\sto\sinsert\scompressed\sdata\sinto\sa\szip\sarchive.
+D 2018-01-15T19:00:35.051
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in 38f84f301cbef443b2d269f67a74b8cc536469831f70df7c3e912acc04932cc2
@@ -303,7 +303,7 @@ F ext/misc/vfsstat.c bf10ef0bc51e1ad6756629e1edb142f7a8db1178
 F ext/misc/vtablog.c 31d0d8f4406795679dcd3a67917c213d3a2a5fb3ea5de35f6e773491ed7e13c9
 F ext/misc/vtshim.c 1976e6dd68dd0d64508c91a6dfab8e75f8aaf6cd
 F ext/misc/wholenumber.c 784b12543d60702ebdd47da936e278aa03076212
-F ext/misc/zipfile.c 46171a19439d0c76acf48770e736c281536f160d3a5a96a0511e34402e262fac
+F ext/misc/zipfile.c 7e48d2947a1fe71d22e54f3b499d6d0581efa105033683585d3dbde76cffaed7
 F ext/rbu/rbu.c ea7d1b7eb44c123a2a619332e19fe5313500705c4a58aaa1887905c0d83ffc2e
 F ext/rbu/rbu1.test 43836fac8c7179a358eaf38a8a1ef3d6e6285842
 F ext/rbu/rbu10.test 1846519a438697f45e9dcb246908af81b551c29e1078d0304fae83f1fed7e9ee
@@ -1600,7 +1600,7 @@ F test/wordcount.c cb589cec469a1d90add05b1f8cee75c7210338d87a5afd65260ed5c0f4bbf
 F test/writecrash.test f1da7f7adfe8d7f09ea79b42e5ca6dcc41102f27f8e334ad71539501ddd910cc
 F test/zeroblob.test 3857870fe681b8185654414a9bccfde80b62a0fa
 F test/zerodamage.test 9c41628db7e8d9e8a0181e59ea5f189df311a9f6ce99cc376dc461f66db6f8dc
-F test/zipfile.test 61c6daf74f71f6d0c4d925cd7e23eb9ffe642491f8927b26aebba476d3244e50
+F test/zipfile.test 71a9d37bb928a1dcee6ab624e8be7bca9f807364440d20fc84363c44ab2f4ac5
 F tool/GetFile.cs a15e08acb5dd7539b75ba23501581d7c2b462cb5
 F tool/GetTclKit.bat 8995df40c4209808b31f24de0b58f90930239a234f7591e3675d45bfbb990c5d
 F tool/Replace.cs 02c67258801c2fb5f63231e0ac0f220b4b36ba91
@@ -1699,7 +1699,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 8151913a3987f4dd2d6efee046727f5fa9b6f11d5d3867ea8f512c03a212ac2b
-R d9864f9f3d3219c31b427d2f3aa9a193
+P cf64087224aff1a2fe169d23996d9e5ed8d86459c655eb5d0bace0466a557ec6
+R 5e0c54dfbab7bd3d719e333800776169
 U dan
-Z 4b1456c73d542678caf828eee79b5bff
+Z a41cf66d60fe2e5069108d980e3c47d6
index 19ee4478a0fbd5a0fbf2a993c744a867355fed31..78a1e43434f7b40ec40ad89e0039f593bf8d8dcc 100644 (file)
@@ -1 +1 @@
-cf64087224aff1a2fe169d23996d9e5ed8d86459c655eb5d0bace0466a557ec6
\ No newline at end of file
+b0b7d0363acf38c2178e2d3041d8ce2a0de061a51caa64670dbf539ee6d4356b
\ No newline at end of file
index e766cb31bf8275692726e5b3e04f686045802d58..e246c9ab18353881179dac9b4ea14d8287ade159 100644 (file)
@@ -36,13 +36,22 @@ do_execsql_test 1.0 {
   6 method {} 0 {} 0
 }
 
-do_execsql_test 1.1.1 {
+do_catchsql_test 1.1.0.1 {
   INSERT INTO zz(name, mode, mtime, sz, rawdata, method) 
   VALUES('f.txt', '-rw-r--r--', 1000000000, 5, 'abcde', 0);
-}
-do_execsql_test 1.1.2 {
+} {1 {constraint failed}}
+do_catchsql_test 1.1.0.1 {
   INSERT INTO zz(name, mtime, sz, rawdata, method) 
   VALUES('g.txt', 1000000002, 5, '12345', 0);
+} {1 {constraint failed}}
+
+do_execsql_test 1.1.1 {
+  INSERT INTO zz(name, mode, mtime, data, method) 
+  VALUES('f.txt', '-rw-r--r--', 1000000000, 'abcde', 0);
+}
+do_execsql_test 1.1.2 {
+  INSERT INTO zz(name, mode, mtime, data, method) 
+  VALUES('g.txt', NULL, 1000000002, '12345', 0);
 }
 
 do_execsql_test 1.2 {