summaryrefslogtreecommitdiff
path: root/src/lib/util.cc
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2018-12-23 21:38:44 +0000
committerCarl Hetherington <cth@carlh.net>2018-12-23 21:38:44 +0000
commit196de029044f4dbac5f74f68e08a89f778c3a236 (patch)
tree92ae37c7b95d8c2839834ab181ad4b0da1f35da8 /src/lib/util.cc
parente7a9a9a0b69d605e327d5a74abe28481d2a61179 (diff)
Be a bit more careful with fwrite.
Diffstat (limited to 'src/lib/util.cc')
-rw-r--r--src/lib/util.cc17
1 files changed, 17 insertions, 0 deletions
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);
}
}