Skip to content

Commit 07645e5

Browse files
Ensure JSON file is JSON object just after parsing
There is no reason to delay this check, and we can provide useful warning messages right at the start of the bytecode analysis.
1 parent 06807c4 commit 07645e5

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,21 @@ void java_bytecode_languaget::set_language_options(const optionst &options)
204204
{
205205
const std::string filename = options.get_option("static-values");
206206
jsont tmp_json;
207-
if(!parse_json(filename, *message_handler, tmp_json))
208-
static_values_json = std::move(tmp_json);
207+
if(parse_json(filename, *message_handler, tmp_json))
208+
{
209+
warning() << "Provided JSON file for static-values cannot be parsed; it"
210+
<< " will be ignored." << messaget::eom;
211+
}
212+
else
213+
{
214+
if(!tmp_json.is_object())
215+
{
216+
warning() << "Provided JSON file for static-values is not a JSON "
217+
<< "object; it will be ignored." << messaget::eom;
218+
}
219+
else
220+
static_values_json = std::move(to_json_object(tmp_json));
221+
}
209222
}
210223

211224
if(options.is_set("context-include") || options.is_set("context-exclude"))

jbmc/src/java_bytecode/java_bytecode_language.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class java_bytecode_languaget:public languaget
232232
/// JSON which contains initial values of static fields (right
233233
/// after the static initializer of the class was run). This is read from the
234234
/// --static-values command-line option.
235-
optionalt<jsont> static_values_json;
235+
optionalt<json_objectt> static_values_json;
236236

237237
// list of classes to force load even without reference from the entry point
238238
std::vector<irep_idt> java_load_classes;

jbmc/src/java_bytecode/java_static_initializers.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,20 +769,19 @@ code_ifthenelset get_clinit_wrapper_body(
769769

770770
code_blockt get_user_specified_clinit_body(
771771
const irep_idt &class_id,
772-
const optionalt<jsont> &static_values_json,
772+
const optionalt<json_objectt> &static_values_json,
773773
symbol_table_baset &symbol_table,
774774
message_handlert &message_handler,
775775
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
776776
size_t max_user_array_length,
777777
std::unordered_map<std::string, object_creation_referencet> &references)
778778
{
779779
const irep_idt &real_clinit_name = clinit_function_name(class_id);
780-
if(static_values_json.has_value() && static_values_json->is_object())
780+
if(static_values_json.has_value())
781781
{
782-
const auto &json_object = to_json_object(*static_values_json);
783782
const auto class_entry =
784-
json_object.find(id2string(strip_java_namespace_prefix(class_id)));
785-
if(class_entry != json_object.end())
783+
static_values_json->find(id2string(strip_java_namespace_prefix(class_id)));
784+
if(class_entry != static_values_json->end())
786785
{
787786
const auto &class_json_value = class_entry->second;
788787
if(class_json_value.is_object())

jbmc/src/java_bytecode/java_static_initializers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ code_ifthenelset get_clinit_wrapper_body(
7676
/// \return the body of the user_specified_clinit function as a code block.
7777
code_blockt get_user_specified_clinit_body(
7878
const irep_idt &class_id,
79-
const optionalt<jsont> &static_values_json,
79+
const optionalt<json_objectt> &static_values_json,
8080
symbol_table_baset &symbol_table,
8181
message_handlert &message_handler,
8282
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,

0 commit comments

Comments
 (0)