| 1 |
/* test-snprintf - Check whether snprintf allows doubly used strings. |
| 2 |
Copyright (C) 2020-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* Written by Mats Erik Andersson. */ |
| 20 |
|
| 21 |
/* |
| 22 |
* It is not in violation of POSIX, but implementations of snprintf() |
| 23 |
* differ in their ability to handle a situation where the target string |
| 24 |
* to be produced by snprintf() is at the same time providing data, which |
| 25 |
* are to be passed through the format string, and to be part of the new |
| 26 |
* resulting string. The different BSD unices, as well as OpenSolaris, |
| 27 |
* are known to correctly handle this, whereas glibc is known to fail. |
| 28 |
* |
| 29 |
* This test issues a warning that non-portable behaviour is possible, |
| 30 |
* should the program detect a discrepancy on this system. If the |
| 31 |
* program detects the macro PACKAGE to have value "inetutils", then |
| 32 |
* get attention with the exit value 77, to fit with our testing |
| 33 |
* frame work. |
| 34 |
* |
| 35 |
* When the environment variable VERBOSE is defined, an encouraging |
| 36 |
* message is issued also in the presence of an enhanced implementation |
| 37 |
* of snprintf(). |
| 38 |
*/ |
| 39 |
|
| 40 |
#include <config.h> |
| 41 |
|
| 42 |
#include <stdio.h> |
| 43 |
#include <stdlib.h> |
| 44 |
#include <string.h> |
| 45 |
|
| 46 |
#define MESSAGE "try a fool" |
| 47 |
#define WRONG_MESSAGE "fool" |
| 48 |
|
| 49 |
int |
| 50 |
main (void) |
| 51 |
{ |
| 52 |
int err = 0; |
| 53 |
char msg[sizeof (MESSAGE)] = "try a "; |
| 54 |
|
| 55 |
snprintf (msg, sizeof (msg), "%s%s", msg, WRONG_MESSAGE); |
| 56 |
|
| 57 |
if (!strcmp (msg, WRONG_MESSAGE)) |
| 58 |
{ |
| 59 |
printf ("Warning! This implementation of snprintf() is not able\n" |
| 60 |
"to handle string variables that appear as target and at the\n" |
| 61 |
"same time as source. This might be a portability issue!\n"); |
| 62 |
#ifdef PACKAGE |
| 63 |
if (!strcmp (PACKAGE, "inetutils")) |
| 64 |
err = 77; |
| 65 |
#endif |
| 66 |
} |
| 67 |
else if (getenv ("VERBOSE")) |
| 68 |
{ |
| 69 |
printf ("The present implementation of snprintf() allows the same\n" |
| 70 |
"string to act as target as well as source. Good news!\n"); |
| 71 |
} |
| 72 |
|
| 73 |
/* Always return successfully, as we are only producing a reminder, |
| 74 |
rather than a show stopper. */ |
| 75 |
return err; |
| 76 |
} |