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:
Tobias Brunner
2026-07-24 08:47:38 +02:00
parent 4d843d3da8
commit a4a123eb4f
4 changed files with 24 additions and 4 deletions
@@ -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);
}