1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-25 13:05:08 -07:00

Make allocate_memory() error path less confusing

This commit is contained in:
Frank Denis 2018-12-26 17:57:06 +01:00
parent e60049aad1
commit c9842d9af9

View File

@ -80,14 +80,11 @@ allocate_memory(block_region **region, uint32_t m_cost)
return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */ return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */
} }
memory_size = sizeof(block) * m_cost; memory_size = sizeof(block) * m_cost;
if (m_cost == 0 || if (m_cost == 0 || memory_size / m_cost != sizeof(block)) {
memory_size / m_cost !=
sizeof(block)) { /*1. Check for multiplication overflow*/
return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */ return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */
} }
*region = (block_region *) malloc( *region = (block_region *) malloc(sizeof(block_region));
sizeof(block_region)); /*2. Try to allocate region*/ if (*region == NULL) {
if (!*region) {
return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */ return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */
} }
(*region)->base = (*region)->memory = NULL; (*region)->base = (*region)->memory = NULL;
@ -116,6 +113,8 @@ allocate_memory(block_region **region, uint32_t m_cost)
} }
#endif #endif
if (base == NULL) { if (base == NULL) {
free(*region);
*region = NULL;
return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */ return ARGON2_MEMORY_ALLOCATION_ERROR; /* LCOV_EXCL_LINE */
} }
(*region)->base = base; (*region)->base = base;