menu_wrap_offset(), which finds where a byte of a string landed in its
word_wrap() or word_wrap_wideglyph() wrap, moves to stdstring.c beside
them as word_wrap_offset(), and MaterialUI and XMB call it from there.
It gave a glyph the wide-glyph wrap had put a line end before the
offset of that line end, so the input dialog's caret sat at the end
of the line above; it now gives the glyph's own.
word_wrap_offset_test wraps plain, wide-glyph and mixed text at every
width from 1 to 39, with line ends left or made NUL as the drivers
make them, and checks that every character lands on itself or on the
line end its space or newline became, in order, that the end lands
on the end, that no wrap more than doubles its text, and that the
caret before a word moved to the next line is on that line. It found
the wrong offset and the wide-glyph wrap's runaway at narrow widths.
string_percent_decode() is net_http_urldecode() without the networking
dependency, so builds without HAVE_NETWORKING can decode file:// URIs.
net_http_urldecode() and net_http_urldecode_inplace() now call it.
rstrtod/rstrtof are locale-independent, correctly rounded float parsers
in one C89 TU: exact power-of-ten fast path, Eisel-Lemire on a 128-bit
product, big-decimal fallback for the ambiguous leftovers. The same
bytes give the same value in every locale on every thread -- strtod
under a comma-decimal locale silently reads "1.5" as 1 -- and they
run 4-5x ahead of glibc strtod. Verified bit-exact against strtod and
strtof over millions of inputs including full subnormal sweeps, clean
under ASan/UBSan, C89 -pedantic, aarch64.
ryaml is a single-TU MSVC-C89 YAML subset parser (block maps and
sequences, flow lists, quoted scalars, anchors rejected as unsupported
rather than mis-parsed), indexed nodes with no per-node allocation.
It reads PCSX2's GameIndex.yaml in LRPS2, where it replaced rapidyaml
at a fraction of the code size.
Neither is wired into the RetroArch build here; the next commit puts
rstrtod behind the float parses.
ozone_draw_osk and the OSK caret rendering in xmb/materialui all
called word_wrap() with the same buffer as source and destination:
(ozone->word_wrap)(cursor_message, sizeof(cursor_message),
cursor_message, strlen(cursor_message), ...);
word_wrap()/word_wrap_wideglyph() copy via strlcpy(), whose buffers
must not overlap. On macOS, compat/strl.h force-defines HAVE_STRL, so
the system strlcpy is used; with _FORTIFY_SOURCE this resolves to
__strlcpy_chk, which aborts on any overlap (including exact aliasing)
via __chk_fail_overlap. As a result, typing a single character into
any OSK field (e.g. the IP Address setting under SMB Network
Settings) hits word_wrap's short-string early return
`strlcpy(s, src, len)` with s == src and raises EXC_BREAKPOINT.
Crash frame: len=2048, src_len=1, line_width=84, s == src == "1";
video_thread_loop -> ozone_draw_osk -> word_wrap -> __strlcpy_chk ->
__chk_fail_overlap. Linux/Windows never crashed only because the
strlcpy_retro__ fallback tolerates exact aliasing by accident.
In addition, in-place use of word_wrap_wideglyph() silently corrupts
CJK OSK text on all platforms: the lastwideglyph wrap branch
overwrites a source byte with '\n' before rewinding src onto it.
In-place wrapping of the kana string
E3 81 82 .. E3 81 93 (10 glyphs, line_width 4, wideglyph_width 200)
produces glyph 1 + 24 bare newlines + glyph 10 instead of one glyph
per line.
Fix: stage the OSK text in a separate cursor_src buffer at all three
call sites and wrap into the destination, matching the tmpstr pattern
already used for ozone's sidebar metadata. Document the
non-overlapping-buffers requirement for word_wrap()/
word_wrap_wideglyph() in stdstring.{c,h}. All other in-tree callers
(rgui, gfx_widgets, message boxes, sublabels) already use distinct
buffers.
Sanitizer sweep (ASan+UBSan+LSan, gcc, system strlcpy via
-DHAVE_STRL on glibc 2.39):
- fixed caller pattern yields "1" for the crashing input, clean
- separate-buffer output verified byte-exact against the previous
in-place output for ASCII inputs across line widths 5/10/30/84
(no rendering change for existing users)
- libretro-common word_wrap_wideglyph overflow regression tests pass
- ozone.c, xmb.c, materialui.c, stdstring.c compile clean
TSan: not applicable (single-threaded string path, no concurrency
change).
Add strlcpy_append, a bound-checked replacement for the unsafe
idiom that's widespread in this codebase:
_len += strlcpy(s + _len, src, len - _len);
That looks like a self-bounding chain but isn't. strlcpy
returns strlen(source) regardless of how much was actually
written, so on truncation _len overshoots len and the next
(len - _len) underflows to a huge size_t, driving the next
strlcpy into an OOB-write or memcpy of attacker-influenced
size. This is the same primitive that drove the shader-
wildcard fix (commit bac6894).
strlcpy_append takes (s, len, *pos, src), checks the
truncation condition correctly, and on overflow:
- leaves s NUL-terminated at len - 1 (strlcpy's contract)
- clamps *pos to len - 1 so subsequent calls in a chain
short-circuit (also returning -1)
- returns -1 to the caller
The clamp is what makes chained use safe with a single check
at the end of the chain.
Apply to database_info_build_query_enum (database_info.c).
That function builds DB query strings of the shape
{'KEY':"PATH"} (or numeric variants without the quotes,
or {'developer':glob('*PATH*')})
via 22 switch cases, each of which was a hand-unrolled chain
of s[++_len] = '...' character writes followed by a path
strlcpy and another short s[++_len] = '...' chain to close
the brace. None of those writes was bounded against len.
A caller passing a buffer smaller than the prefix length
got OOB-writes from the prefix; a caller whose buffer was
just barely enough for prefix + path got the trailing
s[_len]='"';s[++_len]='}' writes off the end (up to 3 bytes
OOB). All callers in tree pass query[4096] from the menu
displaylist, so practical buffers are large -- but the
pattern is wrong and the path component is attacker-
influenced when scanning content metadata.
Side fix in DATABASE_QUERY_ENTRY_BBFC_RATING: pre-rewrite
the case wrote s[++_len] = '"' after the path strlcpy
instead of s[_len] = '"', leaving the strlcpy's NUL
terminator embedded in the query. BBFC rating searches
have been silently returning nothing since this code was
written. Fixed while rewriting.
Adds libretro-common/samples/string/strlcpy_append/
strlcpy_append_test as a regression test, registered in
.github/workflows/Linux-libretro-common-samples.yml's
RUN_TARGETS allowlist (which builds with ASan + UBSan
default since the cdfs/chd commit). Eight cases covering
the contract: basic append, chained append, exact-fit
boundary, wide-margin truncation, chain short-circuiting
after truncation, empty source, NULL/zero-length args, and
*pos already at or past len.
Test follows the existing libretro-common/samples pattern
(heap-allocated destinations sized to exactly the legal
capacity so any reintroduction of the unbounded chain is
flagged by ASan). Verified to fire under ASan when the
helper is replaced with the unsafe naive idiom: heap-
buffer-overflow WRITE of size 2 at offset 26 of an 8-byte
allocation in test_chain_short_circuits_after_truncation.
With the safe helper all 8 cases pass clean.
Verified database_info_build_query_enum produces identical
output for all 21 enum values at len=4096 (the production
caller's buffer size); also verified that at len=8 the
function returns -1 with no OOB write, which is the
behaviour the unsafe naive replacement would have failed at.
delimiter-separated valid extensions far beyond PATH_MAX_LENGTH bounds.
now use a proper dedup function that makes sure we don't add duplicate
entries to this delimiter-separated list of valid extensions. This way
we can vastly reduce the size of newstr2 in general_push too (now
renamed ext_filters which is more true to what it actually does)
(libretro-common) docs - document when string has to be non-NULL or else UB (undefined behavior)
(libretro-common) stdstring.c - string_hex_to_unsigned - make strlen call unneeded
UTF8 byte codes are commonly stored in signed char types thus these macros could be called with negative numbers. Avoid invalid memory access by clamping the input values to 0 ~ 255 with an unsigned char cast.