Skip to content

[C++][Acero] Swiss table still has risks of overflow #45506

Closed

Description

@zanmato1984

Describe the enhancement requested

After fixing #44513 and #45334, I kept looking for possible overflow risks in our Swiss join implementation. And my finding follows.

Background

In the Swiss table, a "block" consists of 8 keys (rows). When the number of rows is large enough, a block occupies 40 bytes, aka. num_block_bytes: 4 bytes for each key and one 8 bytes header. Blocks are stored continuously in a buffer namely uint8_t * blocks_. So locating the address of block_id-th block requires indexing like:

blocks_ + num_block_bytes * block_id

Risks

The limit of number of rows in Swiss table is 2^32. So we can have 2^32 / 8 blocks at most, therefore the block_id is normally represented using uint32_t. The num_block_bytes is represented using regular int. If no explicit type promotion is conducted, num_block_bytes * block_id will perform 32-bit multiplication and overflow may happen (2^32 / 8 * 40 > 2^32).

In our code base, there are places where such calculations are done with promoting to 64-bit multiplication so overflow is avoided, to name a few:

const uint8_t* blockbase =
blocks_->data() + static_cast<uint64_t>(iblock) * num_block_bytes;

const uint64_t num_block_bytes = (8 + num_groupid_bits);
blockbase = blocks_->mutable_data() + num_block_bytes * (start_slot_id >> 3);

However requiring such explicit type promotion is error-prone.

What may cause real trouble is where such calculations are still in 32-bit, there is one:

block = *reinterpret_cast<const uint64_t*>(blocks_->mutable_data() +
num_block_bytes * iblock);

(I wish I could come up with a concrete test case that such overflow results in wrong data - it's possible. But it's non-trivial and wouldn't be practical to run in limited resources.)

Given that such code are either correct but error-prone, or possible for real overflow, and once issues happen I can't imagine how painful the debugging will be, we should refactor them in a more overflow-safe fashion.

Component(s)

C++

Activity

  1. self-assigned this
    on Feb 12, 2025
  2. added a commit that references this issue on Feb 17, 2025
  3. added this to the 20.0.0 milestone on Feb 17, 2025
  4. pitrou commented on Feb 17, 2025

    @pitrou
    Member

    Issue resolved by pull request 45515
    #45515

  5. added a commit that references this issue on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions