Skip to content

Fix: reset the colour when setting a fill or stroke colour space - #50

Open
DTW-Thalion wants to merge 1 commit into
gnustep:masterfrom
DTW-Thalion:fix/cgcontext-colorspace-nullcheck
Open

Fix: reset the colour when setting a fill or stroke colour space#50
DTW-Thalion wants to merge 1 commit into
gnustep:masterfrom
DTW-Thalion:fix/cgcontext-colorspace-nullcheck

Conversation

@DTW-Thalion

Copy link
Copy Markdown
Contributor

CGContextSetFillColorSpace and CGContextSetStrokeColorSpace allocate a components array and then guard it with an inverted null check:

components = calloc(nc+1, sizeof(CGFloat));
if (components) {                 // fires on success, not failure
  NSLog(@"...: calloc failed");
  return;
}

So on a successful allocation they log "calloc failed" and return without setting anything - the functions never reset the colour - and on an actual allocation failure they would fall through and dereference the null pointer.

Corrected to if (!components). The functions now create the colour space's default opaque, zero-intensity colour and set it, matching CoreGraphics.

Verified against Apple: after SetFillColorSpace (or SetStrokeColorSpace) the current colour is reset to black (0,0,0,255). A regression test is in #49 (marked hopeful there; passes once this is merged).

CGContextSetFillColorSpace and CGContextSetStrokeColorSpace guarded the
components allocation with an inverted null check, so on a successful
allocation they logged calloc failed and returned without setting
anything, and on an actual failure they would have dereferenced the null
pointer.  The colour was therefore never reset to the colour space
default.

Correct the check so the functions build the space's default opaque,
zero-intensity colour and set it.
@DTW-Thalion DTW-Thalion changed the title Reset the colour when setting a fill or stroke colour space Fix: reset the colour when setting a fill or stroke colour space Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant