Commit ac4deac1873ca1bb570ffd479ed2cc1010762f89

Authored by Jay Berkenbilt
1 parent 7ccc9bd9

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
1 1 2013-02-28 Jay Berkenbilt <ejb@ql.org>
2 2  
  3 + * New method QUtil::safe_fopen to wrap calls to fopen. This is
  4 + less cumbersome than calling QUtil::fopen_wrapper.
  5 +
3 6 * Remove all calls to sprintf
4 7  
5 8 * New method QUtil::int_to_string_base to convert to octal or
... ...
include/qpdf/QUtil.hh
... ... @@ -54,6 +54,11 @@ namespace QUtil
54 54 QPDF_DLL
55 55 int os_wrapper(std::string const& description, int status);
56 56  
  57 + // If the open fails, throws std::runtime_error. Otherwise, the
  58 + // FILE* is returned.
  59 + QPDF_DLL
  60 + FILE* safe_fopen(char const* filename, char const* mode);
  61 +
57 62 // The FILE* argument is assumed to be the return of fopen. If
58 63 // null, throw std::runtime_error. Otherwise, return the FILE*
59 64 // argument.
... ...
libqpdf/FileInputSource.cc
... ... @@ -15,8 +15,7 @@ FileInputSource::setFilename(char const* filename)
15 15 destroy();
16 16 this->filename = filename;
17 17 this->close_file = true;
18   - this->file = QUtil::fopen_wrapper(std::string("open ") + this->filename,
19   - fopen(this->filename.c_str(), "rb")); // XXXX
  18 + this->file = QUtil::safe_fopen(this->filename.c_str(), "rb");
20 19 }
21 20  
22 21 void
... ...
libqpdf/MD5.cc
... ... @@ -328,10 +328,7 @@ void MD5::encodeFile(char const *filename, int up_to_size)
328 328 {
329 329 unsigned char buffer[1024];
330 330  
331   - FILE *file = QUtil::fopen_wrapper(
332   - std::string("MD5: open ") + filename,
333   - fopen(filename, "rb")); // XXXX
334   -
  331 + FILE *file = QUtil::safe_fopen(filename, "rb");
335 332 size_t len;
336 333 int so_far = 0;
337 334 int to_try = 1024;
... ...
libqpdf/QPDFWriter.cc
... ... @@ -104,8 +104,7 @@ QPDFWriter::setOutputFilename(char const* filename)
104 104 else
105 105 {
106 106 QTC::TC("qpdf", "QPDFWriter write to file");
107   - f = QUtil::fopen_wrapper(std::string("open ") + filename,
108   - fopen(filename, "wb+")); // XXXX
  107 + f = QUtil::safe_fopen(filename, "wb+");
109 108 close_file = true;
110 109 }
111 110 setOutputFile(description, f, close_file);
... ...
libqpdf/QTC.cc
... ... @@ -37,9 +37,7 @@ void QTC::TC(char const* const scope, char const* const ccase, int n)
37 37 }
38 38 cache.insert(std::make_pair(ccase, n));
39 39  
40   - FILE* tc =
41   - QUtil::fopen_wrapper("open test coverage file (" + filename + ")",
42   - fopen(filename.c_str(), "ab")); // XXXX
  40 + FILE* tc = QUtil::safe_fopen(filename.c_str(), "ab");
43 41 fprintf(tc, "%s %d\n", ccase, n);
44 42 fclose(tc);
45 43 }
... ...
libqpdf/QUtil.cc
... ... @@ -110,6 +110,13 @@ QUtil::os_wrapper(std::string const&amp; description, int status)
110 110 }
111 111  
112 112 FILE*
  113 +QUtil::safe_fopen(char const* filename, char const* mode)
  114 +{
  115 + return fopen_wrapper(std::string("open ") + filename,
  116 + fopen(filename, mode)); // XXXX
  117 +}
  118 +
  119 +FILE*
