Ignoring the memory leak and the fact you're not returning anything in the non-error case plus the potential issues with opening the same file twice, something like this might work :
Code:
int edb_initBlob(edb_blobStore *store, char *path)
{
int rc = EDB_OK; /* whatever a good rc is */
size_t ret = 0;
u32 *blobInf = NULL;
store->writer.fp = store->reader.fp = NULL;
store->writer.fp = fopen(path, "rb+");
if(store->writer.fp)
{
store->reader.fp = fopen(path, "rb");
if(store->reader.fp)
{
blobInf = malloc(sizeof(u32) * 4);
if(blobInf)
{
ret = fread(blobInf, sizeof(u32), 4, store->reader.fp);
}
}
}
if (!store->writer.fp || !store->reader.fp)
rc = EDB_BLOB_IOERR;
else if (!blobInf)
rc = EDB_BLOB_NOMEM;
else if (ret != 4)
rc = EDB_BLOB_IOERR;
if (rc != EDB_OK)
{
if (store->writer.fp)
fclose(store->writer.fp);
if (store->reader.fp)
fclose(store->reader.fp);
if (blobInf)
free (blobInf); /* should be freed regardless of RC to avoid mem leak, but this matches the original code */
}
return rc;
}
Not saying it's better or worse, just a different approach that's a bit closer to a C++ RAII feel. Single entry, single exit, clean up on error before leaving.