acdb: Fix stale delta file handle reuse - #114
shijlin-1224 wants to merge 1 commit into
Conversation
Several delta-file management paths could operate on a FILE handle after it had already been closed, leading to stale-pointer dereferences and double-close errors. Invalidate each handle immediately after fclose() so subsequent cleanup paths cannot reuse it. Signed-off-by: Shijie Lin <shijlin@qti.qualcomm.com>
Qualcomm AI ReviewClick to expand Code ReviewReviewed Commits: 15f58ad
Several delta-file management paths could operate on a Signed-off-by: Shijie Lin shijlin@qti.qualcomm.com PR OverviewThis pull request adds null pointer checks before closing file handles in the delta file manager to prevent potential crashes from attempting to close already-closed or uninitialized file handles. Files Changed Summary
Changes Made
Critical Issues Identified
Positive Aspects
[ERROR HANDLING] Missing error handling for ar_fclose() in AcdbDeltaDataCmdSave() - High SeverityIn the If
This is particularly critical since this function is responsible for persisting delta data, and a failed close operation could result in data loss. Fixed Code Snippet: //Close and delete old delta file
if (*fhandle != NULL)
{
status = ar_fclose(*fhandle);
*fhandle = NULL;
if (AR_FAILED(status))
{
ACDB_ERR("Error[%d]: Failed to close delta file before deletion", status);
goto end;
}
}[FUNCTIONALITY] Potential double-free risk in error path of AcdbDeltaDataCmdSave() - Medium SeverityIn Later in the function, if any subsequent operation fails and execution reaches the This could lead to inconsistent state where:
This inconsistency could cause issues in subsequent operations that check these flags. Fixed Code Snippet: status = ar_fdelete(db_info->delta_file_path.path);
if (AR_FAILED(status))
{
ACDB_ERR("Error[%d]: Failed to delete delta file", status);
db_info->exists = FALSE; // Mark as non-existent since we can't delete it
db_info->file_size = 0;
goto end;
} |
|
|
||
| fhandle = &db_info->file_handle; | ||
|
|
||
| /* Close and delete old delta file */ | ||
| ar_fclose(*fhandle); | ||
| //Close and delete old delta file | ||
| if (*fhandle != NULL) | ||
| { | ||
| ar_fclose(*fhandle); | ||
| *fhandle = NULL; | ||
| } | ||
|
|
||
| status = ar_fdelete(db_info->delta_file_path.path); | ||
| if (AR_FAILED(status)) |
There was a problem hiding this comment.
[ERROR HANDLING] Missing error handling for ar_fclose() in AcdbDeltaDataCmdSave() - High Severity
In the AcdbDeltaDataCmdSave() function at lines 331-335, the code calls ar_fclose(*fhandle) but does not check the return status. This is inconsistent with the other two modified functions (AcdbDeltaDeleteFile() and AcdbDeltaDataSwapDelta()) which both check the return status and handle errors appropriately.
If ar_fclose() fails, the function continues execution and attempts to delete the file, which could lead to:
- File corruption if the close operation failed due to buffered data not being flushed
- Resource leaks if the file handle isn't properly released
- Inconsistent state where the file handle is set to NULL but the underlying resource isn't freed
This is particularly critical since this function is responsible for persisting delta data, and a failed close operation could result in data loss.
Fixed Code Snippet
//Close and delete old delta file
if (*fhandle != NULL)
{
status = ar_fclose(*fhandle);
*fhandle = NULL;
if (AR_FAILED(status))
{
ACDB_ERR("Error[%d]: Failed to close delta file before deletion", status);
goto end;
}
}| ar_fclose(*fhandle); | ||
| *fhandle = NULL; | ||
| } | ||
|
|
||
| status = ar_fdelete(db_info->delta_file_path.path); | ||
| if (AR_FAILED(status)) |
There was a problem hiding this comment.
[FUNCTIONALITY] Potential double-free risk in error path of AcdbDeltaDataCmdSave() - Medium Severity
In AcdbDeltaDataCmdSave(), after closing and deleting the old delta file (lines 331-342), if ar_fdelete() fails, the function goes to the end label. However, the file handle has already been closed and set to NULL at line 334.
Later in the function, if any subsequent operation fails and execution reaches the end label, the file handle is already NULL, which is safe. However, there's a subtle issue: if the delete fails but the file was successfully closed, the db_info->file_handle is NULL, but db_info->exists and db_info->file_size are not updated to reflect the current state.
This could lead to inconsistent state where:
- The file handle is NULL (file is closed)
- But
db_info->existsstill indicates the file exists - And
db_info->file_sizestill has the old size
This inconsistency could cause issues in subsequent operations that check these flags.
Fixed Code Snippet
status = ar_fdelete(db_info->delta_file_path.path);
if (AR_FAILED(status))
{
ACDB_ERR("Error[%d]: Failed to delete delta file", status);
db_info->exists = FALSE; // Mark as non-existent since we can't delete it
db_info->file_size = 0;
goto end;
}
Several delta-file management paths could operate on a FILE handle after it had already been closed, leading to stale-pointer dereferences and double-close errors. Invalidate each handle immediately after fclose() so subsequent cleanup paths cannot reuse it.