This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix leak in do { ... } while 0
[perl5.git] / malloc.c
index 53835e1..72cf2cd 100644 (file)
--- a/malloc.c
+++ b/malloc.c
@@ -1230,6 +1230,18 @@ Perl_malloc(size_t nbytes)
        union overhead *p;
        int bucket;
 
+        /* A structure that has more than PTRDIFF_MAX bytes is unfortunately
+         * legal in C, but in such, if two elements are far enough apart, we
+         * can't legally find out how far apart they are.  Limit the size of a
+         * malloc so that pointer subtraction in the same structure is always
+         * well defined */
+        if (nbytes > PTRDIFF_MAX) {
+            MYMALLOC_WRITE2STDERR("Memory requests are limited to PTRDIFF_MAX"
+                                  " bytes to prevent possible undefined"
+                                  " behavior");
+            return NULL;
+        }
+
 #if defined(DEBUGGING) || defined(RCHECK)
        MEM_SIZE size = nbytes;
 #endif