Skip to content

Commit

Permalink
Fix crash in bbob_biobj_problem_best_parameter_print
Browse files Browse the repository at this point in the history
Previously bbob_biobj_problem_best_parameter_print assumed that the
problem was not logged and could misinterpret the ->data member
otherwise which lead to bad data or crashes.

Partially fixes #2224.
  • Loading branch information
olafmersmann committed Oct 25, 2023
1 parent dc944cd commit f7e106e
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions code-experiments/src/coco_problem.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "coco_utilities.c"


/***********************************************************************************************************/

/**
Expand Down Expand Up @@ -626,18 +627,44 @@ void bbob_problem_best_parameter_print(const coco_problem_t *problem) {
}
}

/* We need a forward declaration of this for `bbob_biobj_problem_best_parameter_print` */
static void logger_biobj_evaluate(coco_problem_t *problem, const double *x, double *y);


void bbob_biobj_problem_best_parameter_print(const coco_problem_t *problem) {
size_t i;
FILE *file;
coco_problem_t *problem1, *problem2;

/* THIS IS AN UGLY HACK!
*
* For a biobjective problem that has been wrapped with a logger, we cannot
* access the ->data member of the original problem. But we need access in
* order to access the underlying single objective functions.
*
* There's no easy way to check if a problem is wrapped by a logger, here we
* rely on checking if the ->evaluate_function is the logger one.
*
*/
if (problem->evaluate_function == &logger_biobj_evaluate) {
coco_warning("I'm sorry, Dave. I’m afraid I can’t do that.");
/* In case this function was called previously, overwrite any results. */
file = fopen("._bbob_biobj_problem_best_parameter.txt", "w");
if (file != NULL) {
fclose(file);
}
return;
}

assert(problem != NULL);
assert(problem->data != NULL);
problem1 = ((coco_problem_stacked_data_t *) problem->data)->problem1;
problem2 = ((coco_problem_stacked_data_t *) problem->data)->problem2;
assert(problem1 != NULL);
assert(problem2 != NULL);
assert(problem1->best_parameter != NULL);

problem2 = ((coco_problem_stacked_data_t *) problem->data)->problem2;
assert(problem2 != NULL);
assert(problem2->best_parameter != NULL);

file = fopen("._bbob_biobj_problem_best_parameter.txt", "w");
if (file != NULL) {
for (i = 0; i < problem->number_of_variables; ++i)
Expand Down

0 comments on commit f7e106e

Please sign in to comment.