Skip to content

Commit

Permalink
Fix to explicitly release memory for file handler
Browse files Browse the repository at this point in the history
  • Loading branch information
iliajie committed Aug 3, 2024
1 parent c3098fe commit 07efdf1
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions stats-lib-funcs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ sub stats_read_file_contents {
if (!flock($fh, LOCK_SH)) {
error_stderr("Failed to acquire shared lock on file '$filename': $!");
close($fh);
undef($fh);
return undef;
}
# Read file contents
Expand All @@ -63,8 +64,11 @@ sub stats_read_file_contents {
# Close file
if (!close($fh)) {
error_stderr("Failed to close file '$filename': $!");
undef($fh);
return undef;
}
# Release memory
undef($fh);
# Return file contents
return $contents;
}
Expand All @@ -82,21 +86,27 @@ sub stats_write_file_contents {
if (!flock($fh, LOCK_EX)) {
error_stderr("Failed to acquire exclusive lock on file '$filename': $!");
close($fh);
undef($fh);
return 0;
}
# Write to file
if (!print($fh $contents)) {
error_stderr("Failed to write to file '$filename': $!");
close($fh);
undef($fh);
return 0;
}
# Unlock the file
flock($fh, LOCK_UN);
# Close file
if (!close($fh)) {
error_stderr("Failed to close file '$filename': $!");
undef($fh);
return 0;
}
# Release memory
undef($fh);
# Return success
return 1;
}

Expand Down

0 comments on commit 07efdf1

Please sign in to comment.