Skip to content

Commit 71cda7c

Browse files
committed
Rework handling of malloc-may-fail.
This was being handled in `config.set`, which correspondingly used the command line flags to check if the option should be set, skipping the `options` data structure alltogether, causing our `options.set_option` call for the `malloc-may-fail` and the `malloc-fail-null` to have no observable outcome.
1 parent 54bfd4f commit 71cda7c

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/cbmc/cbmc_parse_options.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ void cbmc_parse_optionst::set_default_analysis_flags(optionst &options)
116116
options.set_option("signed-overflow-check", true);
117117
options.set_option("undefined-shift-check", true);
118118

119-
// Default malloc failure profile chosen to be returning null.
119+
// Default malloc failure profile chosen to be returning null. These options
120+
// are not strictly needed, but they are staying here as part of documentation
121+
// of the default option set for the tool.
120122
options.set_option("malloc-may-fail", true);
121123
options.set_option("malloc-fail-null", true);
122124

@@ -337,11 +339,24 @@ void cbmc_parse_optionst::get_command_line_options(optionst &options)
337339
if(!cmdline.isset("no-standard-checks"))
338340
{
339341
cbmc_parse_optionst::set_default_analysis_flags(options);
342+
// The malloc failure mode is by default handled by the `config.set` call
343+
// which only looks at the `cmdline` flags. In the case of default checks,
344+
// these haven't been set - we need to overwrite the config object to manually
345+
// bootstrap the malloc-may-fail behaviour
346+
if(!config.ansi_c.malloc_may_fail && options.is_set("malloc-may-fail"))
347+
{
348+
config.ansi_c.malloc_may_fail = true;
349+
config.ansi_c.malloc_failure_mode =
350+
configt::ansi_ct::malloc_failure_modet::malloc_failure_mode_return_null;
351+
}
340352
PARSE_OPTIONS_GOTO_CHECK_NEGATIVE_DEFAULT_CHECKS(cmdline, options);
341353
}
342354
else if(cmdline.isset("no-standard-checks"))
343355
{
344356
PARSE_OPTIONS_GOTO_CHECK_POSITIVE_DEFAULT_CHECKS(cmdline, options);
357+
// If the user opts for no standard checks, it's safe to assume he also
358+
// wants to control the malloc failure behaviour, in which case we can
359+
// also assume that it's going to be setup in the `config.set` call above.
345360
}
346361

347362
// all (other) checks supported by goto_check

src/goto-analyzer/goto_analyzer_parse_options.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ void goto_analyzer_parse_optionst::set_default_analysis_flags(optionst &options)
6666
options.set_option("signed-overflow-check", true);
6767
options.set_option("undefined-shift-check", true);
6868

69-
// Default malloc failure profile chosen to be returning null.
69+
// Default malloc failure profile chosen to be returning null. These options
70+
// are not strictly *needed*, but they are staying here as part of documentation
71+
// of the default option set for the tool.
7072
options.set_option("malloc-may-fail", true);
7173
options.set_option("malloc-fail-null", true);
7274

@@ -91,11 +93,24 @@ void goto_analyzer_parse_optionst::get_command_line_options(optionst &options)
9193
if(!cmdline.isset("no-standard-checks"))
9294
{
9395
goto_analyzer_parse_optionst::set_default_analysis_flags(options);
96+
// The malloc failure mode is by default handled by the `config.set` call
97+
// which only looks at the `cmdline` flags. In the case of default checks,
98+
// these haven't been set - we need to overwrite the config object to manually
99+
// bootstrap the malloc-may-fail behaviour
100+
if(!config.ansi_c.malloc_may_fail && options.is_set("malloc-may-fail"))
101+
{
102+
config.ansi_c.malloc_may_fail = true;
103+
config.ansi_c.malloc_failure_mode =
104+
configt::ansi_ct::malloc_failure_modet::malloc_failure_mode_return_null;
105+
}
94106
PARSE_OPTIONS_GOTO_CHECK_NEGATIVE_DEFAULT_CHECKS(cmdline, options);
95107
}
96108
else if(cmdline.isset("no-standard-checks"))
97109
{
98110
PARSE_OPTIONS_GOTO_CHECK_POSITIVE_DEFAULT_CHECKS(cmdline, options);
111+
// If the user opts for no standard checks, it's safe to assume he also
112+
// wants to control the malloc failure behaviour, in which case we can
113+
// also assume that it's going to be setup in the `config.set` call above.
99114
}
100115

101116
// all (other) checks supported by goto_check

0 commit comments

Comments
 (0)