Be a bit more careful with fwrite.
authorCarl Hetherington <cth@carlh.net>
Sun, 23 Dec 2018 21:38:44 +0000 (21:38 +0000)
committerCarl Hetherington <cth@carlh.net>
Sun, 23 Dec 2018 21:38:44 +0000 (21:38 +0000)
src/lib/config.cc
src/lib/internet.cc
src/lib/reel_writer.cc
src/lib/util.cc
src/lib/util.h
src/lib/writer.cc
src/wx/config_dialog.cc
src/wx/swaroop_controls.cc

index 172890dcfbc34610a83a1d5b26ec64cdd0a6108f..eb26713655a575fd1135dee29ed91bc4a60a6a5c 100644 (file)
@@ -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 ();
index ad313bd3f71fb7623dcef484bc8f6d3c70b3c64f..4eba1efa3ada8762fc80a60ec684fa032c23ca6c 100644 (file)
@@ -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;
                }
index f645d6eb5106360de2b84bbcaa76babde5e4f439..e34874a14ca91c7bed111c0c8759fcdb368d21d1 100644 (file)
@@ -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);
 }
 
index 5eba8d73a0f214f07339998c9a6c4543f5f86e2f..595c7e76eb38355bf014c5a05647fea714b8a9a7 100644 (file)
@@ -783,14 +783,31 @@ increment_eyes (Eyes e)
        return EYES_LEFT;
 }
 
+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);
                }
        }
index f62b2c4925e89b2df9152b8afccaf4b0d268165c..94e9e3761ab5c45103d4a8e017e3b557c9531616 100644 (file)
@@ -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
index c31ae2a91661f5e42b5403f28220bc0a1e8c4936..45a74624f7da19564766b26ef2eba18da8f59b84 100644 (file)
@@ -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);
 }
 
index ccf1202ecd25b3bb30f05f6d9e78682590ed8780..8b6d215a2759bc37f3fbc68415ebfe3be01ba822 100644 (file)
@@ -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);
        }
 
index 36aac7cff222e323c6bf1ea433fda2ec1960973c..d6ab5c39daa0de4322b5c6e6b3a0bdc98299eba4 100644 (file)
@@ -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);
        }
 }