Commit Graph
90 Commits
Author SHA1 Message Date
LibretroAdmin 22ba917922 libretro-common: word_wrap_offset(), with a test
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.
2026-11-16 11:49:11 +01:00
LibretroAdmin 247ff38a18 libretro-common: move the percent decoder into stdstring
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.
2026-10-02 11:36:38 +02:00
LibretroAdmin bd79aee4a7 menu: Fix EXC_BREAKPOINT when typing in OSK on macOS (in-place word_wrap)
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).
2026-08-10 16:59:40 +02:00
U-DESKTOP-SPFP6AQ\twistedtechre 78c52ab36e libretro-common/string: add strlcpy_append helper and apply to database_info_build_query_enum
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.
2026-04-28 06:44:59 +02:00
U-DESKTOP-SPFP6AQ\twistedtechre a51455561b core_info_list->all_ext would irresponsibly concatenate every core's
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)
2026-04-12 22:07:01 +02:00
U-DESKTOP-SPFP6AQ\twistedtechre 376bd083f2 Buildfix for strcasecmp not being found for Windows 2026-04-06 09:30:00 +02:00
U-DESKTOP-SPFP6AQ\twistedtechre 0644bf9c3c Add compat/posix_string header include for stdstring.h 2026-04-06 08:55:36 +02:00
U-DESKTOP-SPFP6AQ\twistedtechre 0c8e70343f (menu_settings) Optimizations:
* Remove string_is_empty
* Replace sscanf
* Optimize menu_setting_find_ functions
2026-04-06 08:53:25 +02:00
U-DESKTOP-SPFP6AQ\twistedtechre 69b8e463e4 Faster versions of string_is_equal and string_is_equal_case_insensitive 2026-04-06 08:29:30 +02:00
U-DESKTOP-SPFP6AQ\twistedtechre 9d489a0237 Get rid of more strtok_r calls 2026-03-30 14:52:10 +02:00
U-DESKTOP-SPFP6AQ\twistedtechre f248d3dbc5 string_remove_all_whitespaces - return a length now 2026-03-29 03:33:41 +02:00
libretroadmin 03dbcc53e7 Get rid of superfluous macros around memcmp and strcpy 2025-08-04 19:30:03 +02:00
LibretroAdmin b211adaaca Function argument name standardization 2025-01-17 19:55:32 +01:00
libretroadmin ca7e53e3ca string_replace_substring - move out implicit strlen 2024-12-23 00:40:09 +01:00
Viačasłaŭ 3019b926c2 Fix typos (#17068) 2024-10-01 17:36:33 -07:00
libretroadmin 4b86886d30 (libretro-common) stdstring - get rid of these pointless functions 2024-09-05 11:44:57 +02:00
libretroadmin 24287b1cce Cleanups/simplifications 2023-08-17 21:58:41 +02:00
LibretroAdmin e769436876 (word wrap functions in stdstring.c) Avoid implicit strlens, caller
should pass the 'length' to the function instead
2022-08-23 17:06:01 +02:00
LibretroAdmin ec6f4c1f55 (libretro-common) refinements to documentation 2022-08-01 13:06:35 +02:00
LibretroAdmin c7a1d83675 (libretro-common) Start documenting leaf functon calls in function documentation
(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
2022-08-01 09:52:39 +02:00
Cthulhu-throwaway 98ca6232aa (String) Some function fixes (#14171) 2022-07-12 22:32:58 +02:00
Cthulhu-throwaway f39df40728 (String) Do not assume char is unsigned (#14168) 2022-07-12 02:55:56 +02:00
Tony 044e1ec655 (WinRaw) Fix multiple light guns (#13840)
* (WinRaw) Fix multiple light guns

* Strip non-ASCII characters from mouse display names
2022-04-08 21:46:00 +02:00
pkos a192064403 (Database) Serial scanning for Gamecube/MegaCD/SegaCD/Saturn/PSX/PSP/Dreamcast/Wii 2021-12-31 21:35:16 -05:00
Sage 85a256e2cb Make http header parsing case insensitive (#13267) 2021-11-21 20:43:58 +01:00
toshixm 4fab84ca9e improve message wrapping with CJK languages 2021-06-03 21:09:35 +09:00
toshixm a39c38f267 refactor word_wrap() 2021-05-27 19:55:37 +09:00
Bernhard Schelling c594e8801d Fix ctype style char macros with signed inputs
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.
2020-11-19 23:42:05 +09:00
twinaphex c744baeb89 Add ctype variants to stdstring and use it in RA 2020-09-27 22:15:32 +02:00
twinaphex 1296fc4745 use strlcpy_literal for string literal copies 2020-09-19 23:28:01 +02:00
twinaphex 6a88b3f20e Cleanup strlen_size 2020-08-25 17:24:03 +02:00
jdgleaver 179e77c8b0 (string_list_join_concat) Prevent undefined behaviour if input string buffer is unterminated 2020-08-25 15:54:18 +01:00
Alcaro df2db8bceb stdstring: Delete duplicate function 2020-08-16 02:33:25 +02:00
twinaphex 1ae20ea17c Style nits - turn macro names into upper-case - some while style nits 2020-06-26 20:00:19 +02:00
twinaphex de36ff20fb Create string_ends_with_size 2020-06-25 14:38:06 +02:00
twinaphex 601a73fa8b Use string_starts_with_size where possible - avoids the strlen 2020-06-25 12:51:04 +02:00
jdgleaver d24498bdf1 Image file type detection fixes/optimisations 2020-05-22 16:57:37 +01:00
jdgleaver c346e1f816 Various file path handling optimisations 2020-05-20 17:05:22 +01:00
twinaphex 899c21257c (Video layout) move init_string and set_string to stdstring -
rename them - cleanup strcmp usage in video layout and other
code cleanups
2020-03-05 11:19:44 +01:00
twinaphex cd9ca691ca (libretro-common) Update copyright 2020-01-31 15:43:42 +01:00
jdgleaver 695749f155 (Core Updater) Only download when new core is available + add option to update all installed cores 2019-12-23 17:34:44 +00:00
jdgleaver 170397456c Add manual content scanner 2019-11-29 17:14:46 +00:00
jdgleaver 58ad81f063 Menu drop-down lists: Add proper titles, and remember last postion when navigating core options 2019-09-24 17:41:06 +01:00
twinaphex b94b9e2adb Go back to plain strlcpy/strlcat - probably best to look at
opportunities to reduce these calls vs. replacing them with
unsafe macros
2019-09-23 00:19:01 +02:00
LazyBumHorse f309c85f92 STRLCAT_CONST_INCR should be at most 1 statement, so single-line if, for etc work 2019-09-22 20:12:56 +02:00
twinaphex 3b057d9605 Start using STRLCAT_CONST_INCR and STRLCPY_CONST 2019-09-20 23:33:17 +02:00
twinaphex 76f4cf5cc5 Start using STRLCAT_CONST/STRLCAT_CONST_INCR/STRLCPY_CONST 2019-09-20 20:39:34 +02:00
twinaphex db624a7c99 Start cleaning up the rather messy manual assignment 2019-09-20 02:03:26 +02:00
twinaphex 698387e762 (task_screenshot.c) Cleanups - don't use snprintf unless necessary 2019-09-18 02:31:02 +02:00
twinaphex 8fa6230426 Cleanups 2019-09-18 02:16:47 +02:00