Page MenuHomeFreeBSD

libc: fix some overflow scenarios in vis(3)
ClosedPublic

Authored by kevans on Aug 4 2023, 8:47 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mar 4 2025, 7:42 PM
Unknown Object (File)
Mar 1 2025, 12:30 PM
Unknown Object (File)
Feb 14 2025, 6:02 PM
Unknown Object (File)
Jan 19 2025, 10:45 PM
Unknown Object (File)
Dec 28 2024, 2:06 AM
Unknown Object (File)
Dec 18 2024, 3:29 PM
Unknown Object (File)
Nov 22 2024, 4:45 AM
Unknown Object (File)
Nov 21 2024, 10:53 AM
Subscribers

Details

Summary

The previous incarnation of this would call wcrtomb() on the destination
buffer, and only check for overflow *after* it's happened.
Additionally, the conversion error / VIS_NOLOCALE path also didn't check
for overflow, and the overflow check at the end didn't account for the
fact that we still need to write a NUL terminator afterward.

Start by only doing the multibyte conversion into mbdst directly if we
have enough buffer space to guarantee it'll fit. An additional
MB_CUR_MAX buffer has been stashed on the stack to write into if we're
cutting it close at the end of the buffer, since we don't really have a
good way to determine the length of the wchar_t without just doing the
conversion. We'll do the conversion into the buffer that's guaranteed
to fit, then copy it over if the copy won't overflow.

The byte-for-byte overflow is a little bit easier, as we simply check
for overflow with each byte written and make sure we can still NUL
terminate after.

Tests added to exercise these edge cases.

Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable