From ac4deac1873ca1bb570ffd479ed2cc1010762f89 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Thu, 28 Feb 2013 16:45:11 -0500 Subject: [PATCH] Call QUtil::safe_fopen in place of fopen fopen was previuosly called wrapped by QUtil::fopen_wrapper, but QUtil::safe_fopen does this itself, which is less cumbersome. --- ChangeLog | 3 +++ include/qpdf/QUtil.hh | 5 ++++ libqpdf/FileInputSource.cc | 3 +-- libqpdf/MD5.cc | 5 +--- libqpdf/QPDFWriter.cc | 3 +-- libqpdf/QTC.cc | 4 +--- libqpdf/QUtil.cc | 7 ++++++ libtests/aes.cc | 17 +++----------- libtests/flate.cc | 23 +++++-------------- libtests/lzw.cc | 6 ++--- libtests/md5.cc | 42 ++++++++++++++++------------------ libtests/png_filter.cc | 17 +++----------- libtests/qtest/qutil/qutil.out | 2 +- libtests/qutil.cc | 4 +--- libtests/rc4.cc | 16 +++---------- qpdf/qpdf-ctest.c | 28 +++++++++++------------ qpdf/test_driver.cc | 15 ++++-------- 17 files changed, 77 insertions(+), 123 deletions(-) diff --git a/ChangeLog b/ChangeLog index b4a2ffae..d8f4da71 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2013-02-28 Jay Berkenbilt + * New method QUtil::safe_fopen to wrap calls to fopen. This is + less cumbersome than calling QUtil::fopen_wrapper. + * Remove all calls to sprintf * New method QUtil::int_to_string_base to convert to octal or diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh index 02d98876..8bad535d 100644 --- a/include/qpdf/QUtil.hh +++ b/include/qpdf/QUtil.hh @@ -54,6 +54,11 @@ namespace QUtil QPDF_DLL int os_wrapper(std::string const& description, int status); + // If the open fails, throws std::runtime_error. Otherwise, the + // FILE* is returned. + QPDF_DLL + FILE* safe_fopen(char const* filename, char const* mode); + // The FILE* argument is assumed to be the return of fopen. If // null, throw std::runtime_error. Otherwise, return the FILE* // argument. diff --git a/libqpdf/FileInputSource.cc b/libqpdf/FileInputSource.cc index 0cd0ef96..ef259c4c 100644 --- a/libqpdf/FileInputSource.cc +++ b/libqpdf/FileInputSource.cc @@ -15,8 +15,7 @@ FileInputSource::setFilename(char const* filename) destroy(); this->filename = filename; this->close_file = true; - this->file = QUtil::fopen_wrapper(std::string("open ") + this->filename, - fopen(this->filename.c_str(), "rb")); // XXXX + this->file = QUtil::safe_fopen(this->filename.c_str(), "rb"); } void diff --git a/libqpdf/MD5.cc b/libqpdf/MD5.cc index ff812f35..0504e2d4 100644 --- a/libqpdf/MD5.cc +++ b/libqpdf/MD5.cc @@ -328,10 +328,7 @@ void MD5::encodeFile(char const *filename, int up_to_size) { unsigned char buffer[1024]; - FILE *file = QUtil::fopen_wrapper( - std::string("MD5: open ") + filename, - fopen(filename, "rb")); // XXXX - + FILE *file = QUtil::safe_fopen(filename, "rb"); size_t len; int so_far = 0; int to_try = 1024; diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 96f50c13..82976452 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -104,8 +104,7 @@ QPDFWriter::setOutputFilename(char const* filename) else { QTC::TC("qpdf", "QPDFWriter write to file"); - f = QUtil::fopen_wrapper(std::string("open ") + filename, - fopen(filename, "wb+")); // XXXX + f = QUtil::safe_fopen(filename, "wb+"); close_file = true; } setOutputFile(description, f, close_file); diff --git a/libqpdf/QTC.cc b/libqpdf/QTC.cc index c1d863e6..cbaf558b 100644 --- a/libqpdf/QTC.cc +++ b/libqpdf/QTC.cc @@ -37,9 +37,7 @@ void QTC::TC(char const* const scope, char const* const ccase, int n) } cache.insert(std::make_pair(ccase, n)); - FILE* tc = - QUtil::fopen_wrapper("open test coverage file (" + filename + ")", - fopen(filename.c_str(), "ab")); // XXXX + FILE* tc = QUtil::safe_fopen(filename.c_str(), "ab"); fprintf(tc, "%s %d\n", ccase, n); fclose(tc); } diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index f186389d..b7cdb9b2 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -109,6 +109,13 @@ QUtil::os_wrapper(std::string const& description, int status) return status; } +FILE* +QUtil::safe_fopen(char const* filename, char const* mode) +{ + return fopen_wrapper(std::string("open ") + filename, + fopen(filename, mode)); // XXXX +} + FILE* QUtil::fopen_wrapper(std::string const& description, FILE* f) { diff --git a/libtests/aes.cc b/libtests/aes.cc index 4b11b5f9..b5a63d9a 100644 --- a/libtests/aes.cc +++ b/libtests/aes.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -89,20 +90,8 @@ int main(int argc, char* argv[]) unsigned int hexkeylen = strlen(hexkey); unsigned int keylen = hexkeylen / 2; - FILE* infile = fopen(infilename, "rb"); // XXXX - if (infile == 0) - { - std::cerr << "can't open " << infilename << std::endl; - exit(2); - } - - FILE* outfile = fopen(outfilename, "wb"); // XXXX - if (outfile == 0) - { - std::cerr << "can't open " << outfilename << std::endl; - exit(2); - } - + FILE* infile = QUtil::safe_fopen(infilename, "rb"); + FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); unsigned char* key = new unsigned char[keylen]; for (unsigned int i = 0; i < strlen(hexkey); i += 2) { diff --git a/libtests/flate.cc b/libtests/flate.cc index d5f77162..a6d4eeb6 100644 --- a/libtests/flate.cc +++ b/libtests/flate.cc @@ -2,33 +2,22 @@ #include #include #include +#include #include #include #include #include -FILE* safe_fopen(char const* filename, char const* mode) -{ - FILE* result = fopen(filename, mode); // XXXX - if (result == 0) - { - std::cerr << "fopen " << filename << " failed: " << strerror(errno) // XXXX - << std::endl; - exit(2); - } - return result; -} - void run(char const* filename) { std::string n1 = std::string(filename) + ".1"; std::string n2 = std::string(filename) + ".2"; std::string n3 = std::string(filename) + ".3"; - FILE* o1 = safe_fopen(n1.c_str(), "wb"); - FILE* o2 = safe_fopen(n2.c_str(), "wb"); - FILE* o3 = safe_fopen(n3.c_str(), "wb"); + FILE* o1 = QUtil::safe_fopen(n1.c_str(), "wb"); + FILE* o2 = QUtil::safe_fopen(n2.c_str(), "wb"); + FILE* o3 = QUtil::safe_fopen(n3.c_str(), "wb"); Pipeline* out1 = new Pl_StdioFile("o1", o1); Pipeline* out2 = new Pl_StdioFile("o2", o2); Pipeline* out3 = new Pl_StdioFile("o3", o3); @@ -46,7 +35,7 @@ void run(char const* filename) Pipeline* inf3 = new Pl_Flate("inf3", count3, Pl_Flate::a_inflate); Pipeline* def3 = new Pl_Flate("def3", inf3, Pl_Flate::a_deflate); - FILE* in1 = safe_fopen(filename, "rb"); + FILE* in1 = QUtil::safe_fopen(filename, "rb"); unsigned char buf[1024]; size_t len; while ((len = fread(buf, 1, sizeof(buf), in1)) > 0) @@ -75,7 +64,7 @@ void run(char const* filename) fclose(o3); // Now read the compressed data and write to the output uncompress pipeline - FILE* in2 = safe_fopen(n1.c_str(), "rb"); + FILE* in2 = QUtil::safe_fopen(n1.c_str(), "rb"); while ((len = fread(buf, 1, sizeof(buf), in2)) > 0) { inf2->write(buf, len); diff --git a/libtests/lzw.cc b/libtests/lzw.cc index 1007ae24..003c9217 100644 --- a/libtests/lzw.cc +++ b/libtests/lzw.cc @@ -26,10 +26,8 @@ int main(int argc, char* argv[]) char* infilename = argv[1]; char* outfilename = argv[2]; - FILE* infile = QUtil::fopen_wrapper("open input file", - fopen(infilename, "rb")); // XXXX - FILE* outfile = QUtil::fopen_wrapper("open output file", - fopen(outfilename, "wb")); // XXXX + FILE* infile = QUtil::safe_fopen(infilename, "rb"); + FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); Pl_StdioFile out("output", outfile); Pl_LZWDecoder decode("decode", &out, early_code_change); diff --git a/libtests/md5.cc b/libtests/md5.cc index 71d83f31..9c9c8cf1 100644 --- a/libtests/md5.cc +++ b/libtests/md5.cc @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -46,28 +47,25 @@ int main(int, char*[]) Pl_MD5 p("MD5", &d); for (int i = 0; i < 2; ++i) { - FILE* f = fopen("md5.in", "rb"); // XXXX - if (f) - { - // buffer size < size of md5.in - unsigned char buf[50]; - bool done = false; - while (! done) - { - size_t len = fread(buf, 1, sizeof(buf), f); - if (len <= 0) - { - done = true; - } - else - { - p.write(buf, len); - } - } - fclose(f); - p.finish(); - std::cout << p.getHexDigest() << std::endl; - } + FILE* f = QUtil::safe_fopen("md5.in", "rb"); + // buffer size < size of md5.in + unsigned char buf[50]; + bool done = false; + while (! done) + { + size_t len = fread(buf, 1, sizeof(buf), f); + if (len <= 0) + { + done = true; + } + else + { + p.write(buf, len); + } + } + fclose(f); + p.finish(); + std::cout << p.getHexDigest() << std::endl; } return 0; diff --git a/libtests/png_filter.cc b/libtests/png_filter.cc index 68f50aee..c3d4f646 100644 --- a/libtests/png_filter.cc +++ b/libtests/png_filter.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -7,23 +8,11 @@ #include #include -FILE* safe_fopen(char const* filename, char const* mode) -{ - FILE* result = fopen(filename, mode); // XXXX - if (result == 0) - { - std::cerr << "fopen " << filename << " failed: " << strerror(errno) // XXXX - << std::endl; - exit(2); - } - return result; -} - void run(char const* filename, bool encode, unsigned int columns) { // Decode the file - FILE* in = safe_fopen(filename, "rb"); - FILE* o1 = safe_fopen("out", "wb"); + FILE* in = QUtil::safe_fopen(filename, "rb"); + FILE* o1 = QUtil::safe_fopen("out", "wb"); Pipeline* out = new Pl_StdioFile("out", o1); Pipeline* pl = new Pl_PNGFilter( "png", out, diff --git a/libtests/qtest/qutil/qutil.out b/libtests/qtest/qutil/qutil.out index ebbf97b5..0e996dec 100644 --- a/libtests/qtest/qutil/qutil.out +++ b/libtests/qtest/qutil/qutil.out @@ -19,7 +19,7 @@ before remove exception: remove file: No such file or directory ---- before fopen -exception: fopen file: No such file or directory +exception: open /this/file/does/not/exist: No such file or directory ---- IN_TESTSUITE: 1: 1 HAGOOGAMAGOOGLE: 0 diff --git a/libtests/qutil.cc b/libtests/qutil.cc index dedd73d1..5e4dd196 100644 --- a/libtests/qutil.cc +++ b/libtests/qutil.cc @@ -62,12 +62,10 @@ void os_wrapper_test() void fopen_wrapper_test() { - FILE* f = 0; try { std::cout << "before fopen" << std::endl; - f = QUtil::fopen_wrapper("fopen file", - fopen("/this/file/does/not/exist", "r")); // XXXX + FILE* f = QUtil::safe_fopen("/this/file/does/not/exist", "r"); std::cout << "after fopen" << std::endl; (void) fclose(f); } diff --git a/libtests/rc4.cc b/libtests/rc4.cc index e71e0d01..7a4d8bd9 100644 --- a/libtests/rc4.cc +++ b/libtests/rc4.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -22,13 +23,7 @@ int main(int argc, char* argv[]) unsigned char* key = new unsigned char[keylen + 1]; key[keylen] = '\0'; - FILE* infile = fopen(infilename, "rb"); // XXXX - if (infile == 0) - { - std::cerr << "can't open " << infilename << std::endl; - exit(2); - } - + FILE* infile = QUtil::safe_fopen(infilename, "rb"); for (unsigned int i = 0; i < strlen(hexkey); i += 2) { char t[3]; @@ -40,12 +35,7 @@ int main(int argc, char* argv[]) key[i/2] = static_cast(val); } - FILE* outfile = fopen(outfilename, "wb"); // XXXX - if (outfile == 0) - { - std::cerr << "can't open " << outfilename << std::endl; - exit(2); - } + FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile); // Use a small buffer size (64) for testing Pl_RC4* rc4 = new Pl_RC4("rc4", out, key, keylen, 64); diff --git a/qpdf/qpdf-ctest.c b/qpdf/qpdf-ctest.c index faa202da..12f6a4cc 100644 --- a/qpdf/qpdf-ctest.c +++ b/qpdf/qpdf-ctest.c @@ -8,6 +8,18 @@ static char* whoami = 0; static qpdf_data qpdf = 0; +static FILE* safe_fopen(char const* filename, char const* mode) +{ + FILE* f = fopen(filename, mode); /* XXXX */ + if (f == NULL) + { + fprintf(stderr, "%s: unable to open %s: %s\n", + whoami, filename, strerror(errno)); /* XXXX */ + exit(2); + } + return f; +} + static void report_errors() { qpdf_error e = 0; @@ -56,13 +68,7 @@ static void read_file_into_memory(char const* filename, size_t bytes_read = 0; size_t len = 0; - f = fopen(filename, "rb"); /* XXXX */ - if (f == NULL) - { - fprintf(stderr, "%s: unable to open %s: %s\n", - whoami, filename, strerror(errno)); /* XXXX */ - exit(2); - } + f = safe_fopen(filename, "rb"); fseek(f, 0, SEEK_END); *size = (unsigned long) ftell(f); fseek(f, 0, SEEK_SET); @@ -364,13 +370,7 @@ static void test16(char const* infile, qpdf_set_static_aes_IV(qpdf, QPDF_TRUE); qpdf_set_stream_data_mode(qpdf, qpdf_s_uncompress); qpdf_write(qpdf); - f = fopen(outfile, "wb"); /* XXXX */ - if (f == NULL) - { - fprintf(stderr, "%s: unable to open %s: %s\n", - whoami, outfile, strerror(errno)); /* XXXX */ - exit(2); - } + f = safe_fopen(outfile, "wb"); buflen = qpdf_get_buffer_length(qpdf); buf = qpdf_get_buffer(qpdf); fwrite(buf, 1, buflen, f); diff --git a/qpdf/test_driver.cc b/qpdf/test_driver.cc index 41125be3..75f5b973 100644 --- a/qpdf/test_driver.cc +++ b/qpdf/test_driver.cc @@ -165,16 +165,14 @@ void runtest(int n, char const* filename1, char const* arg2) else { QTC::TC("qpdf", "exercise processFile(FILE*)"); - filep = QUtil::fopen_wrapper(std::string("open ") + filename1, - fopen(filename1, "rb")); // XXXX + filep = QUtil::safe_fopen(filename1, "rb"); pdf.processFile(filename1, filep, false); } } else { QTC::TC("qpdf", "exercise processMemoryFile"); - FILE* f = QUtil::fopen_wrapper(std::string("open ") + filename1, - fopen(filename1, "rb")); // XXXX + FILE* f = QUtil::safe_fopen(filename1, "rb"); fseek(f, 0, SEEK_END); size_t size = QUtil::tell(f); fseek(f, 0, SEEK_SET); @@ -718,8 +716,7 @@ void runtest(int n, char const* filename1, char const* arg2) w.write(); Buffer* b = w.getBuffer(); std::string const filename = (i == 0 ? "a.pdf" : "b.pdf"); - FILE* f = QUtil::fopen_wrapper("open " + filename, - fopen(filename.c_str(), "wb")); // XXXX + FILE* f = QUtil::safe_fopen(filename.c_str(), "wb"); fwrite(b->getBuffer(), b->getSize(), 1, f); fclose(f); delete b; @@ -802,8 +799,7 @@ void runtest(int n, char const* filename1, char const* arg2) checkPageContents(pages[12], "New page 12"); // Exercise writing to FILE* - FILE* out = QUtil::fopen_wrapper(std::string("open a.pdf"), - fopen("a.pdf", "wb")); // XXXX + FILE* out = QUtil::safe_fopen("a.pdf", "wb"); QPDFWriter w(pdf, "FILE* a.pdf", out, true); w.setStaticID(true); w.setStreamDataMode(qpdf_s_preserve); @@ -1183,8 +1179,7 @@ void runtest(int n, char const* filename1, char const* arg2) w.setOutputPipeline(&p); w.write(); PointerHolder b = p.getBuffer(); - FILE* f = QUtil::fopen_wrapper("open a.pdf", - fopen("a.pdf", "wb")); // XXXX + FILE* f = QUtil::safe_fopen("a.pdf", "wb"); fwrite(b->getBuffer(), b->getSize(), 1, f); fclose(f); }