Skip to content

fix(py): reject non-RGBA images before native calls - #780

Open
krishna2707 wants to merge 1 commit into
Ryan-Millard:mainfrom
krishna2707:fix/python-rgba-validation
Open

krishna2707 wants to merge 1 commit into
Ryan-Millard:mainfrom
krishna2707:fix/python-rgba-validation

Conversation

@krishna2707

Copy link
Copy Markdown

Changes & Reason

Changes

Added validation to the shared Python wrapper to require image arrays shaped (H, W, 4). Invalid channel layouts now raise a clear ValueError before reaching native image functions.

Reason

The native code expects RGBA input. Grayscale arrays could otherwise cause buffer overreads and crash the Python process.

Related Issues

Fixes: #772

Testing & Verification

  • Python smoke check with a stubbed native extension: valid RGBA accepted; grayscale, RGB, 5-channel, and 4D inputs rejected with ValueError.
  • py_compile passed.
  • git diff --check passed.
  • Full native package integration test not run.

Additional Resources

None

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: Ryan-Millard/Img2Num/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 2a907769-8ae5-40ed-b4e0-90cb71586d17
📥 Commits

Reviewing files that changed from the base of the PR and between 67e5e60 and 6fc9ae6.

📒 Files selected for processing: 1
  • packages/py/img2num/api.py

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📜 Recent review details
🧰 Additional context used
📚 Code guidelines: 1
.editorconfig — unknown
📓 Path-based instructions: 2
Python source files.

⚙️ CodeRabbit configuration file

Files:

  • packages/py/img2num/api.py
Source excerpt: [*] charset = utf-8 end_of_line = lf indent_style = space indent_size = 2 trim_trailing_whitespace = true insert_final_newline = true max_line_length = 120

📄 CodeRabbit inference engine (.editorconfig)

Files:

  • packages/py/img2num/api.py
  • _inject_dimensions now accepts only RGBA image arrays with shape (H, W, 4).
  • Invalid shapes raise ValueError before the wrapped native function is called.
  • The author reports that the smoke check, py_compile, and git diff --check passed. The full native package integration test was not run.
Author Lines added Lines removed
krishna2707 2 2

Walkthrough

The Python API now requires image inputs to have three dimensions and four channels. _inject_dimensions raises ValueError for inputs that do not meet this shape requirement.

Changes

Image Input Validation

Layer / File(s) Summary
Validate image shape
packages/py/img2num/api.py
_inject_dimensions now rejects inputs unless they have shape (H, W, 4).

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 6fc9a

The PR adds pre-native shape validation for non-RGBA inputs. Other dtypes are already rejected by the typed bindings, leaving no material merge risk evident in the supplied context.

Pre-merge checks: Passed 6 · Failed 2

Failed Failed checks: 2 warnings

Check name Status Explanation Resolution
Linked Issues check Warning Issue #772 requires rejection of inputs that do not use the documented four-channel uint8 RGBA format. The change correctly rejects arrays whose shape is not exactly (H, W, 4) before the wrapped nat… Add a dtype check for np.uint8 in _inject_dimensions and raise the documented clear Python exception before the native wrapper is called. Add an automated test for a non-uint8 (H, W, 4) array.
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 6 passed
Check name Status Explanation
Out of Scope Changes check Passed The pull request changes only the shared Python image validation in packages/py/img2num/api.py. Shape validation for the affected image APIs is directly connected to issue #772. No unrelated change …
No Ai Slop Pr Description Passed The description is specific to this PR. It explains the (H, W, 4) validation, the ValueError, the native-call safety reason, affected input cases, and reported verification. The diff confirms the …
No Strangely-Named Root Markdown Files Passed The pull request changes only packages/py/img2num/api.py. It adds no files at the repository root, so it does not add any unconventional root Markdown file.
Coderabbit Config Needs Update Passed PASS. The PR changes only packages/py/img2num/api.py. The base .coderabbit.yaml already contains reviews.path_instructions for **/*.py, and Python tooling is configured (ruff and pylint). …
Title check Passed The title uses the conventional fix(py): prefix and clearly describes rejecting non-RGBA images before native calls.
Description check Passed The description directly explains the RGBA validation change, its safety reason, testing, and related issue.
Full details: Out of Scope Changes check

Explanation

The pull request changes only the shared Python image validation in packages/py/img2num/api.py. Shape validation for the affected image APIs is directly connected to issue #772. No unrelated change is shown.

Full details: No Ai Slop Pr Description

Explanation

The description is specific to this PR. It explains the (H, W, 4) validation, the ValueError, the native-call safety reason, affected input cases, and reported verification. The diff confirms the described change in packages/py/img2num/api.py; no generic AI boilerplate appears.

Full details: Coderabbit Config Needs Update

Explanation

PASS. The PR changes only packages/py/img2num/api.py. The base .coderabbit.yaml already contains reviews.path_instructions for **/*.py, and Python tooling is configured (ruff and pylint). The diff adds no linter/formatter config, CI/tooling config, or uncovered file extension.

Full details: Linked Issues check

Explanation

Issue #772 requires rejection of inputs that do not use the documented four-channel uint8 RGBA format. The change correctly rejects arrays whose shape is not exactly (H, W, 4) before the wrapped native call. It does not validate image.dtype, so a non-uint8 array with shape (H, W, 4) is not rejected by the shared wrapper. The reported smoke check covers channel layouts and rank, but it does not establish dtype rejection.

Resolution

Add a dtype check for np.uint8 in _inject_dimensions and raise the documented clear Python exception before the native wrapper is called. Add an automated test for a non-uint8 (H, W, 4) array.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
✨ Simplify code
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks each pixel’s place
Four channels pass the shape-bound gate
Two dimensions now raise an error
Three make the image shape appear
The rabbit hops along its way

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

fix(py): grayscale arrays crash image functions

1 participant