diff options
author | schwarze@openbsd.org <schwarze@openbsd.org> | 2016-05-30 12:05:56 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2016-06-08 11:45:05 +1000 |
commit | 75f0844b4f29d62ec3a5e166d2ee94b02df819fc (patch) | |
tree | c58869d41e11844e2e86358b6c7cdb5a235c64f7 | |
parent | 016881eb33a7948028848c90f4c7ac42e3af0e87 (diff) |
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.c | 21 |
1 files changed, 20 insertions, 1 deletions
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 @@ | |||
1 | /* $OpenBSD: tests.c,v 1.1 2016/05/26 19:14:25 schwarze Exp $ */ | 1 | /* $OpenBSD: tests.c,v 1.2 2016/05/30 12:05:56 schwarze Exp $ */ |
2 | /* | 2 | /* |
3 | * Regress test for the utf8.h *mprintf() API | 3 | * Regress test for the utf8.h *mprintf() API |
4 | * | 4 | * |
@@ -13,9 +13,25 @@ | |||
13 | 13 | ||
14 | #include "utf8.h" | 14 | #include "utf8.h" |
15 | 15 | ||
16 | void badarg(void); | ||
16 | void one(const char *, const char *, int, int, int, const char *); | 17 | void one(const char *, const char *, int, int, int, const char *); |
17 | 18 | ||
18 | void | 19 | void |
20 | badarg(void) | ||
21 | { | ||
22 | char buf[16]; | ||
23 | int len, width; | ||
24 | |||
25 | width = 1; | ||
26 | TEST_START("utf8_badarg"); | ||
27 | len = snmprintf(buf, sizeof(buf), &width, "\377"); | ||
28 | ASSERT_INT_EQ(len, -1); | ||
29 | ASSERT_STRING_EQ(buf, ""); | ||
30 | ASSERT_INT_EQ(width, 0); | ||
31 | TEST_DONE(); | ||
32 | } | ||
33 | |||
34 | void | ||
19 | one(const char *name, const char *mbs, int width, | 35 | one(const char *name, const char *mbs, int width, |
20 | int wantwidth, int wantlen, const char *wants) | 36 | int wantwidth, int wantlen, const char *wants) |
21 | { | 37 | { |
@@ -46,6 +62,9 @@ tests(void) | |||
46 | ASSERT_PTR_NE(loc, NULL); | 62 | ASSERT_PTR_NE(loc, NULL); |
47 | TEST_DONE(); | 63 | TEST_DONE(); |
48 | 64 | ||
65 | badarg(); | ||
66 | one("null", NULL, 8, 6, 6, "(null)"); | ||
67 | one("empty", "", 2, 0, 0, ""); | ||
49 | one("ascii", "x", -2, -2, -2, "x"); | 68 | one("ascii", "x", -2, -2, -2, "x"); |
50 | one("newline", "a\nb", -2, -2, -2, "a\nb"); | 69 | one("newline", "a\nb", -2, -2, -2, "a\nb"); |
51 | one("cr", "a\rb", -2, -2, -2, "a\rb"); | 70 | one("cr", "a\rb", -2, -2, -2, "a\rb"); |