aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-03 15:36:40 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-03 15:36:40 -0400
commit46396646bf1ec5f19cfbe52c7a6faf6ab61086ef (patch)
tree988918c67278322a2a62bdf92a5ddc916d6cc476
parent167634eaa469b4b363997188435f18fdd70c2261 (diff)
Add proper error check for heap popping
-rw-r--r--builtins/array.h4
1 files changed, 3 insertions, 1 deletions
diff --git a/builtins/array.h b/builtins/array.h
index 9a90ca6b..c05aeae9 100644
--- a/builtins/array.h
+++ b/builtins/array.h
@@ -77,6 +77,8 @@ void Array$heapify(array_t *heap, closure_t comparison, int64_t padded_item_size
void Array$heap_push(array_t *heap, const void *item, closure_t comparison, int64_t padded_item_size);
#define Array$heap_push_value(heap, _value, comparison, padded_item_size) ({ __typeof(_value) value = _value; Array$heap_push(heap, &value, comparison, padded_item_size); })
void Array$heap_pop(array_t *heap, closure_t comparison, int64_t padded_item_size);
-#define Array$heap_pop_value(heap, comparison, padded_item_size, type) ({ array_t *_heap = heap; type value = *(type*)_heap->data; Array$heap_pop(_heap, comparison, padded_item_size); value; })
+#define Array$heap_pop_value(heap, comparison, padded_item_size, type) \
+ ({ array_t *_heap = heap; if (_heap->length == 0) fail("Attempt to pop from an empty array"); \
+ type value = *(type*)_heap->data; Array$heap_pop(_heap, comparison, padded_item_size); value; })
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0