aboutsummaryrefslogtreecommitdiffstats
path: root/fuzz
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2019-06-21 05:35:23 +0200
committerJay Berkenbilt <ejb@ql.org>2019-06-21 19:17:21 +0200
commitd71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e (patch)
tree60f4a13cbadee1a02a49f35f325460f2ad507c95 /fuzz
parentf40ffc9d6392edf9b6fe74d288d6d578e6d1a240 (diff)
downloadqpdf-d71f05ca07eb5c7cfa4d6d23e5c1f2a800f52e8e.tar.zst
Fix sign and conversion warnings (major)
This makes all integer type conversions that have potential data loss explicit with calls that do range checks and raise an exception. After this commit, qpdf builds with no warnings when -Wsign-conversion -Wconversion is used with gcc or clang or when -W3 -Wd4800 is used with MSVC. This significantly reduces the likelihood of potential crashes from bogus integer values. There are some parts of the code that take int when they should take size_t or an offset. Such places would make qpdf not support files with more than 2^31 of something that usually wouldn't be so large. In the event that such a file shows up and is valid, at least qpdf would raise an error in the right spot so the issue could be legitimately addressed rather than failing in some weird way because of a silent overflow condition.
Diffstat (limited to 'fuzz')
-rw-r--r--fuzz/standalone_fuzz_target_runner.cc58
1 files changed, 35 insertions, 23 deletions
diff --git a/fuzz/standalone_fuzz_target_runner.cc b/fuzz/standalone_fuzz_target_runner.cc
index 9881d0fb..59fb0438 100644
--- a/fuzz/standalone_fuzz_target_runner.cc
+++ b/fuzz/standalone_fuzz_target_runner.cc
@@ -1,34 +1,46 @@
-// Copyright 2017 Google Inc. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0 (the "License");
-
-// Except for formatting, comments, and portability, this was copied
-// from projects/example/my-api-repo/standalone_fuzz_target_runner.cpp
-// in https://github.com/oss-fuzz
-
-#include <cassert>
+#include <qpdf/QUtil.hh>
+#include <qpdf/PointerHolder.hh>
+#include <qpdf/QIntC.hh>
#include <iostream>
-#include <fstream>
-#include <vector>
+#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++)
{
- std::ifstream in(argv[i]);
- in.seekg(0, in.end);
- size_t length = in.tellg();
- in.seekg (0, in.beg);
- std::cout << "checking " << argv[i] << std::endl;
- // Allocate exactly length bytes so that we reliably catch
- // buffer overflows.
- std::vector<char> bytes(length);
- in.read(bytes.data(), bytes.size());
- assert(in);
- LLVMFuzzerTestOneInput(
- reinterpret_cast<unsigned char const*>(bytes.data()),
- bytes.size());
+ PointerHolder<unsigned char> file_buf;
+ size_t size = 0;
+ read_file_into_memory(argv[i], file_buf, size);
+ LLVMFuzzerTestOneInput(file_buf.getPointer(), size);
std::cout << argv[i] << " successful" << std::endl;
}
return 0;