[or-cvs] Allow tor_gzip_uncompress to extract as much as possible fr...

Nick Mathewson nickm at seul.org
Thu Oct 13 22:48:12 UTC 2005


Update of /home/or/cvsroot/tor/src/or
In directory moria:/tmp/cvs-serv32655/src/or

Modified Files:
	directory.c test.c 
Log Message:
Allow tor_gzip_uncompress to extract as much as possible from truncated compressed data.  Also, fix a bug where truncated compressed data could break tor_gzip_uncompress. [This last part is a backport candidate.]

Index: directory.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/directory.c,v
retrieving revision 1.304
retrieving revision 1.305
diff -u -d -r1.304 -r1.305
--- directory.c	12 Oct 2005 18:25:25 -0000	1.304
+++ directory.c	13 Oct 2005 22:48:09 -0000	1.305
@@ -878,11 +878,11 @@
     }
     /* Try declared compression first if we can. */
     if (compression > 0)
-      tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression);
+      tor_gzip_uncompress(&new_body, &new_len, body, body_len, compression, 1);
     /* Okay, if that didn't work, and we think that it was compressed
      * differently, try that. */
     if (!new_body && guessed > 0 && compression != guessed)
-      tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed);
+      tor_gzip_uncompress(&new_body, &new_len, body, body_len, guessed, 1);
     /* If we're pretty sure that we have a compressed directory, and
      * we didn't manage to uncompress it, then warn and bail. */
     if (!plausible && !new_body) {

Index: test.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/test.c,v
retrieving revision 1.207
retrieving revision 1.208
diff -u -d -r1.207 -r1.208
--- test.c	30 Sep 2005 20:47:58 -0000	1.207
+++ test.c	13 Oct 2005 22:48:09 -0000	1.208
@@ -919,7 +919,7 @@
     test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
     test_eq(detect_compression_method(buf2, len1), GZIP_METHOD);
 
-    test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, GZIP_METHOD));
+    test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, GZIP_METHOD, 1));
     test_assert(buf3);
     test_streq(buf1,buf3);
 
@@ -933,20 +933,40 @@
   test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
   test_eq(detect_compression_method(buf2, len1), ZLIB_METHOD);
 
-  test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, ZLIB_METHOD));
+  test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1, ZLIB_METHOD, 1));
   test_assert(buf3);
   test_streq(buf1,buf3);
 
+  /* Check whether we can uncompress concatenated, compresed strings. */
   tor_free(buf3);
   buf2 = tor_realloc(buf2, len1*2);
   memcpy(buf2+len1, buf2, len1);
-  test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2, ZLIB_METHOD));
+  test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2, ZLIB_METHOD, 1));
   test_eq(len2, (strlen(buf1)+1)*2);
   test_memeq(buf3,
              "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
              "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
              (strlen(buf1)+1)*2);
 
+  tor_free(buf1);
+  tor_free(buf2);
+  tor_free(buf3);
+
+  /* Check whether we can uncompress partial strings. */
+  buf1 = tor_strdup("String with low redundancy that won't be compressed much.");
+  test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,ZLIB_METHOD));
+  tor_assert(len1>16);
+  /* when we allow an uncomplete string, we should succeed.*/
+  tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16, ZLIB_METHOD, 0));
+  buf3[len2]='\0';
+  tor_assert(len2 > 5);
+  tor_assert(!strcmpstart(buf1, buf3));
+
+  /* when we demand a complete string, this must fail. */
+  tor_free(buf3);
+  tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16, ZLIB_METHOD, 1));
+  tor_assert(!buf3);
+
   tor_free(buf2);
   tor_free(buf3);
   tor_free(buf1);



More information about the tor-commits mailing list