Skip to content

Fix use-after-free SEGV on RS274-X export (#162)#379

Merged
spe-ciellt merged 1 commit intogerbv:developfrom
SourceParts:fix/rs274x-export-segv
Mar 7, 2026
Merged

Fix use-after-free SEGV on RS274-X export (#162)#379
spe-ciellt merged 1 commit intogerbv:developfrom
SourceParts:fix/rs274x-export-segv

Conversation

@rampageservices
Copy link
Copy Markdown
Contributor

Summary

Fixes #162 — use-after-free crash when exporting a file as RS274-X.

gerbv_image_duplicate_layer() and gerbv_image_duplicate_state() shallow-copy their input structs, which retains the original's next pointer. When the original image is freed during export the duplicate's next becomes dangling, and freeing the duplicate follows it into freed memory.

The fix NULLs next after the shallow copy in both functions. Callers in gerbv_image_duplicate_image() already build their own linked-list chain via lastLayer->next = gerbv_image_duplicate_layer(...), so the duplicates' next pointers are always overwritten — except for the last element, which must be NULL.

Changes

  • src/gerb_image.c: Add newLayer->next = NULL in gerbv_image_duplicate_layer()
  • src/gerb_image.c: Add newState->next = NULL in gerbv_image_duplicate_state()

Test plan

  • Builds cleanly with cmake --preset linux-gnu-gcc && cmake --build build
  • Existing CI regression tests pass
  • gerbv --export=rs274x --output=out.grb input.grb no longer crashes (reproducer from SEGV on export file as RS274-X #162)

gerbv_image_duplicate_layer() and gerbv_image_duplicate_state() do a
shallow struct copy which retains the original's next pointer. When the
original image is freed during export, the duplicate's next pointer
becomes dangling. Freeing the duplicate then follows the dangling
pointer and crashes.

NULL the next pointer after the shallow copy in both functions. Callers
in gerbv_image_duplicate_image() already wire up their own next chain,
so the only element affected is the last in each list, which must be
NULL anyway.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Solution for a potential problem or omission.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SEGV on export file as RS274-X

2 participants