Skip to content

Commit 42c20d7

Browse files
author
Wouter van Oortmerssen
committed
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.
1 parent 63b526d commit 42c20d7

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/flatc.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) {
117117
" This may crash flatc given a mismatched schema.\n"
118118
" --proto Input is a .proto, translate to .fbs.\n"
119119
" --schema Serialize schemas instead of JSON (use with -b)\n"
120-
"FILEs may depend on declarations in earlier files.\n"
120+
"FILEs may be schemas, or JSON files (conforming to preceding schema)\n"
121121
"FILEs after the -- must be binary flatbuffer format files.\n"
122122
"Output files are named using the base file name of the input,\n"
123123
"and written to the current directory or the path given by -o.\n"
@@ -251,6 +251,10 @@ int main(int argc, const char *argv[]) {
251251
}
252252
}
253253
} else {
254+
// Check if file contains 0 bytes.
255+
if (contents.length() != strlen(contents.c_str())) {
256+
Error("input file appears to be binary: " + *file_it, true);
257+
}
254258
if (flatbuffers::GetExtension(*file_it) == "fbs") {
255259
// If we're processing multiple schemas, make sure to start each
256260
// one from scratch. If it depends on previous schemas it must do

0 commit comments

Comments
 (0)