Skip to content

fix(codecs): improve robustness of AVCC/HVCC parsing and remove ful logic#55

Merged
gkozlenko merged 1 commit intomasterfrom
codecs-strict-validation
Jan 31, 2026
Merged

fix(codecs): improve robustness of AVCC/HVCC parsing and remove ful logic#55
gkozlenko merged 1 commit intomasterfrom
codecs-strict-validation

Conversation

@gkozlenko
Copy link
Copy Markdown
Owner

Refactored H.264 and H.265 codec parsing to be more robust and stateless.

Changes:

  • Removed class-level state this._pos and the _readNalUnit method; parse() is now idempotent and uses local variables.
  • Implemented strict bounds checks to prevent RangeError on malformed or incomplete configuration buffers.
  • Added validation for configuration version (must be 1).
  • Enforced NAL unit length size to be 4 bytes, throwing specific errors for unsupported sizes to ensure compatibility.
  • Ensure this._units is only updated after a fully successful parse to prevent inconsistent states.
  • Added unit tests covering edge cases: null input, short buffers, invalid versions, and unsupported NAL sizes.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @gkozlenko, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the robustness and reliability of AAC, H.264, and H.265 codec parsing. By transitioning to a stateless parsing approach and introducing rigorous input validation, the changes prevent common errors arising from malformed or incomplete configuration data. This ensures that the codec parsing logic is more resilient to unexpected inputs and maintains a consistent internal state, ultimately leading to more stable media processing.

Highlights

  • Stateless Codec Parsing: Refactored H.264 and H.265 codec parsing methods to be stateless and idempotent by removing class-level state variables like this._pos and the _readNalUnit method, utilizing local variables instead.
  • Improved Robustness and Validation: Implemented strict bounds checks to prevent RangeError on malformed or incomplete configuration buffers for AAC, H.264, and H.265. Added validation for configuration version (must be 1) and enforced NAL unit length size to be 4 bytes for H.264/H.265, throwing specific errors for unsupported sizes.
  • Consistent State Management: Ensured that the _units array in H.264 and H.265 codecs is only updated after a fully successful parsing operation, preventing inconsistent states if parsing fails midway.
  • Comprehensive Unit Testing: Added new unit tests across AAC, H.264, and H.265 codecs to cover various edge cases, including null input, short buffers, invalid configuration versions, and unsupported NAL unit sizes, validating the new error handling.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly improves the robustness and design of the H.264 and H.265 codec parsers. By removing class-level state, the parse methods are now idempotent and easier to reason about. The introduction of strict bounds checking, version validation, and atomic updates to the parsed units prevents various errors with malformed data and makes the implementation much more resilient. The accompanying unit tests are thorough and cover the new edge cases well. I have one minor suggestion regarding a comment.

@gkozlenko gkozlenko force-pushed the codecs-strict-validation branch from 6bcbee3 to 16305c9 Compare January 31, 2026 08:34
…ful logic

Refactored H.264 and H.265 codec parsing to be more robust and stateless.

Changes:
- Removed class-level state `this._pos` and the `_readNalUnit` method; `parse()` is now idempotent and uses local variables.
- Implemented strict bounds checks to prevent `RangeError` on malformed or incomplete configuration buffers.
- Added validation for configuration version (must be 1).
- Enforced NAL unit length size to be 4 bytes, throwing specific errors for unsupported sizes to ensure compatibility.
- Ensure `this._units` is only updated after a fully successful parse to prevent inconsistent states.
- Added unit tests covering edge cases: null input, short buffers, invalid versions, and unsupported NAL sizes.
@github-actions
Copy link
Copy Markdown
Contributor

Test Coverage Summary Statistics

  • Lines: 94.72%
  • Statements: 94.75%
  • Functions: 92.88%
  • Branches: 78.53%

@gkozlenko gkozlenko merged commit a50f513 into master Jan 31, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant