diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-09-06 16:23:10 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-09-06 16:35:39 -0400 |
commit | 90f9205b40b2f2049df46d819d14d67bfcb055be (patch) | |
tree | a2829130307c85d74b3c83562cfd20a15f3168f3 /tests | |
parent | 377709664480a30fa5acdd11c7ca8c16669678ce (diff) | |
download | bfs-90f9205b40b2f2049df46d819d14d67bfcb055be.tar.xz |
bfstd: Fix printable_len() off-by-one
If xmbrtowc() fails, or if xiswprint() is false, then we shouldn't
include that wide char in the printable length.
Fixes: 19c96abe0a1ee56cf206fd5e87defb1fd3e0daa5
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bfstd.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/bfstd.c b/tests/bfstd.c index 2db084a..83964e5 100644 --- a/tests/bfstd.c +++ b/tests/bfstd.c @@ -5,6 +5,8 @@ #include "../src/config.h" #include "../src/diag.h" #include <errno.h> +#include <langinfo.h> +#include <locale.h> #include <stdio.h> #include <stdint.h> #include <stdlib.h> @@ -33,6 +35,11 @@ static void check_wordesc(const char *str, const char *exp, enum wesc_flags flag } int main(void) { + // Try to set a UTF-8 locale + if (!setlocale(LC_ALL, "C.UTF-8")) { + setlocale(LC_ALL, ""); + } + // From man 3p basename check_base_dir("usr", ".", "usr"); check_base_dir("usr/", ".", "usr"); @@ -54,5 +61,13 @@ int main(void) { check_wordesc("\033[1mbold's\033[0m", "$'\\e[1mbold\\'s\\e[0m'", WESC_SHELL | WESC_TTY); check_wordesc("\x7F", "$'\\x7F'", WESC_SHELL | WESC_TTY); + const char *charmap = nl_langinfo(CODESET); + if (strcmp(charmap, "UTF-8") == 0) { + check_wordesc("\xF0", "$'\\xF0'", WESC_SHELL | WESC_TTY); + check_wordesc("\xF0\x9F", "$'\\xF0\\x9F'", WESC_SHELL | WESC_TTY); + check_wordesc("\xF0\x9F\x98", "$'\\xF0\\x9F\\x98'", WESC_SHELL | WESC_TTY); + check_wordesc("\xF0\x9F\x98\x80", "\xF0\x9F\x98\x80", WESC_SHELL | WESC_TTY); + } + return EXIT_SUCCESS; } |