Validate GD::Image initialization before image operations#57
Open
damseleng wants to merge 4 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR hardens GD::Image construction and instance-method safety by rejecting empty initialization, adding NULL-pointer guards, and making text bounding-box measurement side-effect free, along with a version bump and changelog updates.
Changes:
- Require
width/heightinGD::Image.newand add dimension validation + NULL guards across image operations. - Fix
text_bboxto avoid drawing onto the image during measurement. - Bump gem version to
0.3.1and document the changes inCHANGELOG.md.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| spec/image_spec.rb | Adds specs for .new argument validation and NULL-guard behavior on instance methods. |
| spec/image/initialize_spec.rb | Updates initialization expectations to require width/height. |
| ext/gd/image.c | Enforces required dimensions, adds NULL guards, implements initialize_copy, and adjusts constructors/allocation paths. |
| ext/gd/text.c | Passes NULL image ptr to avoid rendering during bbox calculation. |
| ruby-libgd.gemspec | Bumps gem version to 0.3.1. |
| CHANGELOG.md | Documents new safety behavior and text bbox fix (needs formatting fixes). |
| .gitignore | Ignores aider and notebook artifacts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
15
to
18
| if (argc == 0) { | ||
| rb_raise(rb_eArgError, "width and height are required"); | ||
| return self; | ||
| } |
Comment on lines
25
to
27
| if (argc != 2) { | ||
| rb_raise(rb_eArgError, "expected 0 or 2 arguments"); | ||
| } |
Comment on lines
+20
to
+23
| gd_image_wrapper *wrap; | ||
| TypedData_Get_Struct(self, gd_image_wrapper, &gd_image_type, wrap); | ||
|
|
||
| wrap->img = NULL; |
Comment on lines
80
to
84
| static VALUE gd_image_s_new_true_color(VALUE klass, VALUE width, VALUE height) { | ||
| VALUE obj = rb_class_new_instance(0, NULL, klass); | ||
| gd_image_initialize_true_color(obj, width, height); | ||
| return obj; | ||
| VALUE img = rb_obj_alloc(klass); | ||
| rb_funcall(img, rb_intern("initialize"), 2, width, height); | ||
| return img; | ||
| } |
Comment on lines
+16
to
+17
| - `GD::Image.new` no longer returns an object with `img == NULL`. | ||
| - Calling `GD::Image.new` without dimensions now raises: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR validates
GD::Imageinitialization state before image operations.It changes invalid or uninitialized image usage from native process crashes to Ruby exceptions, and includes the related text bounding box updates from
feature/fix-new-and-tbbox-bugs.Changes
GD::ImageValidation
Tested locally on WSL/Ubuntu.
Before the fix, calling image methods on an uninitialized
GD::Imageinstance could crash the Ruby process. After this change, the same cases raise Ruby exceptions instead.Validated cases:
The test suite passes: