diff options
-rw-r--r-- | regress/usr.bin/ssh/unittests/utf8/tests.c | 21 | ||||
-rw-r--r-- | usr.bin/ssh/utf8.c | 28 |
2 files changed, 38 insertions, 11 deletions
diff --git a/regress/usr.bin/ssh/unittests/utf8/tests.c b/regress/usr.bin/ssh/unittests/utf8/tests.c index d18cadc5d52..fad2ec27944 100644 --- a/regress/usr.bin/ssh/unittests/utf8/tests.c +++ b/regress/usr.bin/ssh/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,9 +13,25 @@ #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"); diff --git a/usr.bin/ssh/utf8.c b/usr.bin/ssh/utf8.c index d6089bdecf5..caf789cee65 100644 --- a/usr.bin/ssh/utf8.c +++ b/usr.bin/ssh/utf8.c @@ -1,4 +1,4 @@ -/* $OpenBSD: utf8.c,v 1.1 2016/05/25 23:48:45 schwarze Exp $ */ +/* $OpenBSD: utf8.c,v 1.2 2016/05/30 12:05:56 schwarze Exp $ */ /* * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> * @@ -81,13 +81,15 @@ vasnmprintf(char **str, size_t maxsz, int *wp, const char *fmt, va_list ap) int width; /* Display width of the character wc. */ int total_width, max_width, print; - src = dst = NULL; - if (vasprintf(&src, fmt, ap) <= 0) + src = NULL; + if ((ret = vasprintf(&src, fmt, ap)) <= 0) goto fail; sz = strlen(src); - if ((dst = malloc(sz)) == NULL) + if ((dst = malloc(sz)) == NULL) { + free(src); goto fail; + } if (maxsz > INT_MAX) maxsz = INT_MAX; @@ -191,12 +193,15 @@ vasnmprintf(char **str, size_t maxsz, int *wp, const char *fmt, va_list ap) return ret; fail: - free(src); - free(dst); - *str = NULL; if (wp != NULL) *wp = 0; - return -1; + if (ret == 0) { + *str = src; + return 0; + } else { + *str = NULL; + return -1; + } } int @@ -209,8 +214,11 @@ snmprintf(char *str, size_t sz, int *wp, const char *fmt, ...) va_start(ap, fmt); ret = vasnmprintf(&cp, sz, wp, fmt, ap); va_end(ap); - (void)strlcpy(str, cp, sz); - free(cp); + if (cp != NULL) { + (void)strlcpy(str, cp, sz); + free(cp); + } else + *str = '\0'; return ret; } |