Fix use-after-free SEGV on RS274-X export (#162)#379
Merged
spe-ciellt merged 1 commit intogerbv:developfrom Mar 7, 2026
Merged
Fix use-after-free SEGV on RS274-X export (#162)#379spe-ciellt merged 1 commit intogerbv:developfrom
spe-ciellt merged 1 commit intogerbv:developfrom
Conversation
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.
This was referenced Mar 7, 2026
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
Fixes #162 — use-after-free crash when exporting a file as RS274-X.
gerbv_image_duplicate_layer()andgerbv_image_duplicate_state()shallow-copy their input structs, which retains the original'snextpointer. When the original image is freed during export the duplicate'snextbecomes dangling, and freeing the duplicate follows it into freed memory.The fix NULLs
nextafter the shallow copy in both functions. Callers ingerbv_image_duplicate_image()already build their own linked-list chain vialastLayer->next = gerbv_image_duplicate_layer(...), so the duplicates'nextpointers are always overwritten — except for the last element, which must be NULL.Changes
src/gerb_image.c: AddnewLayer->next = NULLingerbv_image_duplicate_layer()src/gerb_image.c: AddnewState->next = NULLingerbv_image_duplicate_state()Test plan
cmake --preset linux-gnu-gcc && cmake --build buildgerbv --export=rs274x --output=out.grb input.grbno longer crashes (reproducer from SEGV on export file as RS274-X #162)