This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients alexandre.vassalotti, pitrou, serhiy.storchaka, vstinner
Date 2015-09-29.07:25:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1443511518.66.0.757000511678.issue25262@psf.upfronthosting.co.za>
In-reply-to
Content
To access an array item, the type of the index variable must be size_t (or a type with the same size), otherwise the compiler produce less efficient machine code:
http://www.viva64.com/en/a/0050/

=> please keep Py_ssize_t type for i

(I didn't check for the specific case of Py_ssize_t: it's signed. Does GCC emit the most efficient machine code for it?)

diff -r 16c8278c03f6 Modules/_pickle.c
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -4606,10 +4606,17 @@ static Py_ssize_t
 calc_binsize(char *bytes, int nbytes)
 {
     unsigned char *s = (unsigned char *)bytes;
-    Py_ssize_t i;
+    int i;
     size_t x = 0;
 
-    for (i = 0; i < nbytes && (size_t)i < sizeof(size_t); i++) {
+    if (nbytes > (int)sizeof(size_t)) {
+        for (i = (int)sizeof(size_t); i < nbytes; i++) {
+            if (s[i])
+                return -1;
+        }
+        nbytes = (int)sizeof(size_t);
+    }

Please add a comment here to explain that the first loop check for integer overflow, it's not obvious at the first read.

Does the Python implementation of pickle produce BINBYTES8? If not: why not?

Note: the patch is probably based on a private Mercurial revision, so it didn't get the [Review] button.
History
Date User Action Args
2015-09-29 07:25:18vstinnersetrecipients: + vstinner, pitrou, alexandre.vassalotti, serhiy.storchaka
2015-09-29 07:25:18vstinnersetmessageid: <1443511518.66.0.757000511678.issue25262@psf.upfronthosting.co.za>
2015-09-29 07:25:18vstinnerlinkissue25262 messages
2015-09-29 07:25:18vstinnercreate