From 016881eb33a7948028848c90f4c7ac42e3af0e87 Mon Sep 17 00:00:00 2001 From: "schwarze@openbsd.org" Date: Thu, 26 May 2016 19:14:25 +0000 Subject: upstream commit test the new utf8 module Upstream-Regress-ID: c923d05a20e84e4ef152cbec947fdc4ce6eabbe3 --- regress/unittests/utf8/tests.c | 63 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 regress/unittests/utf8/tests.c (limited to 'regress/unittests/utf8/tests.c') diff --git a/regress/unittests/utf8/tests.c b/regress/unittests/utf8/tests.c new file mode 100644 index 000000000..d18cadc5d --- /dev/null +++ b/regress/unittests/utf8/tests.c @@ -0,0 +1,63 @@ +/* $OpenBSD: tests.c,v 1.1 2016/05/26 19:14:25 schwarze Exp $ */ +/* + * Regress test for the utf8.h *mprintf() API + * + * Written by Ingo Schwarze in 2016 + * and placed in the public domain. + */ + +#include +#include + +#include "test_helper.h" + +#include "utf8.h" + +void one(const char *, const char *, int, int, int, const char *); + +void +one(const char *name, const char *mbs, int width, + int wantwidth, int wantlen, const char *wants) +{ + char buf[16]; + int *wp; + int len; + + if (wantlen == -2) + wantlen = strlen(wants); + (void)strlcpy(buf, "utf8_", sizeof(buf)); + (void)strlcat(buf, name, sizeof(buf)); + TEST_START(buf); + wp = wantwidth == -2 ? NULL : &width; + len = snmprintf(buf, sizeof(buf), wp, "%s", mbs); + ASSERT_INT_EQ(len, wantlen); + ASSERT_STRING_EQ(buf, wants); + ASSERT_INT_EQ(width, wantwidth); + TEST_DONE(); +} + +void +tests(void) +{ + char *loc; + + TEST_START("utf8_setlocale"); + loc = setlocale(LC_CTYPE, "en_US.UTF-8"); + ASSERT_PTR_NE(loc, NULL); + TEST_DONE(); + + one("ascii", "x", -2, -2, -2, "x"); + one("newline", "a\nb", -2, -2, -2, "a\nb"); + one("cr", "a\rb", -2, -2, -2, "a\rb"); + one("tab", "a\tb", -2, -2, -2, "a\tb"); + one("esc", "\033x", -2, -2, -2, "\\033x"); + one("inv_badbyte", "\377x", -2, -2, -2, "\\377x"); + one("inv_nocont", "\341x", -2, -2, -2, "\\341x"); + one("inv_nolead", "a\200b", -2, -2, -2, "a\\200b"); + one("sz_ascii", "1234567890123456", -2, -2, 16, "123456789012345"); + one("sz_esc", "123456789012\033", -2, -2, 16, "123456789012"); + one("width_ascii", "123", 2, 2, -1, "12"); + one("width_double", "a\343\201\201", 2, 1, -1, "a"); + one("double_fit", "a\343\201\201", 3, 3, 4, "a\343\201\201"); + one("double_spc", "a\343\201\201", 4, 3, 4, "a\343\201\201"); +} -- cgit v1.2.3 From 75f0844b4f29d62ec3a5e166d2ee94b02df819fc Mon Sep 17 00:00:00 2001 From: "schwarze@openbsd.org" Date: Mon, 30 May 2016 12:05:56 +0000 Subject: upstream commit Fix two rare edge cases: 1. If vasprintf() returns < 0, do not access a NULL pointer in snmprintf(), and do not free() the pointer returned from vasprintf() because on some systems other than OpenBSD, it might be a bogus pointer. 2. If vasprintf() returns == 0, return 0 and "" rather than -1 and NULL. Besides, free(dst) is pointless after failure (not a bug). One half OK martijn@, the other half OK deraadt@; committing quickly before people get hurt. Upstream-Regress-ID: b164f20923812c9bac69856dbc1385eb1522cba4 --- regress/unittests/utf8/tests.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'regress/unittests/utf8/tests.c') diff --git a/regress/unittests/utf8/tests.c b/regress/unittests/utf8/tests.c index d18cadc5d..fad2ec279 100644 --- a/regress/unittests/utf8/tests.c +++ b/regress/unittests/utf8/tests.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tests.c,v 1.1 2016/05/26 19:14:25 schwarze Exp $ */ +/* $OpenBSD: tests.c,v 1.2 2016/05/30 12:05:56 schwarze Exp $ */ /* * Regress test for the utf8.h *mprintf() API * @@ -13,8 +13,24 @@ #include "utf8.h" +void badarg(void); void one(const char *, const char *, int, int, int, const char *); +void +badarg(void) +{ + char buf[16]; + int len, width; + + width = 1; + TEST_START("utf8_badarg"); + len = snmprintf(buf, sizeof(buf), &width, "\377"); + ASSERT_INT_EQ(len, -1); + ASSERT_STRING_EQ(buf, ""); + ASSERT_INT_EQ(width, 0); + TEST_DONE(); +} + void one(const char *name, const char *mbs, int width, int wantwidth, int wantlen, const char *wants) @@ -46,6 +62,9 @@ tests(void) ASSERT_PTR_NE(loc, NULL); TEST_DONE(); + badarg(); + one("null", NULL, 8, 6, 6, "(null)"); + one("empty", "", 2, 0, 0, ""); one("ascii", "x", -2, -2, -2, "x"); one("newline", "a\nb", -2, -2, -2, "a\nb"); one("cr", "a\rb", -2, -2, -2, "a\rb"); -- cgit v1.2.3