Skip to content

stb_vorbis: slow loads caused by sorting c->sorted_codewords. #1249

Open

Description

@AliceLR

Describe the bug
When loading an ordered codebook with a large number of entries, stb_vorbis can take a very long time to sort the codebook in compute_sorted_huffman. This is due to using qsort (non-inlineable) on a very large array (which is implied to be pre-sorted? Not familiar enough with this part of the Vorbis spec) (edit: on further inspection it's only pre-sorted prior to bit reversal).

Like #1248, this might benefit from early filtering of absurd codebook entries*dimensions values that don't fit in the current packet. This sort is on c->sorted_entries entries, which is bounded by c->entries, so the number of multipliers stored in the packet should directly correspond to this. Otherwise, a sort where the compare can be inlined could help here.

To Reproduce
Load any of these OGG files with stb_vorbis_decode_filename: OGG_slow_loads2.zip

Expected behavior
stb_vorbis should not take several seconds each to reject these input files.

I originally reported this in #1174, but it's completely unrelated to that patch and deserves a separate issue.

Activity

  1. changed the title [-]stb_vorbis: slow loads caused by sorting c->lookup_values.[/-] [+]stb_vorbis: slow loads caused by sorting c->sorted_codewords.[/+] on Nov 18, 2021
  2. ericoporto commented on Nov 7, 2023

    @ericoporto

    Hey, just to check, the PR is indeed unrelated and it doesn't impact in the slow loading time reported here, right?

  3. AliceLR commented on Nov 7, 2023

    @AliceLR
    Author

    Correct. #1174 primarily adds EOF checks to start_decoder. This issue is that malicious or fuzzer-generated Vorbis files can create a large number of codewords that take 40+ seconds sort with qsort, which might be fixable.

  4. added a commit that references this issue on Oct 27, 2026
    a5ccde4
  5. rygorous commented on Oct 27, 2026

    @rygorous
    Collaborator

    I moved the CODEBOOK_LIMIT check further up so that we don't even create the sorted entries when the counts are huge. This limits the max number of sorted entries to CODEBOOK_LIMIT now, i.e. 256k, which while still not instant is a whole lot better than 16M. I'm tentatively tagging this "merged-dev" (meaning fix for this is merged in the dev branch), feel free to reopen if this isn't good enough.

  6. AliceLR commented on Oct 27, 2026

    @AliceLR
    Author

    I noticed adding CODEBOOK_LIMIT to #1960 improved fuzzing speed a bit so I'm not surprised it helps here too, thanks.

    #1174 should also make invalid files get rejected a little faster and would make it easier to fuzz (as libFuzzer hits those leaks a lot). (Also recommend #1172, #1312, #1487, #1490 if there's going to be an stb_vorbis release soon.)

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions