When growing the buffer, realloc was passed the element count instead of
the byte count, shrinking the buffer while capacity was set to the larger
value. Lines with more than 4096 codepoints (e.g. many combining chars)
would then write past the end of the allocation.
Fix dangling text pointer in buffered key events and test CSI param overflow
Key events buffered while waiting for an overlay to become ready stored
the text pointer from the GLFWkeyevent, which is only valid for the
duration of the key callback. Make the buffered events own a copy of
the text and free it in a single place.
Also add a test for the out of bounds write of is_sub_param when a
separator follows the last allowed CSI parameter.
A line scrolled out of a region whose top is row 0 (tmux with a bottom status line) enters the history, but scroll_filter_margins_func left the refs of history lines in place, so they drew over other lines on the next scroll back. A reverse scroll also moved history refs although the history does not move.
A full screen reverse scroll (ESC M at the top, CSI T) moves every image
ref down a row. scroll_filter_func() only removed refs that left through
the top, so a cell image on the bottom line ended up at row `lines`, off
screen, while its line was discarded. The next forward scroll brought it
back onto the bottom row, where the line has no placeholders.
screen_render_line_graphics() returns early for such lines, so nothing
removed the ref and a slice of the old image was drawn over the new text.
tmux and pagers send ESC M when scrolling down, so this shows up in
practice. Fix it by dropping cell image refs that move below the bottom
margin, the same way the margins scroll path drops refs that leave the
region.
This replaces PR #10623, which was not merged for these reasons:
- It does not find the cause. It removes cell image refs at render time
for every line without placeholders, so the stale ref still exists and
is still seen by anything that reads refs before the next render, such
as graphics query and delete commands and the paused rendering path,
which returns before the new code.
- It costs time on every frame. Whenever any image is loaded, even with
no placeholders at all, each clean visible line and each visible
history line triggers a walk over all images and refs. Before, such
lines cost nothing. This fix adds one comparison to a loop that only
runs when the screen scrolls.
Close#10623
Fix problems found while reviewing the EINTR handling changes to
safe-wrappers.h merged from PR #10615.
- safe_connect: the PR waited for POLLOUT and read SO_ERROR after an
EINTR from connect(). That only works for sockets where an interrupted
connect continues in the background, such as TCP. AF_UNIX stream
sockets on Linux are left unconnected but writable, so safe_connect
returned success on an unconnected socket. This broke the single
instance launcher when the server's listen backlog was full. Now
connect() is retried after EINTR, EISCONN on a retry is treated as
success, and poll()+SO_ERROR is used only on EALREADY. While waiting,
SO_SNDTIMEO is honored (with a monotonic deadline across EINTR from
poll) instead of polling with an infinite timeout. The EAGAIN retry
the PR dropped is restored.
- safe_mkstemp: rather than copying the template into a PATH_MAX buffer,
which rejected templates longer than 1024 bytes on macOS/BSD, restore
only the trailing X characters when mkostemp() is interrupted. Use
mkostemp(O_CLOEXEC) on all platforms. macOS has had it since 10.12 and
disk-cache.c already relied on it. This removes the duplicated
platform #if blocks and the racy mkstemp+fcntl(FD_CLOEXEC) fallback.
- disk-cache: open_cache_file_without_tmpfile had its own mkostemp EINTR
loop with the same template corruption bug. Use safe_mkstemp instead.
- Add safe_write_all() and use it in place of the hand-written write
loops in dnd.c and the single instance launcher. Also fix
finish_remote_data() in dnd.c, which passed -1 instead of errno to
abrt() when writing the uri list failed.
- safe_mkstemp: fix template corruption and atomicity
mkstemp mutates the template in place, causing subsequent retries on
EINTR to fail with EINVAL. Save the template before invocation and use
atomic mkostemp(..., O_CLOEXEC) on supported platforms.
- safe_connect and safe_accept: fix socket connection state machine
- safe_connect: Retrying connect() on EINTR violates stream socket semantics
(yields EISCONN/EALREADY). Poll for POLLOUT and query SO_ERROR instead.
- safe_accept: Remove EAGAIN busy-wait loop to avoid 100% CPU spin on
non-blocking sockets or when timeouts expire.
- safe_write: prevent stream corruption
Comparing ret != 0 caused successful writes (> 0) to repeat whenever
stale EINTR remained in errno, duplicating payload data across PTY and
socket streams. Change check to ret < 0.
- safe_close: eliminate descriptor reuse race
POSIX-compliant modern kernels (Linux, macOS, BSDs) detach and release
file descriptors before handling signal interruptions. Looping close() on
EINTR risks closing an unrelated file descriptor allocated concurrently
by another thread.
The scale between scaled and unscaled offsets was only calculated for
positive deltas, so when scrolling up or left it stayed at zero (or stale from
a previous scroll), causing momentum events to have zero offsets.
Closes#10610