Fixed a bugs in the Java runtime that could cause an index out of bounds exception.

Tested: on Windows.

Change-Id: I0d4cdafc21690eb9a509ba31f21e80dacfb602ff
This commit is contained in:
Wouter van Oortmerssen
2014-07-01 17:56:12 -07:00
parent 1256307a38
commit 350011f581
8 changed files with 32 additions and 29 deletions

View File

@@ -160,7 +160,8 @@ static void GenStructArgs(const StructDef &struct_def, std::string *code_ptr,
static void GenStructBody(const StructDef &struct_def, std::string *code_ptr,
const char *nameprefix) {
std::string &code = *code_ptr;
code += " builder.prep(" + NumToString(struct_def.minalign) + ", 0);\n";
code += " builder.prep(" + NumToString(struct_def.minalign) + ", ";
code += NumToString(struct_def.bytesize) + ");\n";
for (auto it = struct_def.fields.vec.rbegin();
it != struct_def.fields.vec.rend();
++it) {
@@ -347,7 +348,8 @@ static void GenStruct(StructDef &struct_def,
// Save out the generated code for a single Java class while adding
// declaration boilerplate.
static bool SaveClass(const Parser &parser, const Definition &def,
const std::string &classcode, const std::string &path) {
const std::string &classcode, const std::string &path,
bool needs_imports) {
if (!classcode.length()) return true;
std::string name_space_java;
@@ -365,8 +367,10 @@ static bool SaveClass(const Parser &parser, const Definition &def,
std::string code = "// automatically generated, do not modify\n\n";
code += "package " + name_space_java + ";\n\n";
code += "import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\n";
code += "import flatbuffers.*;\n\n";
if (needs_imports) {
code += "import java.nio.*;\nimport java.lang.*;\nimport java.util.*;\n";
code += "import flatbuffers.*;\n\n";
}
code += classcode;
auto filename = name_space_dir + PATH_SEPARATOR + def.name + ".java";
return SaveFile(filename.c_str(), code, false);
@@ -383,7 +387,7 @@ bool GenerateJava(const Parser &parser,
it != parser.enums_.vec.end(); ++it) {
std::string enumcode;
GenEnum(**it, &enumcode);
if (!SaveClass(parser, **it, enumcode, path))
if (!SaveClass(parser, **it, enumcode, path, false))
return false;
}
@@ -391,7 +395,7 @@ bool GenerateJava(const Parser &parser,
it != parser.structs_.vec.end(); ++it) {
std::string declcode;
GenStruct(**it, &declcode, parser.root_struct_def);
if (!SaveClass(parser, **it, declcode, path))
if (!SaveClass(parser, **it, declcode, path, true))
return false;
}