From 6aeb9e6e9ca07829b01ff263fe4bb90bfb54c05f Mon Sep 17 00:00:00 2001 From: disarray2077 <86157825+disarray2077@users.noreply.github.com> Date: Fri, 7 Jan 2022 20:13:58 -0300 Subject: [PATCH] Add bounds checking to [Pop]Back/[Pop]Front --- BeefLibs/corlib/src/Collections/List.bf | 38 ++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/BeefLibs/corlib/src/Collections/List.bf b/BeefLibs/corlib/src/Collections/List.bf index 079972f2..c440f74b 100644 --- a/BeefLibs/corlib/src/Collections/List.bf +++ b/BeefLibs/corlib/src/Collections/List.bf @@ -283,18 +283,32 @@ namespace System.Collections public ref T Front { + [Checked] + get + { + Runtime.Assert(mSize != 0); + return ref mItems[0]; + } + + [Unchecked, Inline] get { - Debug.Assert(mSize != 0); return ref mItems[0]; } } public ref T Back { + [Checked] + get + { + Runtime.Assert(mSize != 0); + return ref mItems[mSize - 1]; + } + + [Unchecked, Inline] get { - Debug.Assert(mSize != 0); return ref mItems[mSize - 1]; } } @@ -723,13 +737,29 @@ namespace System.Collections return result; } + [Checked] public T PopBack() { - T backVal = mItems[mSize - 1]; - mSize--; + Runtime.Assert(mSize != 0); + return mItems[--mSize]; + } + + [Unchecked] + public T PopBack() + { + return mItems[--mSize]; + } + + [Checked] + public T PopFront() + { + Runtime.Assert(mSize != 0); + T backVal = mItems[0]; + RemoveAt(0); return backVal; } + [Unchecked] public T PopFront() { T backVal = mItems[0];