From 42c20d7a6940b951f2523aa957000a79697bea59 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Fri, 8 Jan 2016 13:10:25 -0800 Subject: [PATCH] Make flatc check for binary files to avoid accidental parsing. Binary file arguments to flatc have to be preceded by -- to identify them, forgetting this however results in them being attempted to be parsed as schema/json, with cryptic errors. This instead gives an error if 0 bytes are contained in your text file. Bug: 22069056 Change-Id: I226bf651dcb016f18d7c8ffadcf23466a1fc0b87 Tested: on Linux. --- src/flatc.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index 97ee79386..dae107e49 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -117,7 +117,7 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { " This may crash flatc given a mismatched schema.\n" " --proto Input is a .proto, translate to .fbs.\n" " --schema Serialize schemas instead of JSON (use with -b)\n" - "FILEs may depend on declarations in earlier files.\n" + "FILEs may be schemas, or JSON files (conforming to preceding schema)\n" "FILEs after the -- must be binary flatbuffer format files.\n" "Output files are named using the base file name of the input,\n" "and written to the current directory or the path given by -o.\n" @@ -251,6 +251,10 @@ int main(int argc, const char *argv[]) { } } } else { + // Check if file contains 0 bytes. + if (contents.length() != strlen(contents.c_str())) { + Error("input file appears to be binary: " + *file_it, true); + } if (flatbuffers::GetExtension(*file_it) == "fbs") { // If we're processing multiple schemas, make sure to start each // one from scratch. If it depends on previous schemas it must do