Index: lib/fuzzer/FuzzerIO.cpp =================================================================== --- lib/fuzzer/FuzzerIO.cpp +++ lib/fuzzer/FuzzerIO.cpp @@ -31,7 +31,7 @@ } Unit FileToVector(const std::string &Path, size_t MaxSize, bool ExitOnError) { - std::ifstream T(Path); + std::ifstream T(Path, std::ios::binary); if (ExitOnError && !T) { Printf("No such directory: %s; exiting\n", Path.c_str()); exit(1); @@ -51,7 +51,7 @@ } std::string FileToString(const std::string &Path) { - std::ifstream T(Path); + std::ifstream T(Path, std::ios::binary); return std::string((std::istreambuf_iterator(T)), std::istreambuf_iterator()); } Index: lib/fuzzer/afl/afl_driver.cpp =================================================================== --- lib/fuzzer/afl/afl_driver.cpp +++ lib/fuzzer/afl/afl_driver.cpp @@ -289,7 +289,7 @@ // Execute any files provided as parameters. int ExecuteFilesOnyByOne(int argc, char **argv) { for (int i = 1; i < argc; i++) { - std::ifstream in(argv[i]); + std::ifstream in(argv[i], std::ios::binary); in.seekg(0, in.end); size_t length = in.tellg(); in.seekg (0, in.beg); Index: test/fuzzer/ReadBinaryTest.cpp =================================================================== --- test/fuzzer/ReadBinaryTest.cpp +++ test/fuzzer/ReadBinaryTest.cpp @@ -0,0 +1,18 @@ +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. + +// Simple test for a fuzzer. Tests that fuzzer can read a file containing +// carriage returns. +#include +#include +#include +#include + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) { + std::string InputStr(reinterpret_cast(Data), Size); + std::string MagicStr("Hello\r\nWorld\r\n"); + if (InputStr == MagicStr) { + std::cout << "BINGO!"; + } + return 0; +} Index: test/fuzzer/read-binary.test =================================================================== --- test/fuzzer/read-binary.test +++ test/fuzzer/read-binary.test @@ -0,0 +1,7 @@ +# Test that libFuzzer reads files properly. + +# Account for the fact that echo will add a trailing newline. +RUN: echo -e "Hello\r\nWorld\r" > %t-testcase +RUN: %cpp_compiler %S/ReadBinaryTest.cpp -o %t-fuzzer +RUN: %run %t-fuzzer %t-testcase | FileCheck %s +CHECK: BINGO!