diff options
author | Don Anderson <dda@mongodb.com> | 2017-05-25 14:48:26 -0400 |
---|---|---|
committer | Alex Gorrod <alexander.gorrod@mongodb.com> | 2017-05-25 14:48:26 -0400 |
commit | db14d312f68769f358662f5ea7aa74d61b9cd35d (patch) | |
tree | 88f1497f545045c4529ecf9518dc7c3213a26059 | |
parent | 4641a4586fd18925b3e91881b7c5fd7a203c337b (diff) | |
download | mongo-db14d312f68769f358662f5ea7aa74d61b9cd35d.tar.gz |
WT-3333 Fixes for zero length strings packed/unpacked in a 'u' format via Python. (#3432)
-rw-r--r-- | lang/python/wiredtiger/packing.py | 5 | ||||
-rw-r--r-- | test/java/com/wiredtiger/test/PackTest.java | 41 | ||||
-rw-r--r-- | test/suite/test_pack.py | 5 |
3 files changed, 50 insertions, 1 deletions
diff --git a/lang/python/wiredtiger/packing.py b/lang/python/wiredtiger/packing.py index 0506f2afda9..fb674538b76 100644 --- a/lang/python/wiredtiger/packing.py +++ b/lang/python/wiredtiger/packing.py @@ -94,6 +94,9 @@ def unpack(fmt, s): elif f == 'S': size = s.find('\0') elif f == 'u' and offset == len(fmt) - 1: + # A WT_ITEM with a NULL data field will be appear as None. + if s == None: + s = '' size = len(s) else: # Note: 'U' is used internally, and may be exposed to us. @@ -169,7 +172,7 @@ def pack(fmt, *values): result += val[:l] if f == 'S' and not havesize: result += '\0' - elif size > l: + elif size > l and havesize: result += '\0' * (size - l) elif f in 't': # bit type, size is number of bits diff --git a/test/java/com/wiredtiger/test/PackTest.java b/test/java/com/wiredtiger/test/PackTest.java index a162fdfd0f0..302313169cd 100644 --- a/test/java/com/wiredtiger/test/PackTest.java +++ b/test/java/com/wiredtiger/test/PackTest.java @@ -184,6 +184,47 @@ public class PackTest { } @Test + public void pack08() + throws WiredTigerPackingException { + String format = "u"; + PackOutputStream packer = new PackOutputStream(format); + PackInputStream unpacker; + byte[] b0 = {}; + byte[] b1 = { 0x00 }; + byte[] packed; + + packer.addByteArray(b0); + packed = packer.getValue(); + unpacker = new PackInputStream(format, packed); + Assert.assertTrue(java.util.Arrays.equals( + unpacker.getByteArray(), b0)); + + packer = new PackOutputStream(format); + packer.addByteArray(b1); + packed = packer.getValue(); + unpacker = new PackInputStream(format, packed); + Assert.assertTrue(java.util.Arrays.equals( + unpacker.getByteArray(), b1)); + + format = "uu"; + for (int i = 0; i < 2; i++) { + byte[] arg0 = (i == 0 ? b0 : b1); + for (int j = 0; j < 2; j++) { + byte[] arg1 = (j == 0 ? b0 : b1); + packer = new PackOutputStream(format); + packer.addByteArray(arg0); + packer.addByteArray(arg1); + packed = packer.getValue(); + unpacker = new PackInputStream(format, packed); + Assert.assertTrue(java.util.Arrays.equals( + unpacker.getByteArray(), arg0)); + Assert.assertTrue(java.util.Arrays.equals( + unpacker.getByteArray(), arg1)); + } + } + } + + @Test public void packUnpackNumber01() throws WiredTigerPackingException { // Verify that we can pack and unpack single signed longs. diff --git a/test/suite/test_pack.py b/test/suite/test_pack.py index 951c0b0da20..a24ef4fdfe1 100644 --- a/test/suite/test_pack.py +++ b/test/suite/test_pack.py @@ -95,6 +95,11 @@ class test_pack(wttest.WiredTigerTestCase): self.check('3u', r"\x4") self.check('3uu', r"\x4", r"\x42" * 10) self.check('u3u', r"\x42" * 10, r"\x4") + self.check('u', '\x00') + self.check('u', '') + self.check('uu', '', '\x00') + self.check('uu', '\x00', '') + self.check('uu', '', '') self.check('s', "4") self.check("1s", "4") |