-
-
Notifications
You must be signed in to change notification settings - Fork 22k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add error handling for array get method #103329
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,20 +95,22 @@ Array::ConstIterator Array::end() const { | |
return ConstIterator(_p->array.ptr() + _p->array.size(), _p->read_only); | ||
} | ||
|
||
Variant &Array::operator[](int p_idx) { | ||
if (unlikely(_p->read_only)) { | ||
*_p->read_only = _p->array[p_idx]; | ||
return *_p->read_only; | ||
} | ||
return _p->array.write[p_idx]; | ||
Variant Array::operator[](int p_idx) { | ||
ERR_FAIL_INDEX_V_MSG(p_idx < 0 || p_idx >= size(), Variant()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make sure you set your code editor to use tabs for indentation. I highly recommend using an IDE to edit the code, because an IDE will adjust the setting automatically for you, depending on existing indentation in the respective file. |
||
if (unlikely(_p->read_only)) { | ||
*_p->read_only = _p->array[p_idx]; | ||
return *_p->read_only; | ||
} | ||
return _p->array[p_idx]; | ||
} | ||
|
||
const Variant &Array::operator[](int p_idx) const { | ||
if (unlikely(_p->read_only)) { | ||
*_p->read_only = _p->array[p_idx]; | ||
return *_p->read_only; | ||
} | ||
return _p->array[p_idx]; | ||
const Variant Array::operator[](int p_idx) const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
ERR_FAIL_INDEX_V_MSG(p_idx < 0 || p_idx >= size(), Variant()); | ||
if (unlikely(_p->read_only)) { | ||
*_p->read_only = _p->array[p_idx]; | ||
return *_p->read_only; | ||
} | ||
return _p->array[p_idx]; | ||
} | ||
|
||
int Array::size() const { | ||
|
@@ -492,8 +494,9 @@ void Array::set(int p_idx, const Variant &p_value) { | |
operator[](p_idx) = value; | ||
} | ||
|
||
const Variant &Array::get(int p_idx) const { | ||
return operator[](p_idx); | ||
Variant Array::get(int p_idx) const { | ||
ERR_FAIL_INDEX_V_MSG(p_idx < 0 || p_idx >= size(), Variant()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is not required because |
||
return operator[](p_idx); | ||
} | ||
|
||
Array Array::duplicate(bool p_deep) const { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong, operator return writable reference, not a copy.