Skip to content

Commit

Permalink
Merge pull request #429 from DrylandEcology/fix_closeOpenFilesOnFailNC
Browse files Browse the repository at this point in the history
- In SOILWAT2 v8.0.0, there is a bug where the two functions - `SW_NC_write_output()` & `SW_NC_read_out_vars()` - upon error, the open file does not get closed

- The mentioned two functions now close files upon failure before leaving the function
- See #424 and #426
  • Loading branch information
N1ckP3rsl3y authored Aug 8, 2024
2 parents 1605a74 + f657982 commit 54abffe
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/SW_netCDF.c
Original file line number Diff line number Diff line change
Expand Up @@ -3747,7 +3747,7 @@ void SW_NC_write_output(
// Get size of the "time" dimension
get_dimlen_from_dimname(currFileID, "time", &timeSize, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile; // Exit function prematurely due to error
}


Expand All @@ -3761,7 +3761,8 @@ void SW_NC_write_output(
// Locate correct slice in netCDF to write to
get_var_identifier(currFileID, varName, &varID, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
/* Exit function prematurely due to error */
goto closeFile;
}

get_vardim_write_counts(
Expand All @@ -3778,7 +3779,8 @@ void SW_NC_write_output(
fileName, varName, currFileID, varID, count, LogInfo
);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
/* Exit function prematurely due to error*/
goto closeFile;
}
#endif // SWDEBUG

Expand Down Expand Up @@ -3829,7 +3831,8 @@ void SW_NC_write_output(
LogInfo
);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile; // Exit function prematurely due to
// error
}
}

Expand All @@ -3840,6 +3843,8 @@ void SW_NC_write_output(
}
}
}

closeFile: { nc_close(currFileID); }
}

/**
Expand Down Expand Up @@ -5105,7 +5110,7 @@ void SW_NC_read_out_vars(

SW_NC_alloc_output_var_info(OutDom, LogInfo);
if (LogInfo->stopRun) {
return; // Exit prematurely due to error
goto closeFile; // Exit prematurely due to error
}

GetALine(f, inbuf, MAX_FILENAMESIZE); // Ignore the first row (column names)
Expand Down Expand Up @@ -5143,15 +5148,15 @@ void SW_NC_read_out_vars(
scanRes,
NOUT_VAR_INPUTS
);
return; // Exit function prematurely due to error
goto closeFile; // Exit function prematurely due to error
}

// Check if the variable was requested to be output
// Store attribute information for each variable (including names)

doOutputVal = sw_strtoi(input[doOutInd], MyFileName, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile; // Exit function prematurely due to error
}

if (doOutputVal) {
Expand All @@ -5170,7 +5175,7 @@ void SW_NC_read_out_vars(
"nc-output variable name '%s' is too long.",
varKey
);
return; // Exit function prematurely due to error
goto closeFile; // Exit function prematurely due to error
}

get_2d_output_key(varKey, &currOutKey, &varNum, OutDom->nvar_OUT);
Expand Down Expand Up @@ -5302,14 +5307,16 @@ void SW_NC_read_out_vars(
}

if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
/* Exit function prematurely due to error */
goto closeFile;
}
}
} else {
OutDom->outputVarInfo[currOutKey][varNum][index] =
Str_Dup(copyStr, LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
/* Exit function prematurely due to error */
goto closeFile;
}
}
}
Expand All @@ -5322,14 +5329,15 @@ void SW_NC_read_out_vars(
OutDom->units_sw[currOutKey][estVar] =
Str_Dup(SWVarUnits[currOutKey][varNumUnits], LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
/* Exit function prematurely due to error */
goto closeFile;
}
}
} else {
OutDom->units_sw[currOutKey][varNum] =
Str_Dup(SWVarUnits[currOutKey][varNumUnits], LogInfo);
if (LogInfo->stopRun) {
return; // Exit function prematurely due to error
goto closeFile; // Exit function prematurely due to error
}
}
}
Expand All @@ -5342,6 +5350,8 @@ void SW_NC_read_out_vars(
OutDom->use[index] = swFALSE;
}
}

closeFile: { CloseFile(&f, LogInfo); }
}

/** Create unit converters for output variables
Expand Down

0 comments on commit 54abffe

Please sign in to comment.