]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - stdio-common/tst-printf.c
[powerpc] No need to enter "Ignore Exceptions Mode"
[thirdparty/glibc.git] / stdio-common / tst-printf.c
index 99eb3ef709419aed14eb73627cd9318b9d6bae8b..7e7a7699b484c69f8a5e112a04b327b6dfa8233e 100644 (file)
@@ -1,37 +1,49 @@
-/* Copyright (C) 1991, 92, 93, 95, 96, 97 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
    The GNU C Library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
 
 #ifdef BSD
 #include </usr/include/stdio.h>
 #define EXIT_SUCCESS 0
 #else
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #endif
 
 #include <float.h>
+#include <libc-diag.h>
 
-void rfg1 (void);
-void rfg2 (void);
+/* This whole file is picayune tests of corner cases of printf format strings.
+   The compiler warnings are not useful here.  */
+DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
+
+#if __GNUC_PREREQ (7, 0)
+/* Compiler warnings about snprintf output truncation should also be
+   ignored.  */
+DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-truncation");
+#endif
+
+static void rfg1 (void);
+static void rfg2 (void);
+static void rfg3 (void);
 
 
-void
+static void
 fmtchk (const char *fmt)
 {
   (void) fputs(fmt, stdout);
@@ -40,7 +52,7 @@ fmtchk (const char *fmt)
   (void) printf("'\n");
 }
 
-void
+static void
 fmtst1chk (const char *fmt)
 {
   (void) fputs(fmt, stdout);
@@ -49,7 +61,7 @@ fmtst1chk (const char *fmt)
   (void) printf("'\n");
 }
 
-void
+static void
 fmtst2chk (const char *fmt)
 {
   (void) fputs(fmt, stdout);
@@ -57,81 +69,14 @@ fmtst2chk (const char *fmt)
   (void) printf(fmt, 4, 4, 0x12);
   (void) printf("'\n");
 }
-\f
-/* This page is covered by the following copyright: */
-
-/* (C) Copyright C E Chew
- *
- * Feel free to copy, use and distribute this software provided:
- *
- *     1. you do not pretend that you wrote it
- *     2. you leave this copyright notice intact.
- */
-
-/*
- * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
- */
-
-#define DEC -123
-#define INT 255
-#define UNS (~0)
-
-/* Formatted Output Test
- *
- * This exercises the output formatting code.
- */
-
-void
-fp_test (void)
-{
-  int i, j, k, l;
-  char buf[7];
-  char *prefix = buf;
-  char tp[20];
-
-  puts("\nFormatted output test");
-  printf("prefix  6d      6o      6x      6X      6u\n");
-  strcpy(prefix, "%");
-  for (i = 0; i < 2; i++) {
-    for (j = 0; j < 2; j++) {
-      for (k = 0; k < 2; k++) {
-       for (l = 0; l < 2; l++) {
-         strcpy(prefix, "%");
-         if (i == 0) strcat(prefix, "-");
-         if (j == 0) strcat(prefix, "+");
-         if (k == 0) strcat(prefix, "#");
-         if (l == 0) strcat(prefix, "0");
-         printf("%5s |", prefix);
-         strcpy(tp, prefix);
-         strcat(tp, "6d |");
-         printf(tp, DEC);
-         strcpy(tp, prefix);
-         strcat(tp, "6o |");
-         printf(tp, INT);
-         strcpy(tp, prefix);
-         strcat(tp, "6x |");
-         printf(tp, INT);
-         strcpy(tp, prefix);
-         strcat(tp, "6X |");
-         printf(tp, INT);
-         strcpy(tp, prefix);
-         strcat(tp, "6u |");
-         printf(tp, UNS);
-         printf("\n");
-       }
-      }
-    }
-  }
-  printf("%10s\n", (char *) NULL);
-  printf("%-10s\n", (char *) NULL);
-}
-\f
-int
-main (int argc, char *argv[])
+
+static int
+do_test (void)
 {
   static char shortstr[] = "Hi, Z.";
   static char longstr[] = "Good morning, Doctor Chandra.  This is Hal.  \
 I am ready for my first lesson today.";
+  int result = 0;
 
   fmtchk("%.4x");
   fmtchk("%04x");
@@ -146,7 +91,7 @@ I am ready for my first lesson today.";
   fmtst2chk("%0*.*x");
 
 #ifndef        BSD
-  printf("bad format:\t\"%z\"\n");
+  printf("bad format:\t\"%b\"\n");
   printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
 #endif
 
@@ -157,7 +102,7 @@ I am ready for my first lesson today.";
   printf("long octal negative:\t\"%lo\"\n", -2345L);
   printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
   printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
-  printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456);
+  printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
   printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
   printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
 
@@ -165,9 +110,18 @@ I am ready for my first lesson today.";
   printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
   printf("space-padded string:\t\"%10s\"\n", shortstr);
   printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
