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

Ensure that _size is properly set in begin #2706

Merged
merged 3 commits into from
Apr 26, 2019
Merged

Ensure that _size is properly set in begin #2706

merged 3 commits into from
Apr 26, 2019

Conversation

lbernstone
Copy link
Contributor

No description provided.

@@ -131,6 +131,7 @@ bool EEPROMClass::begin(size_t size) {
}

_data = new uint8_t[size];
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is the NULL check? new does not always succeed.

Chuck.

Copy link
Member

Choose a reason for hiding this comment

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

@lbernstone does not like NULL checks :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey, I didn't even write that bit.

Copy link
Contributor

Choose a reason for hiding this comment

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

new does not always succeed.

new doesn't always succeed, but it doesn't return NULL unless it's marked with std::nothrow. It throws an exception on failure.

(Arduino is configured with C++ exception handling enabled, but even if exceptions are disabled the firmware will abort() if new fails without being marked std::nothrow)

gcc will even optimize out a null check on a new result if one is added in the code, as it knows the result can't be NULL. (That's ARM, but Xtensa output will be the same for -Os or higher.)

Copy link
Contributor

Choose a reason for hiding this comment

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

@projectgus So, it is a stackdump. Why is begin() a boolean function if it can never return a fail? If I used this function, I would assume because of the declaration I am responsible for error checking.

Copy link
Contributor

@projectgus projectgus Apr 26, 2019

Choose a reason for hiding this comment

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

I'm not saying you shouldn't check for a failed allocation. I'm saying that a null check on the result of the new operator isn't actually checking anything. There are a number of ways a working out of memory check could be added, the comment hints at some of them.

Copy link
Contributor

Choose a reason for hiding this comment

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

(If it was up to me I'd suggest either adding std::nothrow or changing this to malloc().)

Copy link
Member

Choose a reason for hiding this comment

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

let's go with malloc since that is what is used throughout :)

@me-no-dev me-no-dev merged commit 932666a into espressif:master Apr 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants