]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
libext2fs: fix bug in ext2fs_digest_encode()
authorTheodore Ts'o <tytso@mit.edu>
Mon, 6 Apr 2015 00:35:50 +0000 (20:35 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 6 Apr 2015 00:35:50 +0000 (20:35 -0400)
The ext2fs_digest_encode() function was broken for any input which was
a multiple of 3.  Previously we never hit that case, so we never
noticed it was busted.  Also fix up the unit test so future problems
like this get noticed quickly.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/Makefile.in
lib/ext2fs/digest_encode.c

index 367f440f342c3839d569916c9bb656be075d7539..8a7f8ca5290281724ee31586bdc1933d96353633 100644 (file)
@@ -276,7 +276,7 @@ tst_badblocks: tst_badblocks.o $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
 tst_digest_encode: $(srcdir)/digest_encode.c $(srcdir)/ext2_fs.h
        $(E) "  CC $@"
        $(Q) $(CC) $(ALL_LDFLAGS) $(ALL_CFLAGS) -o tst_digest_encode \
-               $(srcdir)/sha256.c -DUNITTEST $(SYSLIBS)
+               $(srcdir)/digest_encode.c -DUNITTEST $(SYSLIBS)
 
 tst_icount: $(srcdir)/icount.c $(STATIC_LIBEXT2FS) $(DEPSTATIC_LIBCOM_ERR)
        $(E) "  LD $@"
@@ -523,7 +523,8 @@ mkjournal: mkjournal.c $(STATIC_LIBEXT2FS) $(DEPLIBCOM_ERR)
 
 check:: tst_bitops tst_badblocks tst_iscan tst_types tst_icount \
     tst_super_size tst_types tst_inode_size tst_csum tst_crc32c tst_bitmaps \
-    tst_inline tst_inline_data tst_libext2fs tst_sha256 tst_sha512
+    tst_inline tst_inline_data tst_libext2fs tst_sha256 tst_sha512 \
+    tst_digest_encode
        $(TESTENV) ./tst_bitops
        $(TESTENV) ./tst_badblocks
        $(TESTENV) ./tst_iscan
@@ -545,6 +546,7 @@ check:: tst_bitops tst_badblocks tst_iscan tst_types tst_icount \
        diff $(srcdir)/tst_bitmaps_exp tst_bitmaps_out
        $(TESTENV) ./tst_bitmaps -l -f $(srcdir)/tst_bitmaps_cmds > tst_bitmaps_out
        diff $(srcdir)/tst_bitmaps_exp tst_bitmaps_out
+       $(TESTENV) ./tst_digest_encode
 
 installdirs::
        $(E) "  MKINSTALLDIRS $(libdir) $(includedir)/ext2fs"
index 85e7128bb4f51b5f1cb331a3d246fa03a7a45ba3..a24ba6ca2f68a7f3ac223439001d6520c6f655b2 100644 (file)
@@ -56,50 +56,90 @@ int ext2fs_digest_encode(const char *src, unsigned long len, char *dst)
                dst[4*i+1] = lookup_table[c1];
                dst[4*i+2] = lookup_table[c2];
                dst[4*i+3] = lookup_table[c3];
+               i++;
        }
-       return 4*(i+1);
+       return (i * 4);
 }
 
 #ifdef UNITTEST
 static const struct {
        unsigned char d[32];
+       unsigned int len;
        const unsigned char *ed;
 } tests[] = {
        { { 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
            0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
            0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
-           0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 },
+           0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }, 32,
        "JdlXcHj+CqHM7tpYz_wUKCIRbrozBojtKwzMBGNu4wfa"
        },
        { { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
            0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
            0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
-           0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad },
+           0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }, 32,
        "6INf+_yapREqbbK3D5QiJa7aHnQLxOhN0cX+Hjpav0ka"
        },
        { { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
            0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
            0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
-           0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 },
+           0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }, 32,
        "K0OAHjTb4GB5aBYKm4dy5mkpKNfz+hYz2ZE7uNX2gema"
        },
+       { { 0x00, }, 1,
+       "aaaa"
+       },
+       { { 0x01, }, 1,
+       "baaa"
+       },
+       { { 0x01, 0x02 }, 2,
+       "biaa"
+       },
+       { { 0x01, 0x02, 0x03 }, 3,
+       "biWa"
+       },
+       { { 0x01, 0x02, 0x03, 0x04 }, 4,
+       "biWaeaaa"
+       },
+       { { 0x01, 0x02, 0x03, 0x04, 0xff }, 5,
+       "biWae8pa"
+       },
+       { { 0x01, 0x02, 0x03, 0x04, 0xff, 0xfe }, 6,
+       "biWae8V+"
+       },
+       { { 0x01, 0x02, 0x03, 0x04, 0xff, 0xfe, 0xfd }, 7,
+       "biWae8V+9daa"
+       },
 };
 
 int main(int argc, char **argv)
 {
-       int i;
+       int i, ret, len;
        int errors = 0;
-       unsigned char tmp[44];
+       unsigned char tmp[1024];
 
        for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
-               ext2fs_digest_encode(tmp, tests[i].d, 32);
-               printf("Test Digest %d: ", i);
-               if (memcmp(tmp, tests[i].ed, 44) != 0) {
-                       printf("FAILED\n");
+               memset(tmp, 0, sizeof(tmp));
+               ret = ext2fs_digest_encode(tests[i].d, tests[i].len, tmp);
+               len = strlen(tmp);
+               printf("Test Digest %d (returned %d): ", i, ret);
+               if (ret != len) {
+                       printf("FAILED returned %d, string length was %d\n",
+                              ret, len);
+                       errors++;
+               } else if (memcmp(tmp, tests[i].ed, ret) != 0) {
+                       printf("FAILED: got %s, expected %s\n", tmp,
+                              tests[i].ed);
                        errors++;
                } else
                        printf("OK\n");
        }
+       for (i = 1; i < argc; i++) {
+               memset(tmp, 0, sizeof(tmp));
+               ret = ext2fs_digest_encode(argv[i], strlen(argv[i]), tmp);
+               len = strlen(tmp);
+               printf("Digest of '%s' is '%s' (returned %d, length %d)\n",
+                      argv[i], tmp, ret, len);
+       }
        return errors;
 }