Fix DnD memory safety issues, protocol output bugs, and undefined behavior#9869
Merged
kovidgoyal merged 2 commits intomasterfrom Apr 14, 2026
Merged
Fix DnD memory safety issues, protocol output bugs, and undefined behavior#9869kovidgoyal merged 2 commits intomasterfrom
kovidgoyal merged 2 commits intomasterfrom
Conversation
…entation Fixes: - Memory leak: drag_free_remote_item now frees children array - Memory leak: drag_free_offer now frees pending data - Memory leak: toplevel_data_for_drag frees old URI string before replacement - Protocol bug: drag_notify missing OSC prefix in escape code output - Protocol bug: drag_notify missing colon separators between metadata keys - Buffer overflow: add_payload symlink case ensures capacity for null terminator - Integer overflow: expand_rgb_data uses size_t casts for multiplication - Memory leak: realloc anti-pattern fixed in 7 locations (drop_register_window, drop_dispatch_data, drag_add_mimes, drag_add_pre_sent_data, drag_add_image, add_payload default case, queue_payload_to_child) - UB: queue_payload_to_child guards memcpy with NULL source + 0 count Agent-Logs-Url: https://github.com/kovidgoyal/kitty/sessions/3c7e550c-e8e8-413e-a54b-87d61cb8e574 Co-authored-by: kovidgoyal <1308621+kovidgoyal@users.noreply.github.com>
Agent-Logs-Url: https://github.com/kovidgoyal/kitty/sessions/3c7e550c-e8e8-413e-a54b-87d61cb8e574 Co-authored-by: kovidgoyal <1308621+kovidgoyal@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
kovidgoyal
April 14, 2026 12:15
View session
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.
Thorough audit of the DnD protocol implementation (
dnd.c) revealed memory leaks, a buffer overflow, protocol output bugs, and undefined behavior across several code paths.Protocol output bugs
drag_notifymissing OSC prefix — output wast=e:x=1:y=0\x1b\\instead of\x1b]CODE;t=e:x=1:y=0\x1b\\, making notifications unparseable by clientsdrag_notifymissing colon separators — producedt=e:x=1y=0instead oft=e:x=1:y=0Memory leaks
drag_free_remote_item—childrenarray pointer never freed after recursive child cleanupdrag_free_offer—free_pending(&ds.pending)never called, leaking queued unflushed entriestoplevel_data_for_drag— oldstrdup'd URI overwritten withoutfree():drop_register_window,drop_dispatch_data,drag_add_mimes,drag_add_pre_sent_data,drag_add_image,add_payload,queue_payload_to_child) —ptr = realloc(ptr, ...)loses the old pointer on failureBuffer overflow
add_payloadsymlink case —ri->data[ri->data_sz] = 0writes past allocation when decoded data fills capacity exactly. Now ensuresdata_capacity >= data_sz + 1before the null terminator write.Integer overflow
expand_rgb_data—img.width * img.height * 4usedintarithmetic beforesize_tconversion. Fixed with explicit(size_t)casts.Undefined behavior
queue_payload_to_child—memcpy(dst, NULL, 0)whendatais NULL anddata_szis 0. Guarded with size check.Tests
drag_notifycolon separator fix, children array cleanup, and URI replacement. Addeddnd_test_drag_notifyC test helper to invokedrag_notifywith controlled parameters.