HomePhabricator

cuckoocache: Check for uint32 overflow in setup_bytes

Description

cuckoocache: Check for uint32 overflow in setup_bytes

Summary:
This fixes an potential overflow which existed prior to this patchset.

If CuckooCache::cache<Element, Hash>::setup_bytes is called with a
size_t bytes which, when divided by sizeof(Element), does not fit into
an uint32_t, the implicit conversion to uint32_t in the call to setup
will result in an overflow.

At least on x86_64, this overflow is possible:

static_assert(std::numeric_limits<size_t>::max() / 32 <= std::numeric_limits<uint32_t>::max());
static_assert(std::numeric_limits<size_t>::max() / 4 <= std::numeric_limits<uint32_t>::max());

This commit detects such cases and signals to callers that the `size_t
bytes` input is too large.

This is a backport of core#25527
https://github.com/bitcoin/bitcoin/pull/25527/commits/82d3058539f54ebad745e2b02b61df01aa832a54

Depends on D15736

Test Plan: ninja all check-all

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D15737

Details

Provenance
Carl Dong <contact@carldong.me>Authored on Jul 1 2022, 23:53
PiRKCommitted on Mar 19 2024, 16:21
PiRKPushed on Mar 19 2024, 16:21
Reviewer
Restricted Project
Differential Revision
D15737: cuckoocache: Check for uint32 overflow in setup_bytes
Parents
rABCf2314681f2d4: validationcaches: Abolish arbitrary limit
Branches
Unknown
Tags
Unknown