Skip to content

Remove incorrect defers that would free internal vips pointers#511

Merged
davidbyttow merged 4 commits intomasterfrom
fix/remove-incorrect-defers
Mar 6, 2026
Merged

Remove incorrect defers that would free internal vips pointers#511
davidbyttow merged 4 commits intomasterfrom
fix/remove-incorrect-defers

Conversation

@davidbyttow
Copy link
Owner

@davidbyttow davidbyttow commented Mar 5, 2026

Summary

Four functions had defer calls to free memory returned by vips getter functions that return internal pointers (not caller-owned allocations):

  • vipsImageGetMetaLoader: vips_image_get_string returns internal pointer, must not free
  • vipsImageGetDelay: vips_image_get_array_int returns internal pointer, must not free
  • vipsImageGetBackground: vips_image_get_array_double returns internal pointer, must not free
  • vipsImageGetString: vips_image_get_string returns internal pointer, must not free

These defers were harmless in practice (Go's defer f(x) evaluates x at the defer statement, capturing nil before the C call populated the pointer), but they are incorrect and misleading. If someone "fixed" them with closures, they would corrupt vips internal data.

Test plan

  • go build ./vips/
  • go test ./vips/ -run "TestImage_Metadata|TestImage_Flip|TestImage_Crop|TestImage_Embed|TestImage_Resize"

🤖 Generated with Claude Code

Note

Remove incorrect defers that free internal VIPS-owned pointers in vips/operations.go

Drop defer frees for VIPS-returned buffers and strings to avoid releasing internal memory in vips/operations.go.

📍Where to Start

Start with the updated pointer ownership handling in vipsImageGetMetaLoader, vipsImageGetDelay, vipsImageGetBackground, and vipsImageGetString in vips/operations.go.

Macroscope summarized f69fa90.

davidbyttow and others added 4 commits March 5, 2026 15:53
get_meta_loader wraps vips_image_get_string which returns an internal
vips pointer that must not be freed by the caller. The defer was
harmless (captured nil due to Go's defer evaluation semantics) but
incorrect and misleading.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
get_image_delay wraps vips_image_get_array_int which returns an
internal vips pointer that must not be freed by the caller.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
get_background wraps vips_image_get_array_double which returns an
internal vips pointer that must not be freed by the caller.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
image_get_string wraps vips_image_get_string which returns an internal
vips pointer that must not be freed by the caller.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@coveralls
Copy link

Coverage Status

coverage: 36.343% (-0.02%) from 36.365%
when pulling f69fa90 on fix/remove-incorrect-defers
into 12bbea8 on master.

@davidbyttow davidbyttow merged commit d929cf5 into master Mar 6, 2026
3 checks passed
@davidbyttow davidbyttow deleted the fix/remove-incorrect-defers branch March 6, 2026 00:00
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.

2 participants