+  /* GCC 9 warns about the NULL format argument; this is deliberately
+     tested here.  */
+  DIAG_PUSH_NEEDS_COMMENT;
+#if __GNUC_PREREQ (7, 0)
+  DIAG_IGNORE_NEEDS_COMMENT (9, "-Wformat-overflow=");
+#endif
   printf("null string:\t\"%s\"\n", (char *)NULL);
+  DIAG_POP_NEEDS_COMMENT;
   printf("limited string:\t\"%.22s\"\n", longstr);
 
+  printf("a-style max:\t\"%a\"\n", DBL_MAX);
+  printf("a-style -max:\t\"%a\"\n", -DBL_MAX);
   printf("e-style >= 1:\t\"%e\"\n", 12.34);
   printf("e-style >= .1:\t\"%e\"\n", 0.1234);
   printf("e-style < .1:\t\"%e\"\n", 0.001234);
@@ -187,6 +141,8 @@ I am ready for my first lesson today.";
 
   printf ("%#03x\n", 1);
 
+  printf ("something really insane: %.10000f\n", 1.0);
+
   {
     double d = FLT_MIN;
     int niter = 17;
@@ -212,12 +168,16 @@ I am ready for my first lesson today.";
 
   {
     char buf[20];
+    char buf2[512];
     printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
-           snprintf (buf, sizeof (buf), "%30s", "foo"), sizeof (buf), buf);
+           snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
+           buf);
+    printf ("snprintf (\"%%.999999u\", 10) == %d\n",
+           snprintf (buf2, sizeof (buf2), "%.999999u", 10));
   }
 
-  fp_test ();
-
+  printf("%.8f\n", DBL_MAX);
+  printf("%.8f\n", -DBL_MAX);
   printf ("%e should be 1.234568e+06\n", 1234567.8);
   printf ("%f should be 1234567.800000\n", 1234567.8);
   printf ("%g should be 1.23457e+06\n", 1234567.8);
@@ -226,30 +186,81 @@ I am ready for my first lesson today.";
   printf ("%g should be 10\n", 10.0);
   printf ("%g should be 0.02\n", 0.02);
 
+#if 0
+  /* This test rather checks the way the compiler handles constant
+     folding.  gcc behavior wrt to this changed in 3.2 so it is not a
+     portable test.  */
   {
     double x=1.0;
     printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
   }
-
-  puts ("--- Should be no further output. ---");
-  rfg1 ();
-  rfg2 ();
+#endif
 
   {
     char buf[200];
-    int result;
 
     sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
 
-    result = strcmp (buf,
-                    "onetwo                 three                         ");
+    result |= strcmp (buf,
+                     "onetwo                 three                         ");
 
     puts (result != 0 ? "Test failed!" : "Test ok.");
-    return result != 0;
   }
+
+  {
+    char buf[200];
+
+    sprintf (buf, "%07Lo", 040000000000ll);
+    printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
+
+    if (strcmp (buf, "40000000000") != 0)
+      {
+       result = 1;
+       fputs ("\tFAILED", stdout);
+      }
+    puts ("");
+  }
+
+  printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
+  printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
+  printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
+  printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
+
+  printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
+         UCHAR_MAX + 2, UCHAR_MAX + 2);
+  printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
+  printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
+         UCHAR_MAX + 2, UCHAR_MAX + 2);
+  printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
+
+  puts ("--- Should be no further output. ---");
+  rfg1 ();
+  rfg2 ();
+  rfg3 ();
+
+  {
+    char bytes[7];
+    char buf[20];
+
+    memset (bytes, '\xff', sizeof bytes);
+    sprintf (buf, "foo%hhn\n", &bytes[3]);
+    if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
+       || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
+      {
+       puts ("%hhn overwrite more bytes");
+       result = 1;
+      }
+    if (bytes[3] != 3)
+      {
+       puts ("%hhn wrote incorrect value");
+       result = 1;
+      }
+  }
+
+  return result != 0;
 }
 \f
-void
+static void
 rfg1 (void)
 {
   char buf[100];
@@ -274,7 +285,7 @@ rfg1 (void)
     printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
 }
 
-void
+static void
 rfg2 (void)
 {
   int prec;
@@ -309,3 +320,26 @@ rfg2 (void)
   if (strcmp (buf, " 021") != 0)
     printf ("got: '%s', expected: '%s'\n", buf, " 021");
 }
+
+static void
+rfg3 (void)
+{
+  char buf[100];
+  double g = 5.0000001;
+  unsigned long l = 1234567890;
+  double d = 321.7654321;
+  const char s[] = "test-string";
+  int i = 12345;
+  int h = 1234;
+
+  sprintf (buf,
+          "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
+          i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
+  if (strcmp (buf,
+             "   12345  1234    11145401322     321.765432   3.217654e+02   5    test-string") != 0)
+    printf ("got: '%s', expected: '%s'\n", buf,
+           "   12345  1234    11145401322     321.765432   3.217654e+02   5    test-string");
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"