cube_hash.h: finalize CubeHash if there's no incomplete block.

Previously, we assume the existence of a incomplete block at
end of the input. However, it's possible that input's an exact
multiple of block size. In this case, the first argument of
process_final_incomplete_block() will be one-past-the-last
element, the second argument will be zero. This' an ill-defined
call, and it will trigger an assertion failure of std::vector

Assertion '__builtin_expect(__n < this->size(), true)' failed.

This commit introduced a check. If we see the length of the last
incomplete block is zero, we call

    process_final_incomplete_block(NULL, 0);

which immediately finalizes CubeHash without hashing additional
data.

Signed-off-by: Tom Li <tomli@tomli.me>
This commit is contained in:
Tom Li 2019-01-03 21:32:11 +08:00
parent 29f7826b1e
commit 025a958a20
No known key found for this signature in database
GPG Key ID: FAD3EB05E88E8D6D

View File

@ -39,7 +39,11 @@ public:
for (i = 0; i + B <= a.size(); i += B)
state.process_block (& (a[i]));
state.process_final_incomplete_block (& (a[i]), a.size() - i);
if (a.size() - i != 0)
state.process_final_incomplete_block (& (a[i]), a.size() - i);
else
state.process_final_incomplete_block (NULL, 0); //empty block, just finalize
std::vector<byte> result;
result.resize (H, 0);
state.get_hash (& (result[0]));