2006-02-19 11:50:20 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2021-08-10 23:21:09 +00:00
|
|
|
#include "includes.h"
|
|
|
|
|
2006-02-19 11:50:20 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2006-08-05 03:27:29 +00:00
|
|
|
#include <string.h>
|
2006-02-19 11:50:20 +00:00
|
|
|
|
|
|
|
static int failed = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
fail(const char *m)
|
|
|
|
{
|
2006-08-18 10:09:32 +00:00
|
|
|
fprintf(stderr, "snprintftest: %s\n", m);
|
2006-02-19 11:50:20 +00:00
|
|
|
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];
|
2019-08-05 11:36:48 +00:00
|
|
|
char *src = NULL;
|
2006-02-19 11:50:20 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
|
2019-08-05 11:36:48 +00:00
|
|
|
free(src);
|
2006-02-19 11:50:20 +00:00
|
|
|
return failed;
|
|
|
|
}
|