| /* |
| * Copyright (c) 2005 Darren Tucker |
| * Copyright (c) 2005 Damien Miller |
| * |
| * Permission to use, copy, modify, and distribute this software for any |
| * purpose with or without fee is hereby granted, provided that the above |
| * copyright notice and this permission notice appear in all copies. |
| * |
| * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| */ |
| |
| #define BUFSZ 2048 |
| |
| #include <sys/types.h> |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <stdarg.h> |
| #include <string.h> |
| |
| static int failed = 0; |
| |
| static void |
| fail(const char *m) |
| { |
| fprintf(stderr, "%s", m); |
| failed = 1; |
| } |
| |
| int x_snprintf(char *str, size_t count, const char *fmt, ...) |
| { |
| size_t ret; |
| va_list ap; |
| |
| va_start(ap, fmt); |
| ret = vsnprintf(str, count, fmt, ap); |
| va_end(ap); |
| return ret; |
| } |
| |
| int |
| main(void) |
| { |
| char b[5]; |
| char *src; |
| |
| snprintf(b,5,"123456789"); |
| if (b[4] != '\0') |
| fail("snprintf does not correctly terminate long strings"); |
| |
| /* check for read overrun on unterminated string */ |
| if ((src = malloc(BUFSZ)) == NULL) { |
| fail("malloc failed"); |
| } else { |
| memset(src, 'a', BUFSZ); |
| snprintf(b, sizeof(b), "%.*s", 1, src); |
| if (strcmp(b, "a") != 0) |
| fail("failed with length limit '%%.s'"); |
| } |
| |
| /* check that snprintf and vsnprintf return sane values */ |
| if (snprintf(b, 1, "%s %d", "hello", 12345) != 11) |
| fail("snprintf does not return required length"); |
| if (x_snprintf(b, 1, "%s %d", "hello", 12345) != 11) |
| fail("vsnprintf does not return required length"); |
| |
| return failed; |
| } |