| 1 |
/* Test of vasnprintf() and asnprintf() functions. |
| 2 |
Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2007. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include "vasnprintf.h" |
| 22 |
|
| 23 |
#include <errno.h> |
| 24 |
#include <stdarg.h> |
| 25 |
#include <stdlib.h> |
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
#include "macros.h" |
| 29 |
|
| 30 |
static void |
| 31 |
test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) |
| 32 |
{ |
| 33 |
char buf[8]; |
| 34 |
int size; |
| 35 |
|
| 36 |
for (size = 0; size <= 8; size++) |
| 37 |
{ |
| 38 |
size_t length = size; |
| 39 |
char *result = my_asnprintf (NULL, &length, "%d", 12345); |
| 40 |
ASSERT (result != NULL); |
| 41 |
ASSERT (strcmp (result, "12345") == 0); |
| 42 |
ASSERT (length == 5); |
| 43 |
free (result); |
| 44 |
} |
| 45 |
|
| 46 |
for (size = 0; size <= 8; size++) |
| 47 |
{ |
| 48 |
size_t length; |
| 49 |
char *result; |
| 50 |
|
| 51 |
memcpy (buf, "DEADBEEF", 8); |
| 52 |
length = size; |
| 53 |
result = my_asnprintf (buf, &length, "%d", 12345); |
| 54 |
ASSERT (result != NULL); |
| 55 |
ASSERT (strcmp (result, "12345") == 0); |
| 56 |
ASSERT (length == 5); |
| 57 |
if (size < 5 + 1) |
| 58 |
ASSERT (result != buf); |
| 59 |
ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0); |
| 60 |
if (result != buf) |
| 61 |
free (result); |
| 62 |
} |
| 63 |
|
| 64 |
/* Note: This test assumes IEEE 754 representation of 'double' floats. */ |
| 65 |
for (size = 0; size <= 8; size++) |
| 66 |
{ |
| 67 |
size_t length; |
| 68 |
char *result; |
| 69 |
|
| 70 |
memcpy (buf, "DEADBEEF", 8); |
| 71 |
length = size; |
| 72 |
result = my_asnprintf (buf, &length, "%2.0f", 1.6314159265358979e+125); |
| 73 |
ASSERT (result != NULL); |
| 74 |
/* The exact result and the result on glibc systems is |
| 75 |
163141592653589790215729350939528493057529598899734151772468186268423257777068536614838678161083520756952076273094236944990208 |
| 76 |
On Cygwin, the result is |
| 77 |
163141592653589790215729350939528493057529600000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
| 78 |
On HP-UX 11.31 / hppa and IRIX 6.5, the result is |
| 79 |
163141592653589790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 |
| 80 |
*/ |
| 81 |
ASSERT (strlen (result) == 126); |
| 82 |
ASSERT (memcmp (result, "163141592653589790", 18) == 0); |
| 83 |
ASSERT (length == 126); |
| 84 |
if (size < 126 + 1) |
| 85 |
ASSERT (result != buf); |
| 86 |
ASSERT (memcmp (buf + size, &"DEADBEEF"[size], 8 - size) == 0); |
| 87 |
if (result != buf) |
| 88 |
free (result); |
| 89 |
} |
| 90 |
|
| 91 |
/* Verify that [v]asnprintf() rejects a width > 2 GiB, < 4 GiB. */ |
| 92 |
{ |
| 93 |
size_t length; |
| 94 |
char *s = my_asnprintf (NULL, &length, "x%03000000000dy\n", -17); |
| 95 |
ASSERT (s == NULL); |
| 96 |
ASSERT (errno == EOVERFLOW); |
| 97 |
} |
| 98 |
{ |
| 99 |
size_t length; |
| 100 |
char *s = my_asnprintf (NULL, &length, "x%03000000000cy\n", '@'); |
| 101 |
ASSERT (s == NULL); |
| 102 |
ASSERT (errno == EOVERFLOW); |
| 103 |
} |
| 104 |
|
| 105 |
/* Verify that [v]asnprintf() rejects a width > 4 GiB. */ |
| 106 |
{ |
| 107 |
size_t length; |
| 108 |
char *s = |
| 109 |
my_asnprintf (NULL, &length, |
| 110 |
"x%04294967306dy\n", /* 2^32 + 10 */ |
| 111 |
-17); |
| 112 |
ASSERT (s == NULL); |
| 113 |
ASSERT (errno == EOVERFLOW); |
| 114 |
} |
| 115 |
{ |
| 116 |
size_t length; |
| 117 |
char *s = |
| 118 |
my_asnprintf (NULL, &length, |
| 119 |
"x%04294967306cy\n", /* 2^32 + 10 */ |
| 120 |
'@'); |
| 121 |
ASSERT (s == NULL); |
| 122 |
ASSERT (errno == EOVERFLOW); |
| 123 |
} |
| 124 |
{ |
| 125 |
size_t length; |
| 126 |
char *s = |
| 127 |
my_asnprintf (NULL, &length, |
| 128 |
"x%018446744073709551626dy\n", /* 2^64 + 10 */ |
| 129 |
-17); |
| 130 |
ASSERT (s == NULL); |
| 131 |
ASSERT (errno == EOVERFLOW); |
| 132 |
} |
| 133 |
{ |
| 134 |
size_t length; |
| 135 |
char *s = |
| 136 |
my_asnprintf (NULL, &length, |
| 137 |
"x%018446744073709551626cy\n", /* 2^64 + 10 */ |
| 138 |
'@'); |
| 139 |
ASSERT (s == NULL); |
| 140 |
ASSERT (errno == EOVERFLOW); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
static char * |
| 145 |
my_asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...) |
| 146 |
{ |
| 147 |
va_list args; |
| 148 |
char *ret; |
| 149 |
|
| 150 |
va_start (args, format); |
| 151 |
ret = vasnprintf (resultbuf, lengthp, format, args); |
| 152 |
va_end (args); |
| 153 |
return ret; |
| 154 |
} |
| 155 |
|
| 156 |
static void |
| 157 |
test_vasnprintf () |
| 158 |
{ |
| 159 |
test_function (my_asnprintf); |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
test_asnprintf () |
| 164 |
{ |
| 165 |
test_function (asnprintf); |
| 166 |
} |
| 167 |
|
| 168 |
int |
| 169 |
main () |
| 170 |
{ |
| 171 |
test_vasnprintf (); |
| 172 |
test_asnprintf (); |
| 173 |
return test_exit_status; |
| 174 |
} |