HomePhabricator

util: fix ReadBinaryFile() and WriteBinaryFile() return status

Description

util: fix ReadBinaryFile() and WriteBinaryFile() return status

Summary:

util: fix ReadBinaryFile() returning partial contents

If an error occurs and fread() returns 0 (nothing was read) then the
code before this patch would have returned "success" with a partially
read contents of the file.

https://github.com/bitcoin/bitcoin/pull/20685/commits/8b6e4b3b23027da263d257b342f5d9a53e4032d5

util: fix WriteBinaryFile() claiming success even if error occurred

fclose() is flushing any buffered data to disk, so if it fails then
that could mean that the data was not completely written to disk.

Thus, check if fclose() succeeds and only then claim success from
WriteBinaryFile().

https://github.com/bitcoin/bitcoin/pull/20685/commits/545bc5f81d60fa6ff7c5cc43a2e9eef82f911466

This is a backport of core#20685 [2&3/20]

Depends on D11012

Test Plan:
ninja all check-all

eviewers:

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D11013

Details

Provenance
Vasil Dimov <vd@FreeBSD.org>Authored on Nov 18 2020, 16:34
PiRKCommitted on Feb 10 2022, 09:12
PiRKPushed on Feb 10 2022, 09:12
Reviewer
Restricted Project
Differential Revision
D11013: util: fix ReadBinaryFile() and WriteBinaryFile() return status
Parents
rABC4557b8ebb275: util: extract {Read,Write}BinaryFile() to its own files
Branches
Unknown
Tags
Unknown