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)
Mon, Dec 15, 6:11 PM
Unknown Object (File)
Sun, Dec 14, 8:32 PM
Unknown Object (File)
Fri, Nov 21, 4:34 AM
Unknown Object (File)
Nov 19 2025, 9:24 AM
Unknown Object (File)
Nov 18 2025, 3:03 AM
Unknown Object (File)
Nov 17 2025, 7:19 PM
Unknown Object (File)
Nov 16 2025, 4:18 PM
Unknown Object (File)
Nov 10 2025, 8:57 PM
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