Loading...
tests/netbsd_vis.c Libc-1534.40.2 Libc-1583.60.2
--- Libc/Libc-1534.40.2/tests/netbsd_vis.c
+++ Libc/Libc-1583.60.2/tests/netbsd_vis.c
@@ -34,6 +34,9 @@
 #include <vis.h>
 
 #include <darwintest.h>
+#include <locale.h>
+
+#include <TargetConditionals.h>
 
 static int styles[] = {
 	VIS_OCTAL,
@@ -99,6 +102,25 @@
 	T_ASSERT_EQ(dst[1], 'a', NULL);
 }
 
+#if TARGET_OS_OSX
+/* rdar://problem/108684957 - multibyte spaces should also be passed through */
+T_DECL(strvis_cstyle_mbspace, "strvis(3) multibyte space")
+{
+	const char str[] = "\xe2\x80\xaf";
+	char dst[(sizeof(str) - 1) * 4];
+	int ret;
+
+	(void)setlocale(LC_CTYPE, "en_US.UTF-8");
+
+	ret = strsnvisx(dst, sizeof(dst), str, sizeof(str) - 1,
+	    VIS_CSTYLE | VIS_NOSLASH, "\\\"\b\f\n\r\t");
+	T_ASSERT_EQ(ret, sizeof(str) - 1, NULL);
+	T_ASSERT_EQ_STR(dst, str, NULL);
+
+	(void)setlocale(LC_CTYPE, "C");
+}
+#endif
+
 T_DECL(strunvis_hex, "strunvis(3) \\Xxx")
 {
 	static const struct {
@@ -120,3 +142,53 @@
 			T_ASSERT_EQ(memcmp(ed[i].d, uv, (unsigned long)ed[i].error), 0, NULL);
 	}
 }
+
+#define	STRVIS_OVERFLOW_MARKER	0xff	/* Arbitrary */
+
+#if TARGET_OS_OSX
+T_DECL(strvis_overflow_mb, "Test strvis(3) multi-byte overflow")
+{
+	const char src[] = "\xf0\x9f\xa5\x91";
+	/* Extra byte to detect overflow */
+	char dst[sizeof(src) + 1];
+	int n;
+
+	setlocale(LC_CTYPE, "en_US.UTF-8");
+
+	/* Arbitrary */
+	memset(dst, STRVIS_OVERFLOW_MARKER, sizeof(dst));
+
+	/*
+	 * If we only provide four bytes of buffer, we shouldn't be able encode
+	 * a full 4-byte sequence.
+	 */
+	n = strnvis(dst, 4, src, VIS_SAFE);
+	T_ASSERT_EQ_CHAR((unsigned char)dst[4], STRVIS_OVERFLOW_MARKER, NULL);
+	T_ASSERT_EQ(n, -1, NULL);
+
+	n = strnvis(dst, sizeof(src), src, VIS_SAFE);
+	T_ASSERT_EQ(n, sizeof(src) - 1, NULL);
+}
+#endif
+
+T_DECL(strvis_overflow_c, "Test strvis(3) C locale overflow")
+{
+	const char src[] = "AAAA";
+	/* Extra byte to detect overflow */
+	char dst[sizeof(src) + 1];
+	int n;
+
+	/* Arbitrary */
+	memset(dst, STRVIS_OVERFLOW_MARKER, sizeof(dst));
+
+	/*
+	 * If we only provide four bytes of buffer, we shouldn't be able encode
+	 * 4 bytes of input.
+	 */
+	n = strnvis(dst, 4, src, VIS_SAFE | VIS_NOLOCALE);
+	T_ASSERT_EQ_CHAR((unsigned char)dst[4], STRVIS_OVERFLOW_MARKER, NULL);
+	T_ASSERT_EQ(n, -1, NULL);
+
+	n = strnvis(dst, sizeof(src), src, VIS_SAFE | VIS_NOLOCALE);
+	T_ASSERT_EQ(n, sizeof(src) - 1, NULL);
+}