summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--malloc.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/malloc.c b/malloc.c
index 53835e1f47..72cf2cd307 100644
--- 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