summaryrefslogtreecommitdiff
path: root/openbsd-compat/regress/snprintftest.c
diff options
context:
space:
mode:
authorDarren Tucker <dtucker@zip.com.au>2006-02-19 22:50:20 +1100
committerDarren Tucker <dtucker@zip.com.au>2006-02-19 22:50:20 +1100
commit4881c371cef5e3c1065d7dfa314fffc3a5ab0256 (patch)
treeaad34a5efc70e601b688ae83c7ebdfd01792297d /openbsd-compat/regress/snprintftest.c
parentbf209f590108a12a9758dcfcdf4297a3d593ce87 (diff)
- (dtucker) [Makefile.in configure.ac, added openbsd-compat/regress/]
Add first attempt at regress tests for compat library. ok djm@
Diffstat (limited to 'openbsd-compat/regress/snprintftest.c')
-rw-r--r--openbsd-compat/regress/snprintftest.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/openbsd-compat/regress/snprintftest.c b/openbsd-compat/regress/snprintftest.c
new file mode 100644
index 000000000..e25bf223f
--- /dev/null
+++ b/openbsd-compat/regress/snprintftest.c
@@ -0,0 +1,72 @@
1/*
2 * Copyright (c) 2005 Darren Tucker
3 * Copyright (c) 2005 Damien Miller
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#define BUFSZ 2048
19
20#include <sys/types.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <stdarg.h>
24
25static int failed = 0;
26
27static void
28fail(const char *m)
29{
30 fprintf(stderr, "%s", m);
31 failed = 1;
32}
33
34int x_snprintf(char *str, size_t count, const char *fmt, ...)
35{
36 size_t ret;
37 va_list ap;
38
39 va_start(ap, fmt);
40 ret = vsnprintf(str, count, fmt, ap);
41 va_end(ap);
42 return ret;
43}
44
45int
46main(void)
47{
48 char b[5];
49 char *src;
50
51 snprintf(b,5,"123456789");
52 if (b[4] != '\0')
53 fail("snprintf does not correctly terminate long strings");
54
55 /* check for read overrun on unterminated string */
56 if ((src = malloc(BUFSZ)) == NULL) {
57 fail("malloc failed");
58 } else {
59 memset(src, 'a', BUFSZ);
60 snprintf(b, sizeof(b), "%.*s", 1, src);
61 if (strcmp(b, "a") != 0)
62 fail("failed with length limit '%%.s'");
63 }
64
65 /* check that snprintf and vsnprintf return sane values */
66 if (snprintf(b, 1, "%s %d", "hello", 12345) != 11)
67 fail("snprintf does not return required length");
68 if (x_snprintf(b, 1, "%s %d", "hello", 12345) != 11)
69 fail("vsnprintf does not return required length");
70
71 return failed;
72}