Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Make (Read/Write)BinaryFile work with char vector, use AutoFile #29229
Make (Read/Write)BinaryFile work with char vector, use AutoFile #29229
Changes from all commits
f9b134e
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
I liked more the previous version which called
fread(3)
here. It was simple stupid. This>>
is now hard to follow, especially given that it depends onT
. Forvector
it ends up callingAutoFile::detail_fread()
. It does not check whetherferror(3)
occurred.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.
It does. If the return value of
detail_fread
is notoutput.size()
,operator>>
will fail.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.
Where? There is no
ferror(3)
call. From the man page: "The function fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred."Yes, but it can be equal to the desired size under two conditions: eof, or error. The previous code distinguished between both.
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.
No, the eof-error would only be raised if read past the desired size, not to it. Unless I am missing something?
I am asking, because if there was a bug, it should be fixed, or at least an issue should be filed.
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.
That "must" in the man page is pretty clear: "callers must use feof(3) and ferror(3) to determine which occurred". A
ferror(3)
check can't hurt and it is better to have an extra check that always returns "no error" than a missing check, failing to detect an IO error. The previous code was doing that - a dumbfread(3)
followed by an unconditionalferror(3)
.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.
Yes, it is called to determine which error occurred, see
bitcoin/src/streams.cpp
Line 27 in e69796c
Again, if there is a bug in the current code in master, it should be fixed.
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.
I would extend the check to:
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.
Done in #29307
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.
Glad something useful came out of this PR :-)
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.
Same as above, the dumb version was easier to follow. The new one does not check whether
fclose(3)
failed, but it should. I think that is a serious deficiency inAutoFile
itself.fwrite(3)
may succeed, but if a subsequentfclose(3)
fails we should consider the data did not make it safely to disk and that the file is corrupted (fclose(3)
writes any buffered data to disk usingfflush(3)
, so a failure atfclose(3)
is as bad as failure atfwrite(3)
).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.
Logged as #29307
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.
@vasild I would also prefer to fix things in AutoFile, but perhaps add a few comments to explain what happens under the hood?
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 could happen:
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.
That's not good. I guess we also don't want to sync to disk, and block for that to complete, for every field that's
>>
'd to a file though.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.
Right. That's why I don't see a good solution. It is a design issue with
AutoFile
to flush/close from the destructor which can't signal the failure to the caller.