summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschwarze@openbsd.org <schwarze@openbsd.org>2016-05-30 12:05:56 +0000
committerDamien Miller <djm@mindrot.org>2016-06-08 11:45:05 +1000
commit75f0844b4f29d62ec3a5e166d2ee94b02df819fc (patch)
treec58869d41e11844e2e86358b6c7cdb5a235c64f7
parent016881eb33a7948028848c90f4c7ac42e3af0e87 (diff)
downloadopenssh-git-75f0844b4f29d62ec3a5e166d2ee94b02df819fc.tar.gz
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
-rw-r--r--regress/unittests/utf8/tests.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/regress/unittests/utf8/tests.c b/regress/unittests/utf8/tests.c
index d18cadc5..fad2ec27 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,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");