aboutsummaryrefslogtreecommitdiffstats
path: root/fuzz/standalone_fuzz_target_runner.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-06-22 16:12:10 +0200
committerJay Berkenbilt <ejb@ql.org>2019-06-22 16:14:25 +0200
commit1bde5c68a302c99c627f86d8c95226a8a7623ac3 (patch)
treea29bbd128894dd564375ecef0917da310ef1ee6d /fuzz/standalone_fuzz_target_runner.cc
parent658b5bb3be49d2666b91d35671de71c1cf0a5853 (diff)
downloadqpdf-1bde5c68a302c99c627f86d8c95226a8a7623ac3.tar.zst
Add QUtil::read_file_into_memory
This code was essentially duplicated between test_driver and standalone_fuzz_target_runner.
Diffstat (limited to 'fuzz/standalone_fuzz_target_runner.cc')
-rw-r--r--fuzz/standalone_fuzz_target_runner.cc36
1 files changed, 4 insertions, 32 deletions
diff --git a/fuzz/standalone_fuzz_target_runner.cc b/fuzz/standalone_fuzz_target_runner.cc
index 59fb0438..4b42fc56 100644
--- a/fuzz/standalone_fuzz_target_runner.cc
+++ b/fuzz/standalone_fuzz_target_runner.cc
@@ -1,46 +1,18 @@
#include <qpdf/QUtil.hh>
-#include <qpdf/PointerHolder.hh>
-#include <qpdf/QIntC.hh>
#include <iostream>
#include <string>
extern "C" int LLVMFuzzerTestOneInput(unsigned char const* data, size_t size);
-static void read_file_into_memory(
- char const* filename,
- PointerHolder<unsigned char>& file_buf, size_t& size)
-{
- FILE* f = QUtil::safe_fopen(filename, "rb");
- fseek(f, 0, SEEK_END);
- size = QIntC::to_size(QUtil::tell(f));
- fseek(f, 0, SEEK_SET);
- file_buf = PointerHolder<unsigned char>(true, new unsigned char[size]);
- unsigned char* buf_p = file_buf.getPointer();
- size_t bytes_read = 0;
- size_t len = 0;
- while ((len = fread(buf_p + bytes_read, 1, size - bytes_read, f)) > 0)
- {
- bytes_read += len;
- }
- if (bytes_read != size)
- {
- throw std::runtime_error(
- std::string("failure reading file ") + filename +
- " into memory: read " +
- QUtil::uint_to_string(bytes_read) + "; wanted " +
- QUtil::uint_to_string(size));
- }
- fclose(f);
-}
-
int main(int argc, char **argv)
{
for (int i = 1; i < argc; i++)
{
- PointerHolder<unsigned char> file_buf;
+ PointerHolder<char> file_buf;
size_t size = 0;
- read_file_into_memory(argv[i], file_buf, size);
- LLVMFuzzerTestOneInput(file_buf.getPointer(), size);
+ QUtil::read_file_into_memory(argv[i], file_buf, size);
+ LLVMFuzzerTestOneInput(
+ reinterpret_cast<unsigned char*>(file_buf.getPointer()), size);
std::cout << argv[i] << " successful" << std::endl;
}
return 0;