113 120 QUtil::fopen_wrapper(std::string const& description, FILE* f)
114 121 {
115 122 if (f == 0)
... ...
libtests/aes.cc
1 1 #include <qpdf/Pl_AES_PDF.hh>
2 2 #include <qpdf/Pl_StdioFile.hh>
  3 +#include <qpdf/QUtil.hh>
3 4  
4 5 #include <stdio.h>
5 6 #include <string.h>
... ... @@ -89,20 +90,8 @@ int main(int argc, char* argv[])
89 90 unsigned int hexkeylen = strlen(hexkey);
90 91 unsigned int keylen = hexkeylen / 2;
91 92  
92   - FILE* infile = fopen(infilename, "rb"); // XXXX
93   - if (infile == 0)
94   - {
95   - std::cerr << "can't open " << infilename << std::endl;
96   - exit(2);
97   - }
98   -
99   - FILE* outfile = fopen(outfilename, "wb"); // XXXX
100   - if (outfile == 0)
101   - {
102   - std::cerr << "can't open " << outfilename << std::endl;
103   - exit(2);
104   - }
105   -
  93 + FILE* infile = QUtil::safe_fopen(infilename, "rb");
  94 + FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
106 95 unsigned char* key = new unsigned char[keylen];
107 96 for (unsigned int i = 0; i < strlen(hexkey); i += 2)
108 97 {
... ...
libtests/flate.cc
... ... @@ -2,33 +2,22 @@
2 2 #include <qpdf/Pl_Flate.hh>
3 3 #include <qpdf/Pl_StdioFile.hh>
4 4 #include <qpdf/Pl_Count.hh>
  5 +#include <qpdf/QUtil.hh>
5 6  
6 7 #include <iostream>
7 8 #include <errno.h>
8 9 #include <string.h>
9 10 #include <stdlib.h>
10 11  
11   -FILE* safe_fopen(char const* filename, char const* mode)
12   -{
13   - FILE* result = fopen(filename, mode); // XXXX
14   - if (result == 0)
15   - {
16   - std::cerr << "fopen " << filename << " failed: " << strerror(errno) // XXXX
17   - << std::endl;
18   - exit(2);
19   - }
20   - return result;
21   -}
22   -
23 12 void run(char const* filename)
24 13 {
25 14 std::string n1 = std::string(filename) + ".1";
26 15 std::string n2 = std::string(filename) + ".2";
27 16 std::string n3 = std::string(filename) + ".3";
28 17  
29   - FILE* o1 = safe_fopen(n1.c_str(), "wb");
30   - FILE* o2 = safe_fopen(n2.c_str(), "wb");
31   - FILE* o3 = safe_fopen(n3.c_str(), "wb");
  18 + FILE* o1 = QUtil::safe_fopen(n1.c_str(), "wb");
  19 + FILE* o2 = QUtil::safe_fopen(n2.c_str(), "wb");
  20 + FILE* o3 = QUtil::safe_fopen(n3.c_str(), "wb");
32 21 Pipeline* out1 = new Pl_StdioFile("o1", o1);
33 22 Pipeline* out2 = new Pl_StdioFile("o2", o2);
34 23 Pipeline* out3 = new Pl_StdioFile("o3", o3);
... ... @@ -46,7 +35,7 @@ void run(char const* filename)
46 35 Pipeline* inf3 = new Pl_Flate("inf3", count3, Pl_Flate::a_inflate);
47 36 Pipeline* def3 = new Pl_Flate("def3", inf3, Pl_Flate::a_deflate);
48 37  
49   - FILE* in1 = safe_fopen(filename, "rb");
  38 + FILE* in1 = QUtil::safe_fopen(filename, "rb");
50 39 unsigned char buf[1024];
51 40 size_t len;
52 41 while ((len = fread(buf, 1, sizeof(buf), in1)) > 0)
... ... @@ -75,7 +64,7 @@ void run(char const* filename)
75 64 fclose(o3);
76 65  
77 66 // Now read the compressed data and write to the output uncompress pipeline
78   - FILE* in2 = safe_fopen(n1.c_str(), "rb");
  67 + FILE* in2 = QUtil::safe_fopen(n1.c_str(), "rb");
79 68 while ((len = fread(buf, 1, sizeof(buf), in2)) > 0)
80 69 {
81 70 inf2->write(buf, len);
... ...
libtests/lzw.cc
... ... @@ -26,10 +26,8 @@ int main(int argc, char* argv[])
26 26 char* infilename = argv[1];
27 27 char* outfilename = argv[2];
28 28  
29   - FILE* infile = QUtil::fopen_wrapper("open input file",
30   - fopen(infilename, "rb")); // XXXX
31   - FILE* outfile = QUtil::fopen_wrapper("open output file",
32   - fopen(outfilename, "wb")); // XXXX
  29 + FILE* infile = QUtil::safe_fopen(infilename, "rb");
  30 + FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
33 31  
34 32 Pl_StdioFile out("output", outfile);
35 33 Pl_LZWDecoder decode("decode", &out, early_code_change);
... ...
libtests/md5.cc
1 1 #include <qpdf/MD5.hh>
2 2 #include <qpdf/Pl_MD5.hh>
3 3 #include <qpdf/Pl_Discard.hh>
  4 +#include <qpdf/QUtil.hh>
4 5 #include <iostream>
5 6 #include <stdio.h>
6 7  
... ... @@ -46,28 +47,25 @@ int main(int, char*[])
46 47 Pl_MD5 p("MD5", &d);
47 48 for (int i = 0; i < 2; ++i)
48 49 {
49   - FILE* f = fopen("md5.in", "rb"); // XXXX
50   - if (f)
51   - {
52   - // buffer size < size of md5.in
53   - unsigned char buf[50];
54   - bool done = false;
55   - while (! done)
56   - {
57   - size_t len = fread(buf, 1, sizeof(buf), f);
58   - if (len <= 0)
59   - {
60   - done = true;
61   - }
62   - else
63   - {
64   - p.write(buf, len);
65   - }
66   - }
67   - fclose(f);
68   - p.finish();
69   - std::cout << p.getHexDigest() << std::endl;
70   - }
  50 + FILE* f = QUtil::safe_fopen("md5.in", "rb");
  51 + // buffer size < size of md5.in
  52 + unsigned char buf[50];
  53 + bool done = false;
  54 + while (! done)
  55 + {
  56 + size_t len = fread(buf, 1, sizeof(buf), f);
  57 + if (len <= 0)
  58 + {
  59 + done = true;
  60 + }
  61 + else
  62 + {
  63 + p.write(buf, len);
  64 + }
  65 + }
  66 + fclose(f);
  67 + p.finish();
  68 + std::cout << p.getHexDigest() << std::endl;
71 69 }
72 70  
73 71 return 0;
... ...
libtests/png_filter.cc
1 1 #include <qpdf/Pl_PNGFilter.hh>
2 2 #include <qpdf/Pl_StdioFile.hh>
  3 +#include <qpdf/QUtil.hh>
3 4  
4 5 #include <iostream>
5 6 #include <errno.h>
... ... @@ -7,23 +8,11 @@
7 8 #include <string.h>
8 9 #include <stdlib.h>
9 10  
10   -FILE* safe_fopen(char const* filename, char const* mode)
11   -{
12   - FILE* result = fopen(filename, mode); // XXXX
13   - if (result == 0)
14   - {
15   - std::cerr << "fopen " << filename << " failed: " << strerror(errno) // XXXX
16   - << std::endl;
17   - exit(2);
18   - }
19   - return result;
20   -}
21   -
22 11 void run(char const* filename, bool encode, unsigned int columns)
23 12 {
24 13 // Decode the file
25   - FILE* in = safe_fopen(filename, "rb");
26   - FILE* o1 = safe_fopen("out", "wb");
  14 + FILE* in = QUtil::safe_fopen(filename, "rb");
  15 + FILE* o1 = QUtil::safe_fopen("out", "wb");
27 16 Pipeline* out = new Pl_StdioFile("out", o1);
28 17 Pipeline* pl = new Pl_PNGFilter(
29 18 "png", out,
... ...
libtests/qtest/qutil/qutil.out
... ... @@ -19,7 +19,7 @@ before remove
19 19 exception: remove file: No such file or directory
20 20 ----
21 21 before fopen
22   -exception: fopen file: No such file or directory
  22 +exception: open /this/file/does/not/exist: No such file or directory
23 23 ----
24 24 IN_TESTSUITE: 1: 1
25 25 HAGOOGAMAGOOGLE: 0
... ...
libtests/qutil.cc
... ... @@ -62,12 +62,10 @@ void os_wrapper_test()
62 62  
63 63 void fopen_wrapper_test()
64 64 {
65   - FILE* f = 0;
66 65 try
67 66 {
68 67 std::cout << "before fopen" << std::endl;
69   - f = QUtil::fopen_wrapper("fopen file",
70   - fopen("/this/file/does/not/exist", "r")); // XXXX
  68 + FILE* f = QUtil::safe_fopen("/this/file/does/not/exist", "r");
71 69 std::cout << "after fopen" << std::endl;
72 70 (void) fclose(f);
73 71 }
... ...
libtests/rc4.cc
1 1 #include <qpdf/Pl_RC4.hh>
2 2 #include <qpdf/Pl_StdioFile.hh>
  3 +#include <qpdf/QUtil.hh>
3 4  
4 5 #include <stdio.h>
5 6 #include <string.h>
... ... @@ -22,13 +23,7 @@ int main(int argc, char* argv[])
22 23 unsigned char* key = new unsigned char[keylen + 1];
23 24 key[keylen] = '\0';
24 25  
25   - FILE* infile = fopen(infilename, "rb"); // XXXX
26   - if (infile == 0)
27   - {
28   - std::cerr << "can't open " << infilename << std::endl;
29   - exit(2);
30   - }
31   -
  26 + FILE* infile = QUtil::safe_fopen(infilename, "rb");
32 27 for (unsigned int i = 0; i < strlen(hexkey); i += 2)
33 28 {
34 29 char t[3];
... ... @@ -40,12 +35,7 @@ int main(int argc, char* argv[])
40 35 key[i/2] = static_cast<unsigned char>(val);
41 36 }
42 37  
43   - FILE* outfile = fopen(outfilename, "wb"); // XXXX
44   - if (outfile == 0)
45   - {
46   - std::cerr << "can't open " << outfilename << std::endl;
47   - exit(2);
48   - }
  38 + FILE* outfile = QUtil::safe_fopen(outfilename, "wb");
49 39 Pl_StdioFile* out = new Pl_StdioFile("stdout", outfile);
50 40 // Use a small buffer size (64) for testing
51 41 Pl_RC4* rc4 = new Pl_RC4("rc4", out, key, keylen, 64);
... ...
qpdf/qpdf-ctest.c
... ... @@ -8,6 +8,18 @@
8 8 static char* whoami = 0;
9 9 static qpdf_data qpdf = 0;
10 10  
  11 +static FILE* safe_fopen(char const* filename, char const* mode)
  12 +{
  13 + FILE* f = fopen(filename, mode); /* XXXX */
  14 + if (f == NULL)
  15 + {
  16 + fprintf(stderr, "%s: unable to open %s: %s\n",
  17 + whoami, filename, strerror(errno)); /* XXXX */
  18 + exit(2);
  19 + }
  20 + return f;
  21 +}
  22 +
11 23 static void report_errors()
12 24 {
13 25 qpdf_error e = 0;
... ... @@ -56,13 +68,7 @@ static void read_file_into_memory(char const* filename,
56 68 size_t bytes_read = 0;
57 69 size_t len = 0;
58 70  
59   - f = fopen(filename, "rb"); /* XXXX */
60   - if (f == NULL)
61   - {
62   - fprintf(stderr, "%s: unable to open %s: %s\n",
63   - whoami, filename, strerror(errno)); /* XXXX */
64   - exit(2);
65   - }
  71 + f = safe_fopen(filename, "rb");
66 72 fseek(f, 0, SEEK_END);
67 73 *size = (unsigned long) ftell(f);
68 74 fseek(f, 0, SEEK_SET);
... ... @@ -364,13 +370,7 @@ static void test16(char const* infile,
364 370 qpdf_set_static_aes_IV(qpdf, QPDF_TRUE);
365 371 qpdf_set_stream_data_mode(qpdf, qpdf_s_uncompress);
366 372 qpdf_write(qpdf);
367   - f = fopen(outfile, "wb"); /* XXXX */
368   - if (f == NULL)
369   - {
370   - fprintf(stderr, "%s: unable to open %s: %s\n",
371   - whoami, outfile, strerror(errno)); /* XXXX */
372   - exit(2);
373   - }
  373 + f = safe_fopen(outfile, "wb");
374 374 buflen = qpdf_get_buffer_length(qpdf);
375 375 buf = qpdf_get_buffer(qpdf);
376 376 fwrite(buf, 1, buflen, f);
... ...
qpdf/test_driver.cc
... ... @@ -165,16 +165,14 @@ void runtest(int n, char const* filename1, char const* arg2)
165 165 else
166 166 {
167 167 QTC::TC("qpdf", "exercise processFile(FILE*)");
168   - filep = QUtil::fopen_wrapper(std::string("open ") + filename1,
169   - fopen(filename1, "rb")); // XXXX
  168 + filep = QUtil::safe_fopen(filename1, "rb");
170 169 pdf.processFile(filename1, filep, false);
171 170 }
172 171 }
173 172 else
174 173 {
175 174 QTC::TC("qpdf", "exercise processMemoryFile");
176   - FILE* f = QUtil::fopen_wrapper(std::string("open ") + filename1,
177   - fopen(filename1, "rb")); // XXXX
  175 + FILE* f = QUtil::safe_fopen(filename1, "rb");
178 176 fseek(f, 0, SEEK_END);
179 177 size_t size = QUtil::tell(f);
180 178 fseek(f, 0, SEEK_SET);
... ... @@ -718,8 +716,7 @@ void runtest(int n, char const* filename1, char const* arg2)
718 716 w.write();
719 717 Buffer* b = w.getBuffer();
720 718 std::string const filename = (i == 0 ? "a.pdf" : "b.pdf");
721   - FILE* f = QUtil::fopen_wrapper("open " + filename,
722   - fopen(filename.c_str(), "wb")); // XXXX
  719 + FILE* f = QUtil::safe_fopen(filename.c_str(), "wb");
723 720 fwrite(b->getBuffer(), b->getSize(), 1, f);
724 721 fclose(f);
725 722 delete b;
... ... @@ -802,8 +799,7 @@ void runtest(int n, char const* filename1, char const* arg2)
802 799 checkPageContents(pages[12], "New page 12");
803 800  
804 801 // Exercise writing to FILE*
805   - FILE* out = QUtil::fopen_wrapper(std::string("open a.pdf"),
806   - fopen("a.pdf", "wb")); // XXXX
  802 + FILE* out = QUtil::safe_fopen("a.pdf", "wb");
807 803 QPDFWriter w(pdf, "FILE* a.pdf", out, true);
808 804 w.setStaticID(true);
809 805 w.setStreamDataMode(qpdf_s_preserve);
... ... @@ -1183,8 +1179,7 @@ void runtest(int n, char const* filename1, char const* arg2)
1183 1179 w.setOutputPipeline(&p);
1184 1180 w.write();
1185 1181 PointerHolder<Buffer> b = p.getBuffer();
1186   - FILE* f = QUtil::fopen_wrapper("open a.pdf",
1187   - fopen("a.pdf", "wb")); // XXXX
  1182 + FILE* f = QUtil::safe_fopen("a.pdf", "wb");
1188 1183 fwrite(b->getBuffer(), b->getSize(), 1, f);
1189 1184 fclose(f);
1190 1185 }
... ...