Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

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.

ERR_FAIL_INDEX_V_MSG(p_idx < 0 || p_idx >= size(), Variant());
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Expand Down Expand Up @@ -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());
Copy link
Member

@Ivorforce Ivorforce Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not required because operator[] already does the check. You'd do it twice if you have it here, too :)

return operator[](p_idx);
}

Array Array::duplicate(bool p_deep) const {
Expand Down
Loading