Skip to content

Commit

Permalink
Fix: "Freed pointer was not allocated error"
Browse files Browse the repository at this point in the history
- close #356

- given that `SW_SoilWat->hist.file_prefix` is not initialized when `SW_SWC_construct()` is called (by SOILWAT2), then `!isnull(SW_SoilWat->hist.file_prefix)` was TRUE and we freed the not allocated pointer.

-> now, initialize instead of free the pointer

-> NOTE: rSOILWAT2 may now have a memory leak if it calls this function with a previously allocated `file_prefix` (e.g., if a `sw_exec()` fails and doesn't properly clean up).
  • Loading branch information
dschlaep committed Jul 18, 2023
1 parent 1e9e449 commit e429d21
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# NEWS

# SOILWAT2 v7.1.0-9000
* Bugfixes
* Fix an error where a pointer was freed even though it was not allocated
(issue #356; @dschlaep).

# SOILWAT2 v7.0.0
* This version produces nearly identical simulation output
Expand Down
8 changes: 3 additions & 5 deletions src/SW_SoilWater.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,9 @@ void SW_SWC_construct(SW_SOILWAT* SW_SoilWat, LOG_INFO* LogInfo) {
/* =================================================== */
OutPeriod pd;

// Clear memory before setting it
if (!isnull(SW_SoilWat->hist.file_prefix)) {
Mem_Free(SW_SoilWat->hist.file_prefix);
SW_SoilWat->hist.file_prefix = NULL;
}
/* initialize pointer */
SW_SoilWat->hist.file_prefix = NULL;


// Clear the module structure:
memset(SW_SoilWat, 0, sizeof(SW_SOILWAT));
Expand Down

0 comments on commit e429d21

Please sign in to comment.