Skip to content

Validate GD::Image initialization before image operations - #57

Open
damseleng wants to merge 4 commits into
ggerman:mainfrom
damseleng:fix-new-and-tbbox-bugs
Open

Validate GD::Image initialization before image operations#57
damseleng wants to merge 4 commits into
ggerman:mainfrom
damseleng:fix-new-and-tbbox-bugs

Conversation

@damseleng

Copy link
Copy Markdown

Summary

This PR validates GD::Image initialization 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

  • Require width and height when creating a new GD::Image
  • Raise an exception when image methods are called on an uninitialized image object
  • Add defensive checks before image operations that require an initialized image
  • Include the related text bounding box changes

Validation

Tested locally on WSL/Ubuntu.

Before the fix, calling image methods on an uninitialized GD::Image instance could crash the Ruby process. After this change, the same cases raise Ruby exceptions instead.

Validated cases:

ruby -Ilib -Iext -rgd -e 'img = GD::Image.new; puts img.width'# ArgumentError: width and height are required
ruby -Ilib -Iext -rgd -e 'img = GD::Image.allocate; puts img.width'# RuntimeError: image not initialized

The test suite passes:

bundle exec rspec
# 43 examples, 0 failures

CopilotAI review requested due to automatic review settings June 9, 2026 01:14

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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/height in GD::Image.new and add dimension validation + NULL guards across image operations.
  • Fix text_bbox to avoid drawing onto the image during measurement.
  • Bump gem version to 0.3.1 and document the changes in CHANGELOG.md.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
FileDescription
spec/image_spec.rbAdds specs for .new argument validation and NULL-guard behavior on instance methods.
spec/image/initialize_spec.rbUpdates initialization expectations to require width/height.
ext/gd/image.cEnforces required dimensions, adds NULL guards, implements initialize_copy, and adjusts constructors/allocation paths.
ext/gd/text.cPasses NULL image ptr to avoid rendering during bbox calculation.
ruby-libgd.gemspecBumps gem version to 0.3.1.
CHANGELOG.mdDocuments new safety behavior and text bbox fix (needs formatting fixes).
.gitignoreIgnores aider and notebook artifacts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadext/gd/image.c
Comment on lines 15 to 18
if (argc == 0) {
rb_raise(rb_eArgError, "width and height are required");
return self;
}
Comment threadext/gd/image.c
Comment on lines 25 to 27
if (argc != 2) {
rb_raise(rb_eArgError, "expected 0 or 2 arguments");
}
Comment threadext/gd/image.c
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 threadext/gd/image.c
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 threadCHANGELOG.md
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:
Sign up for freeto 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.

3 participants

@damseleng@ggerman