summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-09-15 02:35:15 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-09-15 02:35:15 (GMT)
commit0891ac017dd02f9c592c40940dff6b050523be00 (patch)
treec923b2083e61dfea689d38e7528a7e9936a5d9c1
parent1048aa933f096c33738f5088c7362a00e2c5b85c (diff)
downloadcpython-0891ac017dd02f9c592c40940dff6b050523be00.zip
cpython-0891ac017dd02f9c592c40940dff6b050523be00.tar.gz
cpython-0891ac017dd02f9c592c40940dff6b050523be00.tar.bz2
The 'p' (Pascal string) pack code acts unreasonably when the string size
and count exceed 255. Changed to preserve as much of the string as possible (instead of count%256 characters).
-rw-r--r--Lib/test/test_struct.py25
-rw-r--r--Modules/structmodule.c2
2 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index 0f3ac91..34abad5 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -368,3 +368,28 @@ for args in [("bB", 1),
("qQ", 8)]:
t = IntTester(*args)
t.run()
+
+
+###########################################################################
+# The p ("Pascal string") code.
+
+def test_p_code():
+ for code, input, expected, expectedback in [
+ ('p','abc', '\x00', ''),
+ ('1p', 'abc', '\x00', ''),
+ ('2p', 'abc', '\x01a', 'a'),
+ ('3p', 'abc', '\x02ab', 'ab'),
+ ('4p', 'abc', '\x03abc', 'abc'),
+ ('5p', 'abc', '\x03abc\x00', 'abc'),
+ ('6p', 'abc', '\x03abc\x00\x00', 'abc'),
+ ('1000p', 'x'*1000, '\xff' + 'x'*999, 'x'*255)]:
+ got = struct.pack(code, input)
+ if got != expected:
+ raise TestFailed("pack(%r, %r) == %r but expected %r" %
+ (code, input, got, expected))
+ (got,) = struct.unpack(code, got)
+ if got != expectedback:
+ raise TestFailed("unpack(%r, %r) == %r but expected %r" %
+ (code, input, got, expectedback))
+
+test_p_code()
diff --git a/Modules/structmodule.c b/Modules/structmodule.c
index 46aa75b..61436f9 100644
--- a/Modules/structmodule.c
+++ b/Modules/structmodule.c
@@ -1360,6 +1360,8 @@ struct_pack(PyObject *self, PyObject *args)
if (n < num)
/* no real need, just to be nice */
memset(res+1+n, '\0', num-n);
+ if (n > 255)
+ n = 255;
*res++ = n; /* store the length byte */
res += num;
break;