1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
/* Test of sprintf(), invalid format.
$Id: sprintf-inv.c,v 1.2 2007/03/11 03:37:00 dmix Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "progmem.h"
#ifndef __AVR__
# define sprintf_P sprintf
#endif
void Check (int line,
int explen,
const char *expstr,
int retval, const char *retstr)
{
int code;
if (retval != explen)
code = 1000 + line;
else if (strcmp_P (retstr, expstr))
code = line;
else
return;
#if !defined(__AVR__)
if (code) {
printf ("\nLine %3d: expect: %3d, \"%s\","
"\n output: %3d, \"%s\"\n",
line, explen, expstr, retval, retstr);
code = (line < 256 ? line : 255); /* common OS restriction */
}
#elif defined(DEBUG)
exit ((int)retstr);
#endif
exit (code);
}
/* 'vp' is used to avoid gcc warnings about format string. */
#define CHECK(explen, expstr, fmt, ...) do { \
char s[260]; \
int i; \
int (* volatile vp)(char *, const char *, ...) = sprintf_P; \
memset (s, 0, sizeof(s)); \
i = vp (s, PSTR(fmt), ##__VA_ARGS__); \
Check (__LINE__, explen, PSTR(expstr), i, s); \
} while (0)
int main ()
{
/* Format last symbol is absent */
#ifdef __AVR__ /* Glibc return -1, if '%' has not a format spec. */
CHECK (0, "", "%");
CHECK (1, ".", ".%");
CHECK (1, ".", ".%0");
CHECK (6, "12345 ", "%d %+l", 12345);
#endif
/* 'hh' length modifier is possible, but it is ignored in avr-libc */
CHECK (3, "123", "%hhd", 123);
/* Length modifier and conversion mismatch: ignored */
CHECK (1, "a", "%hhc", 'a');
CHECK (1, "b", "%hc", 'b');
CHECK (3, "foo", "%hhs", "foo");
CHECK (3, "foo", "%hs", "foo");
/* If the `space' and `+' flags both appear, the `space' flag
is ignored. */
CHECK (7, "+10.+11", "% +d.%+ d", 10, 11);
#ifdef __AVR__
/* Unknown format: glibc output is "%b", return value 2. */
CHECK (0, "", "%b");
/* Two precision dots. */
CHECK (0, "", "%8..4d", 1);
/* Long long arg */
CHECK (0, "", "%lld", 1LL);
/* wint_t, wchar_t: ignore */
CHECK (1, "c", "%lc", 'c');
CHECK (3, "foo", "%ls", "foo");
/* The asterisk `*' in width or precision. */
CHECK (0, "", "%*d", 10, 12345);
CHECK (0, "", "%10.*d", 6, 12345);
CHECK (0, "", "%*.*d", 10, 6, 12345);
#endif
return 0;
}
|