Skip to content

Commit a97cf27

Browse files
committed
[Bug 14702] LCB: Don't try to get head or tail of empty lists.
1 parent 22ca40a commit a97cf27

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

libfoundation/src/foundation-proper-list.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,19 @@ MCValueRef MCProperListFetchHead(MCProperListRef self)
348348
{
349349
if (MCProperListIsIndirect(self))
350350
self = self -> contents;
351-
351+
352+
MCAssert (self->length > 0);
353+
352354
return self -> list[0];
353355
}
354356

355357
MCValueRef MCProperListFetchTail(MCProperListRef self)
356358
{
357359
if (MCProperListIsIndirect(self))
358360
self = self -> contents;
359-
361+
362+
MCAssert (self->length > 0);
363+
360364
return self -> list[self -> length - 1];
361365
}
362366

libscript/src/module-list.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,25 @@
2020

2121
extern "C" MC_DLLEXPORT void MCListEvalHeadOf(MCProperListRef p_target, MCValueRef& r_output)
2222
{
23+
if (MCProperListIsEmpty (p_target))
24+
{
25+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
26+
r_output = nil;
27+
return;
28+
}
29+
2330
r_output = MCValueRetain(MCProperListFetchHead(p_target));
2431
}
2532

2633
extern "C" MC_DLLEXPORT void MCListEvalTailOf(MCProperListRef p_target, MCValueRef& r_output)
2734
{
35+
if (MCProperListIsEmpty (p_target))
36+
{
37+
MCErrorCreateAndThrow(kMCGenericErrorTypeInfo, "reason", MCSTR("chunk index out of range"), nil);
38+
r_output = nil;
39+
return;
40+
}
41+
2842
r_output = MCValueRetain(MCProperListFetchTail(p_target));
2943
}
3044

tests/lcb/stdlib/list.lcb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public handler TestHead()
5353
test "head" when the head of t is "x"
5454
test "first" when the first element of t is "x"
5555

56-
skip test "head (empty)" because "bug 14702"
57-
--MCUnitTestHandlerThrows(TestHead_Empty, "head (empty)")
56+
MCUnitTestHandlerThrows(TestHead_Empty, "head (empty)")
5857
MCUnitTestHandlerThrows(TestHead_FirstEmpty, "first (empty)")
5958
end handler
6059

@@ -70,8 +69,7 @@ public handler TestTail()
7069
test "tail" when the tail of t
7170
test "last" when the last element of t
7271

73-
skip test "tail (empty)" because "bug 14702"
74-
--MCUnitTestHandlerThrows(TestTail_Empty, "tail (empty)")
72+
MCUnitTestHandlerThrows(TestTail_Empty, "tail (empty)")
7573
MCUnitTestHandlerThrows(TestTail_LastEmpty, "last (empty)")
7674
end handler
7775

0 commit comments

Comments
 (0)