diff options
| author | Carl Hetherington <cth@carlh.net> | 2018-12-23 21:38:44 +0000 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2018-12-23 21:38:44 +0000 |
| commit | 196de029044f4dbac5f74f68e08a89f778c3a236 (patch) | |
| tree | 92ae37c7b95d8c2839834ab181ad4b0da1f35da8 /src | |
| parent | e7a9a9a0b69d605e327d5a74abe28481d2a61179 (diff) | |
Be a bit more careful with fwrite.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/config.cc | 6 | ||||
| -rw-r--r-- | src/lib/internet.cc | 3 | ||||
| -rw-r--r-- | src/lib/reel_writer.cc | 6 | ||||
| -rw-r--r-- | src/lib/util.cc | 17 | ||||
| -rw-r--r-- | src/lib/util.h | 1 | ||||
| -rw-r--r-- | src/lib/writer.cc | 2 | ||||
| -rw-r--r-- | src/wx/config_dialog.cc | 12 | ||||
| -rw-r--r-- | src/wx/swaroop_controls.cc | 2 |
8 files changed, 32 insertions, 17 deletions
diff --git a/src/lib/config.cc b/src/lib/config.cc index 172890dcf..eb2671365 100644 --- a/src/lib/config.cc +++ b/src/lib/config.cc @@ -987,11 +987,7 @@ Config::write_config () const if (!f) { throw FileError (_("Could not open file for writing"), cf); } - size_t const w = fwrite (s.c_str(), 1, s.length(), f); - if (w != s.length()) { - fclose (f); - throw FileError (_("Could not write whole file"), cf); - } + checked_fwrite (s.c_str(), s.length(), f, cf); fclose (f); } catch (xmlpp::exception& e) { string s = e.what (); diff --git a/src/lib/internet.cc b/src/lib/internet.cc index ad313bd3f..4eba1efa3 100644 --- a/src/lib/internet.cc +++ b/src/lib/internet.cc @@ -22,6 +22,7 @@ #include "compose.hpp" #include "exceptions.h" #include "cross.h" +#include "util.h" #include <curl/curl.h> #include <zip.h> #include <boost/function.hpp> @@ -140,7 +141,7 @@ get_from_zip_url (string url, string file, bool pasv, function<void (boost::file char buffer[4096]; while (true) { int const N = zip_fread (file_in_zip, buffer, sizeof (buffer)); - fwrite (buffer, 1, N, f); + checked_fwrite (buffer, N, f, temp_cert.file()); if (N < int (sizeof (buffer))) { break; } diff --git a/src/lib/reel_writer.cc b/src/lib/reel_writer.cc index f645d6eb5..e34874a14 100644 --- a/src/lib/reel_writer.cc +++ b/src/lib/reel_writer.cc @@ -143,9 +143,9 @@ ReelWriter::write_frame_info (Frame frame, Eyes eyes, dcp::FrameInfo info) const throw OpenFileError (info_file, errno, read); } dcpomatic_fseek (file, frame_info_position (frame, eyes), SEEK_SET); - fwrite (&info.offset, sizeof (info.offset), 1, file); - fwrite (&info.size, sizeof (info.size), 1, file); - fwrite (info.hash.c_str(), 1, info.hash.size(), file); + checked_fwrite (&info.offset, sizeof (info.offset), file, info_file); + checked_fwrite (&info.size, sizeof (info.size), file, info_file); + checked_fwrite (info.hash.c_str(), info.hash.size(), file, info_file); fclose (file); } diff --git a/src/lib/util.cc b/src/lib/util.cc index 5eba8d73a..595c7e76e 100644 --- a/src/lib/util.cc +++ b/src/lib/util.cc @@ -784,13 +784,30 @@ increment_eyes (Eyes e) } void +checked_fwrite (void const * ptr, size_t size, FILE* stream, boost::filesystem::path path) +{ + size_t N = fwrite (ptr, 1, size, stream); + if (N != size) { + if (ferror(stream)) { + fclose (stream); + throw FileError (String::compose("fwrite error %1", errno), path); + } else { + fclose (stream); + throw FileError ("Unexpected short write", path); + } + } +} + +void checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path path) { size_t N = fread (ptr, 1, size, stream); if (N != size) { if (ferror(stream)) { + fclose (stream); throw FileError (String::compose("fread error %1", errno), path); } else { + fclose (stream); throw FileError ("Unexpected short read", path); } } diff --git a/src/lib/util.h b/src/lib/util.h index f62b2c492..94e9e3761 100644 --- a/src/lib/util.h +++ b/src/lib/util.h @@ -99,5 +99,6 @@ extern std::pair<int, int> audio_channel_types (std::list<int> mapped, int chann extern boost::shared_ptr<AudioBuffers> remap (boost::shared_ptr<const AudioBuffers> input, int output_channels, AudioMapping map); extern Eyes increment_eyes (Eyes e); extern void checked_fread (void* ptr, size_t size, FILE* stream, boost::filesystem::path path); +extern void checked_fwrite (void const * ptr, size_t size, FILE* stream, boost::filesystem::path path); #endif diff --git a/src/lib/writer.cc b/src/lib/writer.cc index c31ae2a91..45a74624f 100644 --- a/src/lib/writer.cc +++ b/src/lib/writer.cc @@ -634,7 +634,7 @@ Writer::write_cover_sheet () boost::algorithm::replace_all (text, "$LENGTH", length); - fwrite (text.c_str(), 1, text.length(), f); + checked_fwrite (text.c_str(), text.length(), f, cover); fclose (f); } diff --git a/src/wx/config_dialog.cc b/src/wx/config_dialog.cc index ccf1202ec..8b6d215a2 100644 --- a/src/wx/config_dialog.cc +++ b/src/wx/config_dialog.cc @@ -536,7 +536,7 @@ CertificateChainEditor::export_certificate () } string const s = j->certificate (true); - fwrite (s.c_str(), 1, s.length(), f); + checked_fwrite (s.c_str(), s.length(), f, path); fclose (f); } d->Destroy (); @@ -709,7 +709,7 @@ CertificateChainEditor::export_private_key () } string const s = _get()->key().get (); - fwrite (s.c_str(), 1, s.length(), f); + checked_fwrite (s.c_str(), s.length(), f, path); fclose (f); } d->Destroy (); @@ -805,10 +805,10 @@ KeysPage::export_decryption_chain_and_key () } string const chain = Config::instance()->decryption_chain()->chain(); - fwrite (chain.c_str(), 1, chain.length(), f); + checked_fwrite (chain.c_str(), chain.length(), f, path); optional<string> const key = Config::instance()->decryption_chain()->key(); DCPOMATIC_ASSERT (key); - fwrite (key->c_str(), 1, key->length(), f); + checked_fwrite (key->c_str(), key->length(), f, path); fclose (f); } d->Destroy (); @@ -883,7 +883,7 @@ KeysPage::export_decryption_chain () } string const s = Config::instance()->decryption_chain()->chain(); - fwrite (s.c_str(), 1, s.length(), f); + checked_fwrite (s.c_str(), s.length(), f, path); fclose (f); } d->Destroy (); @@ -905,7 +905,7 @@ KeysPage::export_decryption_certificate () } string const s = Config::instance()->decryption_chain()->leaf().certificate (true); - fwrite (s.c_str(), 1, s.length(), f); + checked_fwrite (s.c_str(), s.length(), f, path); fclose (f); } diff --git a/src/wx/swaroop_controls.cc b/src/wx/swaroop_controls.cc index 36aac7cff..d6ab5c39d 100644 --- a/src/wx/swaroop_controls.cc +++ b/src/wx/swaroop_controls.cc @@ -158,7 +158,7 @@ SwaroopControls::viewer_position_changed () + " " + dcp::raw_convert<string>(_selected_playlist_position) + " " + dcp::raw_convert<string>(_viewer->position().get()); - fwrite (p.c_str(), p.length(), 1, f); + checked_fwrite (p.c_str(), p.length(), f, Config::path("position")); fclose (f); } } |
