Skip to content

Conversation

@bytting
Copy link
Contributor

@bytting bytting commented Jul 15, 2017

Fixes a potential file handle leak when size of entropy is invalid

do {
ssize_t n = read(f, ent32 + have, NUM_OS_RANDOM_BYTES - have);
if (n <= 0 || n + have > NUM_OS_RANDOM_BYTES) {
close(f);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it needed at all as RandFailure() aborts?

Copy link
Contributor Author

@bytting bytting Jul 15, 2017

Choose a reason for hiding this comment

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

As I understand it, most OS's will close descriptors on exit, but it is not guaranteed by the C language, so best practice is to close properly.

Also, depending on the behavior of RandFailure, which could change in the future, seems sloppy

Copy link
Contributor

Choose a reason for hiding this comment

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

Heh, if we abort() there's gonna be a lot of things left open, but OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm so used to try to bring everything down brick by brick, I can't remember ever resorting to abort, or even exit 😟

@practicalswift
Copy link
Contributor

utACK a8ae0b2

+1 for explicit closing. Reduces the number of false positives when hunting file descriptor leaks.

Thanks for a nice first-time contribution!

Copy link
Contributor

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

utACK, I guess.

do {
ssize_t n = read(f, ent32 + have, NUM_OS_RANDOM_BYTES - have);
if (n <= 0 || n + have > NUM_OS_RANDOM_BYTES) {
close(f);
Copy link
Contributor

Choose a reason for hiding this comment

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

Heh, if we abort() there's gonna be a lot of things left open, but OK.

@practicalswift
Copy link
Contributor

Follow-up PR: RandFailure() (which is called from GetDevURandom(…)) is marked as [[noreturn]] in #10843. Thanks for bringing this to my attention @corebob!

@sipa
Copy link
Member

sipa commented Jul 16, 2017

utACK a8ae0b2. Better to be explicit.

@promag
Copy link
Contributor

promag commented Jul 16, 2017

utACK a8ae0b2.

@paveljanik
Copy link
Contributor

ACK a8ae0b2

@laanwj laanwj changed the title Fix resource leak Fix resource leak on error in GetDevURandom Jul 17, 2017
@laanwj laanwj merged commit a8ae0b2 into bitcoin:master Jul 17, 2017
laanwj added a commit that referenced this pull request Jul 17, 2017
a8ae0b2 Fix resource leak (Dag Robole)

Pull request description:

  Fixes a potential file handle leak when size of entropy is invalid

Tree-SHA512: 692d24daaf370bba1f842925b037275126f9494f54769650bcf5829c794a0fb8561a86f42347bdf088a484e4f107bce7fa14cd7bdbfb4ecfbeb51968953da3ae
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 24, 2019
a8ae0b2 Fix resource leak (Dag Robole)

Pull request description:

  Fixes a potential file handle leak when size of entropy is invalid

Tree-SHA512: 692d24daaf370bba1f842925b037275126f9494f54769650bcf5829c794a0fb8561a86f42347bdf088a484e4f107bce7fa14cd7bdbfb4ecfbeb51968953da3ae
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants