diff options
| author | Literally Void <literallyvoid@gmail.com> | 2020-09-09 20:49:49 -0700 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2020-09-10 12:00:53 +0300 |
| commit | 78baa16da020ec9ad741b8f3f9c6168ec77ef58d (patch) | |
| tree | e04b2a1763c3aa3b62ba0b9ccd2327011e53ea59 /lib/std/priority_queue.zig | |
| parent | 749417a1f3060f0695bbfe72d929f06b0be42535 (diff) | |
| download | zig-78baa16da020ec9ad741b8f3f9c6168ec77ef58d.tar.gz zig-78baa16da020ec9ad741b8f3f9c6168ec77ef58d.zip | |
Fix issue #6303: iterating empty PriorityQueue crashes
Diffstat (limited to 'lib/std/priority_queue.zig')
| -rw-r--r-- | lib/std/priority_queue.zig | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/std/priority_queue.zig b/lib/std/priority_queue.zig index 1c0d230d4d..b9be9b70bf 100644 --- a/lib/std/priority_queue.zig +++ b/lib/std/priority_queue.zig @@ -195,7 +195,7 @@ pub fn PriorityQueue(comptime T: type) type { count: usize, pub fn next(it: *Iterator) ?T { - if (it.count > it.queue.len - 1) return null; + if (it.count >= it.queue.len) return null; const out = it.count; it.count += 1; return it.queue.items[out]; @@ -428,3 +428,12 @@ test "std.PriorityQueue: remove at index" { expectEqual(queue.remove(), 3); expectEqual(queue.removeOrNull(), null); } + +test "std.PriorityQueue: iterator while empty" { + var queue = PQ.init(testing.allocator, lessThan); + defer queue.deinit(); + + var it = queue.iterator(); + + expectEqual(it.next(), null); +} |
