mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user