Skip to content

[Bug]: Hash-keyed sets of file values can make a read or save quadratic #248

Backlog

Description

@decriptor

What happens

Several readers and savers key a hash set or dictionary on values that come from the file. Where those keys can be made to collide, a hostile file can make the work quadratic in its size:

  • long and ulong keys:

    • Matroska TargetTypeValue level sets (MatroskaTag, three sites);
    • Audible and TIFF size maps (AudibleFile, TiffSize);
    • AppleHeadroom;
    • IfdGraph.

    A 64-bit key hashes as its high half XOR its low half, so every (k << 32) | k hashes to 0.

  • int and uint keys: MatroskaRewrite's length map, and MP4 track IDs (Mp4ChapterCodec, Mp4PictureSkip).

  • String keys:

    • InvolvedPeopleFrame;
    • ImageGainMap;
    • Mp4QuickTimeKeys;
    • the ApeTag item dictionary.

    String hashing is not randomized on .NET Framework, where netstandard2.0 runs.

The Matroska save paths moved to comparison-based sets for this reason in the follow-ups branch (#244, #247).

Expected

No read or save takes more than O(n log n) time in a count the file controls, whatever the values. Each site should use a comparison-based structure, or be bounded so that collisions cannot add up. Each one needs a scaling test with colliding keys.

Activity

  1. added this to the v0.8.0 milestone on Dec 4, 2026
  2. added
    bug
    on Dec 4, 2026
  3. decriptor commented on Dec 4, 2026

    @decriptor
    OwnerAuthor

    Reopening. PR #249 mentioned this issue as related and still open, but GitHub read its wording ("not closed: #248") as a closing keyword. #249 fixed this only on the Matroska save paths it changed. The sites listed here are untouched.

  4. decriptor commented on Dec 4, 2026

    @decriptor
    OwnerAuthor

    Another site: Mp4Tag._preservedByType is a Dictionary<uint, List<int>> keyed by the 4-byte type of each unmapped ilst child, and a uint hashes to itself. 56,000 empty atoms whose types collide (a 448 KB file) take about 3.3 s to read, against 48 ms for sequential types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions