From c0403e15272150dc65bc7f9b2adddbf79baa1b17 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Thu, 4 Aug 2016 16:18:17 -0700 Subject: [PATCH] path.d: add overflow checks --- std/path.d | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/std/path.d b/std/path.d index d2d19a18f8c..40f618c1f85 100644 --- a/std/path.d +++ b/std/path.d @@ -3913,7 +3913,10 @@ string expandTilde(string inputPath) nothrow assert(last_char > 1); // Reserve C memory for the getpwnam_r() function. - int extra_memory_size = 5 * 1024; + version (unittest) + uint extra_memory_size = 2; + else + uint extra_memory_size = 5 * 1024; char* extra_memory; scope(exit) free(extra_memory); @@ -3937,11 +3940,16 @@ string expandTilde(string inputPath) nothrow break; } - if (errno != ERANGE) + if (errno != ERANGE && + // On FreeBSD and OSX, errno can be left at 0 instead of set to ERANGE + errno != 0) onOutOfMemoryError(); // extra_memory isn't large enough - extra_memory_size *= 2; + import core.checkedint : mulu; + bool overflow; + extra_memory_size = mulu(extra_memory_size, 2, overflow); + if (overflow) assert(0); } return path; }