From 63fcf7436e753417b599802c565e04bf36df50ce Mon Sep 17 00:00:00 2001 From: Stanislav Laznicka Date: Thu, 21 Jan 2016 16:22:45 +0100 Subject: [PATCH] Fixes IndexError in Component.from_ical() --- CHANGES.rst | 3 +++ src/icalendar/cal.py | 5 ++++- src/icalendar/tests/test_fixed_issues.py | 9 +++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index bf6455aa..37073650 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,6 +10,9 @@ New: Fixes: +- Fixed possible IndexError exception during parsing of an ical string. + [stlaz] + - Fixed date-time being recognized as date or time during parsing. Added better error handling to parsing from ical strings. [stlaz] diff --git a/src/icalendar/cal.py b/src/icalendar/cal.py index 501eedae..4c9e9172 100644 --- a/src/icalendar/cal.py +++ b/src/icalendar/cal.py @@ -362,7 +362,10 @@ class Component(CaselessDict): # we are adding properties to the current top of the stack else: factory = types_factory.for_property(name) - component = stack[-1] + component = stack[-1] if stack else None + if not component: + raise ValueError('Property "{prop}" does not have ' + 'a parent component.'.format(prop=name)) datetime_names = ('DTSTART', 'DTEND', 'RECURRENCE-ID', 'DUE', 'FREEBUSY', 'RDATE', 'EXDATE') try: diff --git a/src/icalendar/tests/test_fixed_issues.py b/src/icalendar/tests/test_fixed_issues.py index 97e7f69b..7692cb33 100644 --- a/src/icalendar/tests/test_fixed_issues.py +++ b/src/icalendar/tests/test_fixed_issues.py @@ -360,3 +360,12 @@ END:VCALENDAR""" b'DTEND:20150905T100000Z\r\nUID:123\r\n' b'END:VEVENT\r\nEND:VCALENDAR\r\n' ) + + def test_index_error_issue(self): + """ + Found an issue where from_ical() would raise IndexError for properties + without parent components + """ + + with self.assertRaises(ValueError): + cal = icalendar.Calendar.from_ical('VERSION:2.0')