Skip to content

Commit

Permalink
Fix an issue where smspsec might dereference null
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Jan 25, 2024
1 parent a949810 commit 131f2b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
32 changes: 13 additions & 19 deletions lib/resdata/rd_smspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,14 +1198,7 @@ static bool rd_smspec_fread_header(rd_smspec_type *rd_smspec,
rd_smspec_type *rd_smspec_fread_alloc(const char *header_file,
const char *key_join_string,
bool include_restart) {
rd_smspec_type *rd_smspec;

{
char *path;
util_alloc_file_components(header_file, &path, NULL, NULL);
rd_smspec = rd_smspec_alloc_empty(false, key_join_string);
free(path);
}
rd_smspec_type *rd_smspec = rd_smspec_alloc_empty(false, key_join_string);

if (rd_smspec_fread_header(rd_smspec, header_file, include_restart)) {

Expand All @@ -1226,20 +1219,21 @@ rd_smspec_type *rd_smspec_fread_alloc(const char *header_file,

const rd::smspec_node *day_node =
rd_smspec_get_var_node(rd_smspec->misc_var_index, "DAY");
if (day_node != NULL) {
rd_smspec->day_index = smspec_node_get_params_index(day_node);
rd_smspec->month_index = smspec_node_get_params_index(
&rd_smspec->misc_var_index["MONTH"]);
rd_smspec->year_index = smspec_node_get_params_index(
&rd_smspec->misc_var_index["YEAR"]);
const rd::smspec_node *month_node =
rd_smspec_get_var_node(rd_smspec->misc_var_index, "MONTH");
const rd::smspec_node *year_node =
rd_smspec_get_var_node(rd_smspec->misc_var_index, "YEAR");

if (day_node != NULL && month_node != NULL && year_node != NULL) {
rd_smspec->day_index = day_node->get_params_index();
rd_smspec->month_index = month_node->get_params_index();
rd_smspec->year_index = year_node->get_params_index();
}

if ((rd_smspec->time_index == -1) && (rd_smspec->day_index == -1)) {
/* Unusable configuration.
Seems the restart file can also have time specified with
'YEARS' as basic time unit; that mode is not supported.
*/
// Unusable configuration.
// Seems the restart file can also have time specified with
// 'YEARS' as basic time unit; that mode is not supported.

util_abort("%s: Sorry the SMSPEC file seems to lack all time "
"information, need either TIME, or DAY/MONTH/YEAR "
Expand Down
2 changes: 2 additions & 0 deletions python/tests/rd_tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def test_identify_var_type(self):
)
self.assertEqual(Summary.varType("RPR"), SummaryVarType.RD_SMSPEC_REGION_VAR)
self.assertEqual(Summary.varType("WNEWTON"), SummaryVarType.RD_SMSPEC_MISC_VAR)
self.assertEqual(Summary.varType("YEAR"), SummaryVarType.RD_SMSPEC_MISC_VAR)
self.assertEqual(Summary.varType("MONTH"), SummaryVarType.RD_SMSPEC_MISC_VAR)
self.assertEqual(
Summary.varType("AARQ:4"), SummaryVarType.RD_SMSPEC_AQUIFER_VAR
)
Expand Down

0 comments on commit 131f2b6

Please sign in to comment.