mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
Implement cleanup function for open failures to ensure proper resource deallocation
This commit is contained in:
48
src/open.c
48
src/open.c
@@ -28,6 +28,28 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "utarray.h"
|
#include "utarray.h"
|
||||||
|
|
||||||
|
static void cleanup_open_failure(aaruformatContext *ctx)
|
||||||
|
{
|
||||||
|
if(ctx == NULL) return;
|
||||||
|
|
||||||
|
if(ctx->imageStream != NULL)
|
||||||
|
{
|
||||||
|
fclose(ctx->imageStream);
|
||||||
|
ctx->imageStream = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(ctx->readableSectorTags);
|
||||||
|
ctx->readableSectorTags = NULL;
|
||||||
|
|
||||||
|
free(ctx->imageInfo.ApplicationVersion);
|
||||||
|
ctx->imageInfo.ApplicationVersion = NULL;
|
||||||
|
|
||||||
|
free(ctx->imageInfo.Version);
|
||||||
|
ctx->imageInfo.Version = NULL;
|
||||||
|
|
||||||
|
free(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Opens an existing AaruFormat image file.
|
* @brief Opens an existing AaruFormat image file.
|
||||||
*
|
*
|
||||||
@@ -146,7 +168,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
{
|
{
|
||||||
FATAL("Error %d opening file %s for reading", errno, filepath);
|
FATAL("Error %d opening file %s for reading", errno, filepath);
|
||||||
error_no = errno;
|
error_no = errno;
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = error_no;
|
errno = error_no;
|
||||||
|
|
||||||
TRACE("Exiting aaruf_open() = NULL");
|
TRACE("Exiting aaruf_open() = NULL");
|
||||||
@@ -160,7 +182,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
if(read_bytes != sizeof(AaruHeader))
|
if(read_bytes != sizeof(AaruHeader))
|
||||||
{
|
{
|
||||||
FATAL("Could not read header");
|
FATAL("Could not read header");
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_FILE_TOO_SMALL;
|
errno = AARUF_ERROR_FILE_TOO_SMALL;
|
||||||
|
|
||||||
TRACE("Exiting aaruf_open() = NULL");
|
TRACE("Exiting aaruf_open() = NULL");
|
||||||
@@ -170,7 +192,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
if(ctx->header.identifier != DIC_MAGIC && ctx->header.identifier != AARU_MAGIC)
|
if(ctx->header.identifier != DIC_MAGIC && ctx->header.identifier != AARU_MAGIC)
|
||||||
{
|
{
|
||||||
FATAL("Incorrect identifier for AaruFormat file: %8.8s", (char *)&ctx->header.identifier);
|
FATAL("Incorrect identifier for AaruFormat file: %8.8s", (char *)&ctx->header.identifier);
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_NOT_AARUFORMAT;
|
errno = AARUF_ERROR_NOT_AARUFORMAT;
|
||||||
|
|
||||||
TRACE("Exiting aaruf_open() = NULL");
|
TRACE("Exiting aaruf_open() = NULL");
|
||||||
@@ -186,7 +208,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
|
|
||||||
if(read_bytes != sizeof(AaruHeaderV2))
|
if(read_bytes != sizeof(AaruHeaderV2))
|
||||||
{
|
{
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_FILE_TOO_SMALL;
|
errno = AARUF_ERROR_FILE_TOO_SMALL;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -197,7 +219,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
{
|
{
|
||||||
FATAL("Incompatible AaruFormat version %d.%d found, maximum supported is %d.%d", ctx->header.imageMajorVersion,
|
FATAL("Incompatible AaruFormat version %d.%d found, maximum supported is %d.%d", ctx->header.imageMajorVersion,
|
||||||
ctx->header.imageMinorVersion, AARUF_VERSION_V2, 0);
|
ctx->header.imageMinorVersion, AARUF_VERSION_V2, 0);
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_INCOMPATIBLE_VERSION;
|
errno = AARUF_ERROR_INCOMPATIBLE_VERSION;
|
||||||
|
|
||||||
TRACE("Exiting aaruf_open() = NULL");
|
TRACE("Exiting aaruf_open() = NULL");
|
||||||
@@ -212,7 +234,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
if(ctx->readableSectorTags == NULL)
|
if(ctx->readableSectorTags == NULL)
|
||||||
{
|
{
|
||||||
FATAL("Could not allocate memory for readable sector tags bitmap");
|
FATAL("Could not allocate memory for readable sector tags bitmap");
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
errno = AARUF_ERROR_NOT_ENOUGH_MEMORY;
|
||||||
|
|
||||||
TRACE("Exiting aaruf_open() = NULL");
|
TRACE("Exiting aaruf_open() = NULL");
|
||||||
@@ -243,7 +265,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
pos = fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET);
|
pos = fseek(ctx->imageStream, ctx->header.indexOffset, SEEK_SET);
|
||||||
if(pos < 0)
|
if(pos < 0)
|
||||||
{
|
{
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -252,7 +274,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
pos = ftell(ctx->imageStream);
|
pos = ftell(ctx->imageStream);
|
||||||
if(pos != ctx->header.indexOffset)
|
if(pos != ctx->header.indexOffset)
|
||||||
{
|
{
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -264,7 +286,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
(signature != IndexBlock && signature != IndexBlock2 && signature != IndexBlock3))
|
(signature != IndexBlock && signature != IndexBlock2 && signature != IndexBlock3))
|
||||||
{
|
{
|
||||||
FATAL("Could not read index header or incorrect identifier %4.4s", (char *)&signature);
|
FATAL("Could not read index header or incorrect identifier %4.4s", (char *)&signature);
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
|
|
||||||
TRACE("Exiting aaruf_open() = NULL");
|
TRACE("Exiting aaruf_open() = NULL");
|
||||||
@@ -282,7 +304,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
{
|
{
|
||||||
FATAL("Could not process index.");
|
FATAL("Could not process index.");
|
||||||
utarray_free(index_entries);
|
utarray_free(index_entries);
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
errno = AARUF_ERROR_CANNOT_READ_INDEX;
|
||||||
|
|
||||||
TRACE("Exiting aaruf_open() = NULL");
|
TRACE("Exiting aaruf_open() = NULL");
|
||||||
@@ -322,7 +344,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
if(error_no != AARUF_STATUS_OK)
|
if(error_no != AARUF_STATUS_OK)
|
||||||
{
|
{
|
||||||
utarray_free(index_entries);
|
utarray_free(index_entries);
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = error_no;
|
errno = error_no;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -336,7 +358,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
if(error_no != AARUF_STATUS_OK)
|
if(error_no != AARUF_STATUS_OK)
|
||||||
{
|
{
|
||||||
utarray_free(index_entries);
|
utarray_free(index_entries);
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = error_no;
|
errno = error_no;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -349,7 +371,7 @@ void *aaruf_open(const char *filepath)
|
|||||||
if(error_no != AARUF_STATUS_OK)
|
if(error_no != AARUF_STATUS_OK)
|
||||||
{
|
{
|
||||||
utarray_free(index_entries);
|
utarray_free(index_entries);
|
||||||
free(ctx);
|
cleanup_open_failure(ctx);
|
||||||
errno = error_no;
|
errno = error_no;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user