hasher: Avoid theoretical memory leaks for hashers that could potentially fail
These `get_hash()` implementations could potentially fail (realistically only for serious system errors like OOM). This change ensures we comply with the documented behavior (i.e. only allocate memory on success), as no callers currently expect they have to clean up on failure.
This commit is contained in:
@@ -119,7 +119,12 @@ METHOD(hasher_t, allocate_hash, bool,
|
||||
if (hash)
|
||||
{
|
||||
*hash = chunk_alloc(get_hash_size(this));
|
||||
return get_hash(this, chunk, hash->ptr);
|
||||
if (!get_hash(this, chunk, hash->ptr))
|
||||
{
|
||||
chunk_free(hash);
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
return get_hash(this, chunk, NULL);
|
||||
}
|
||||
|
||||
@@ -89,7 +89,12 @@ METHOD(hasher_t, allocate_hash, bool,
|
||||
if (hash)
|
||||
{
|
||||
*hash = chunk_alloc(get_hash_size(this));
|
||||
return get_hash(this, chunk, hash->ptr);
|
||||
if (!get_hash(this, chunk, hash->ptr))
|
||||
{
|
||||
chunk_free(hash);
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
return get_hash(this, chunk, NULL);
|
||||
}
|
||||
|
||||
@@ -77,7 +77,12 @@ METHOD(hasher_t, allocate_hash, bool,
|
||||
if (hash)
|
||||
{
|
||||
*hash = chunk_alloc(get_hash_size(this));
|
||||
return get_hash(this, chunk, hash->ptr);
|
||||
if (!get_hash(this, chunk, hash->ptr))
|
||||
{
|
||||
chunk_free(hash);
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
return get_hash(this, chunk, NULL);
|
||||
}
|
||||
|
||||
@@ -84,7 +84,12 @@ METHOD(hasher_t, allocate_hash, bool,
|
||||
if (hash)
|
||||
{
|
||||
*hash = chunk_alloc(get_hash_size(this));
|
||||
return get_hash(this, chunk, hash->ptr);
|
||||
if (!get_hash(this, chunk, hash->ptr))
|
||||
{
|
||||
chunk_free(hash);
|
||||
return FALSE;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
return get_hash(this, chunk, NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user