From 1f851a7663e1ffd738ccb5357b386e88fdd11828 Mon Sep 17 00:00:00 2001 From: Thomas Bruederli Date: Wed, 16 Oct 2013 12:46:13 +0200 Subject: [PATCH] iCal parsing can consume high memory when file contains lots of events. Abort import if memory is expected to get exhausted (#2353) --- plugins/libcalendaring/libvcalendar.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plugins/libcalendaring/libvcalendar.php b/plugins/libcalendaring/libvcalendar.php index c23d40b1..24781c93 100644 --- a/plugins/libcalendaring/libvcalendar.php +++ b/plugins/libcalendaring/libvcalendar.php @@ -113,6 +113,15 @@ class libvcalendar // TODO: convert charset to UTF-8 if other try { + // estimate the memory usage and try to avoid fatal errors when allowed memory gets exhausted + $count = substr_count($vcal, 'BEGIN:VEVENT'); + $memory_available = parse_bytes(ini_get('memory_limit')) - (function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024); + $expected_memory = $count * 70*1024; // assume ~ 70K per event (empirically determined) + + if ($memory_available > 0 && $expected_memory > $memory_available) { + throw new Exception("iCal file too big"); + } + $vobject = VObject\Reader::read($vcal, VObject\Reader::OPTION_FORGIVING | VObject\Reader::OPTION_IGNORE_INVALID_LINES); if ($vobject) return $this->import_from_vobject($vobject);