Back to index

glibc  2.9
tst-chk1.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <fcntl.h>
00022 #include <locale.h>
00023 #include <obstack.h>
00024 #include <paths.h>
00025 #include <setjmp.h>
00026 #include <signal.h>
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <unistd.h>
00031 #include <wchar.h>
00032 #include <sys/socket.h>
00033 #include <sys/un.h>
00034 
00035 #define obstack_chunk_alloc malloc
00036 #define obstack_chunk_free free
00037 
00038 char *temp_filename;
00039 static void do_prepare (void);
00040 static int do_test (void);
00041 #define PREPARE(argc, argv) do_prepare ()
00042 #define TEST_FUNCTION do_test ()
00043 #include "../test-skeleton.c"
00044 
00045 static void
00046 do_prepare (void)
00047 {
00048   int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
00049   if (temp_fd == -1)
00050     {
00051       printf ("cannot create temporary file: %m\n");
00052       exit (1);
00053     }
00054 
00055   const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
00056   if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
00057     {
00058       puts ("could not write test strings into file");
00059       unlink (temp_filename);
00060       exit (1);
00061     }
00062 }
00063 
00064 volatile int chk_fail_ok;
00065 volatile int ret;
00066 jmp_buf chk_fail_buf;
00067 
00068 static void
00069 handler (int sig)
00070 {
00071   if (chk_fail_ok)
00072     {
00073       chk_fail_ok = 0;
00074       longjmp (chk_fail_buf, 1);
00075     }
00076   else
00077     _exit (127);
00078 }
00079 
00080 char buf[10];
00081 wchar_t wbuf[10];
00082 volatile size_t l0;
00083 volatile char *p;
00084 volatile wchar_t *wp;
00085 const char *str1 = "JIHGFEDCBA";
00086 const char *str2 = "F";
00087 const char *str3 = "%s%n%s%n";
00088 const char *str4 = "Hello, ";
00089 const char *str5 = "World!\n";
00090 const wchar_t *wstr1 = L"JIHGFEDCBA";
00091 const wchar_t *wstr2 = L"F";
00092 const wchar_t *wstr3 = L"%s%n%s%n";
00093 const wchar_t *wstr4 = L"Hello, ";
00094 const wchar_t *wstr5 = L"World!\n";
00095 char buf2[10] = "%s";
00096 int num1 = 67;
00097 int num2 = 987654;
00098 
00099 #define FAIL() \
00100   do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
00101 #define CHK_FAIL_START \
00102   chk_fail_ok = 1;                        \
00103   if (! setjmp (chk_fail_buf))                   \
00104     {
00105 #define CHK_FAIL_END \
00106       chk_fail_ok = 0;                           \
00107       FAIL ();                                   \
00108     }
00109 #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
00110 #define CHK_FAIL2_START CHK_FAIL_START
00111 #define CHK_FAIL2_END CHK_FAIL_END
00112 #else
00113 #define CHK_FAIL2_START
00114 #define CHK_FAIL2_END
00115 #endif
00116 
00117 static int
00118 do_test (void)
00119 {
00120   struct sigaction sa;
00121   sa.sa_handler = handler;
00122   sa.sa_flags = 0;
00123   sigemptyset (&sa.sa_mask);
00124 
00125   sigaction (SIGABRT, &sa, NULL);
00126 
00127   /* Avoid all the buffer overflow messages on stderr.  */
00128   int fd = open (_PATH_DEVNULL, O_WRONLY);
00129   if (fd == -1)
00130     close (STDERR_FILENO);
00131   else
00132     {
00133       dup2 (fd, STDERR_FILENO);
00134       close (fd);
00135     }
00136   setenv ("LIBC_FATAL_STDERR_", "1", 1);
00137 
00138   struct A { char buf1[9]; char buf2[1]; } a;
00139   struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
00140 
00141   printf ("Test checking routines at fortify level %d\n",
00142 #ifdef __USE_FORTIFY_LEVEL
00143          (int) __USE_FORTIFY_LEVEL
00144 #else
00145          0
00146 #endif
00147          );
00148 
00149 #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
00150   printf ("Test skipped");
00151   if (l0 == 0)
00152     return 0;
00153 #endif
00154 
00155   /* These ops can be done without runtime checking of object size.  */
00156   memcpy (buf, "abcdefghij", 10);
00157   memmove (buf + 1, buf, 9);
00158   if (memcmp (buf, "aabcdefghi", 10))
00159     FAIL ();
00160 
00161   if (mempcpy (buf + 5, "abcde", 5) != buf + 10
00162       || memcmp (buf, "aabcdabcde", 10))
00163     FAIL ();
00164 
00165   memset (buf + 8, 'j', 2);
00166   if (memcmp (buf, "aabcdabcjj", 10))
00167     FAIL ();
00168 
00169   strcpy (buf + 4, "EDCBA");
00170   if (memcmp (buf, "aabcEDCBA", 10))
00171     FAIL ();
00172 
00173   if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
00174     FAIL ();
00175 
00176   strncpy (buf + 6, "X", 4);
00177   if (memcmp (buf, "aabcEDX\0\0", 10))
00178     FAIL ();
00179 
00180   if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
00181     FAIL ();
00182 
00183   if (snprintf (buf + 7, 3, "%s", "987654") != 6
00184       || memcmp (buf, "aabcEDX98", 10))
00185     FAIL ();
00186 
00187   /* These ops need runtime checking, but shouldn't __chk_fail.  */
00188   memcpy (buf, "abcdefghij", l0 + 10);
00189   memmove (buf + 1, buf, l0 + 9);
00190   if (memcmp (buf, "aabcdefghi", 10))
00191     FAIL ();
00192 
00193   if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
00194       || memcmp (buf, "aabcdabcde", 10))
00195     FAIL ();
00196 
00197   memset (buf + 8, 'j', l0 + 2);
00198   if (memcmp (buf, "aabcdabcjj", 10))
00199     FAIL ();
00200 
00201   strcpy (buf + 4, str1 + 5);
00202   if (memcmp (buf, "aabcEDCBA", 10))
00203     FAIL ();
00204 
00205   if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
00206     FAIL ();
00207 
00208   strncpy (buf + 6, "X", l0 + 4);
00209   if (memcmp (buf, "aabcEDX\0\0", 10))
00210     FAIL ();
00211 
00212   if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
00213       || memcmp (buf, "aabcEcd\0\0", 10))
00214     FAIL ();
00215 
00216   if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
00217     FAIL ();
00218 
00219   if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
00220     FAIL ();
00221 
00222   buf[l0 + 8] = '\0';
00223   strcat (buf, "A");
00224   if (memcmp (buf, "aabcEcd9A", 10))
00225     FAIL ();
00226 
00227   buf[l0 + 7] = '\0';
00228   strncat (buf, "ZYXWV", l0 + 2);
00229   if (memcmp (buf, "aabcEcdZY", 10))
00230     FAIL ();
00231 
00232   memcpy (a.buf1, "abcdefghij", l0 + 10);
00233   memmove (a.buf1 + 1, a.buf1, l0 + 9);
00234   if (memcmp (a.buf1, "aabcdefghi", 10))
00235     FAIL ();
00236 
00237   if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
00238       || memcmp (a.buf1, "aabcdabcde", 10))
00239     FAIL ();
00240 
00241   memset (a.buf1 + 8, 'j', l0 + 2);
00242   if (memcmp (a.buf1, "aabcdabcjj", 10))
00243     FAIL ();
00244 
00245 #if __USE_FORTIFY_LEVEL < 2
00246   /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
00247      and sufficient GCC support, as the string operations overflow
00248      from a.buf1 into a.buf2.  */
00249   strcpy (a.buf1 + 4, str1 + 5);
00250   if (memcmp (a.buf1, "aabcEDCBA", 10))
00251     FAIL ();
00252 
00253   if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
00254       || memcmp (a.buf1, "aabcEDCBF", 10))
00255     FAIL ();
00256 
00257   strncpy (a.buf1 + 6, "X", l0 + 4);
00258   if (memcmp (a.buf1, "aabcEDX\0\0", 10))
00259     FAIL ();
00260 
00261   if (sprintf (a.buf1 + 7, "%d", num1) != 2
00262       || memcmp (a.buf1, "aabcEDX67", 10))
00263     FAIL ();
00264 
00265   if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
00266       || memcmp (a.buf1, "aabcEDX98", 10))
00267     FAIL ();
00268 
00269   a.buf1[l0 + 8] = '\0';
00270   strcat (a.buf1, "A");
00271   if (memcmp (a.buf1, "aabcEDX9A", 10))
00272     FAIL ();
00273 
00274   a.buf1[l0 + 7] = '\0';
00275   strncat (a.buf1, "ZYXWV", l0 + 2);
00276   if (memcmp (a.buf1, "aabcEDXZY", 10))
00277     FAIL ();
00278 
00279 #endif
00280 
00281 #if __USE_FORTIFY_LEVEL >= 1
00282   /* Now check if all buffer overflows are caught at runtime.  */
00283 
00284   CHK_FAIL_START
00285   memcpy (buf + 1, "abcdefghij", l0 + 10);
00286   CHK_FAIL_END
00287 
00288   CHK_FAIL_START
00289   memmove (buf + 2, buf + 1, l0 + 9);
00290   CHK_FAIL_END
00291 
00292   CHK_FAIL_START
00293   p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
00294   CHK_FAIL_END
00295 
00296   CHK_FAIL_START
00297   memset (buf + 9, 'j', l0 + 2);
00298   CHK_FAIL_END
00299 
00300   CHK_FAIL_START
00301   strcpy (buf + 5, str1 + 5);
00302   CHK_FAIL_END
00303 
00304   CHK_FAIL_START
00305   p = stpcpy (buf + 9, str2);
00306   CHK_FAIL_END
00307 
00308   CHK_FAIL_START
00309   strncpy (buf + 7, "X", l0 + 4);
00310   CHK_FAIL_END
00311 
00312   CHK_FAIL_START
00313   stpncpy (buf + 6, "cd", l0 + 5);
00314   CHK_FAIL_END
00315 
00316 # if !defined __cplusplus || defined __va_arg_pack
00317   CHK_FAIL_START
00318   sprintf (buf + 8, "%d", num1);
00319   CHK_FAIL_END
00320 
00321   CHK_FAIL_START
00322   snprintf (buf + 8, l0 + 3, "%d", num2);
00323   CHK_FAIL_END
00324 
00325   CHK_FAIL_START
00326   swprintf (wbuf + 8, 3, L"%d", num1);
00327   CHK_FAIL_END
00328 
00329   CHK_FAIL_START
00330   swprintf (wbuf + 8, l0 + 3, L"%d", num1);
00331   CHK_FAIL_END
00332 # endif
00333 
00334   memcpy (buf, str1 + 2, l0 + 9);
00335   CHK_FAIL_START
00336   strcat (buf, "AB");
00337   CHK_FAIL_END
00338 
00339   memcpy (buf, str1 + 3, l0 + 8);
00340   CHK_FAIL_START
00341   strncat (buf, "ZYXWV", l0 + 3);
00342   CHK_FAIL_END
00343 
00344   CHK_FAIL_START
00345   memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
00346   CHK_FAIL_END
00347 
00348   CHK_FAIL_START
00349   memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
00350   CHK_FAIL_END
00351 
00352   CHK_FAIL_START
00353   p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
00354   CHK_FAIL_END
00355 
00356   CHK_FAIL_START
00357   memset (a.buf1 + 9, 'j', l0 + 2);
00358   CHK_FAIL_END
00359 
00360 # if __USE_FORTIFY_LEVEL >= 2
00361 #  define O 0
00362 # else
00363 #  define O 1
00364 # endif
00365 
00366   CHK_FAIL_START
00367   strcpy (a.buf1 + (O + 4), str1 + 5);
00368   CHK_FAIL_END
00369 
00370   CHK_FAIL_START
00371   p = stpcpy (a.buf1 + (O + 8), str2);
00372   CHK_FAIL_END
00373 
00374   CHK_FAIL_START
00375   strncpy (a.buf1 + (O + 6), "X", l0 + 4);
00376   CHK_FAIL_END
00377 
00378 # if !defined __cplusplus || defined __va_arg_pack
00379   CHK_FAIL_START
00380   sprintf (a.buf1 + (O + 7), "%d", num1);
00381   CHK_FAIL_END
00382 
00383   CHK_FAIL_START
00384   snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
00385   CHK_FAIL_END
00386 # endif
00387 
00388   memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
00389   CHK_FAIL_START
00390   strcat (a.buf1, "AB");
00391   CHK_FAIL_END
00392 
00393   memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
00394   CHK_FAIL_START
00395   strncat (a.buf1, "ZYXWV", l0 + 3);
00396   CHK_FAIL_END
00397 #endif
00398 
00399 
00400   /* These ops can be done without runtime checking of object size.  */
00401   wmemcpy (wbuf, L"abcdefghij", 10);
00402   wmemmove (wbuf + 1, wbuf, 9);
00403   if (wmemcmp (wbuf, L"aabcdefghi", 10))
00404     FAIL ();
00405 
00406   if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
00407       || wmemcmp (wbuf, L"aabcdabcde", 10))
00408     FAIL ();
00409 
00410   wmemset (wbuf + 8, L'j', 2);
00411   if (wmemcmp (wbuf, L"aabcdabcjj", 10))
00412     FAIL ();
00413 
00414   wcscpy (wbuf + 4, L"EDCBA");
00415   if (wmemcmp (wbuf, L"aabcEDCBA", 10))
00416     FAIL ();
00417 
00418   if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
00419     FAIL ();
00420 
00421   wcsncpy (wbuf + 6, L"X", 4);
00422   if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
00423     FAIL ();
00424 
00425   if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
00426       || wmemcmp (wbuf, L"aabcEDX98", 10))
00427     FAIL ();
00428 
00429   if (swprintf (wbuf + 7, 3, L"64") != 2
00430       || wmemcmp (wbuf, L"aabcEDX64", 10))
00431     FAIL ();
00432 
00433   /* These ops need runtime checking, but shouldn't __chk_fail.  */
00434   wmemcpy (wbuf, L"abcdefghij", l0 + 10);
00435   wmemmove (wbuf + 1, wbuf, l0 + 9);
00436   if (wmemcmp (wbuf, L"aabcdefghi", 10))
00437     FAIL ();
00438 
00439   if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
00440       || wmemcmp (wbuf, L"aabcdabcde", 10))
00441     FAIL ();
00442 
00443   wmemset (wbuf + 8, L'j', l0 + 2);
00444   if (wmemcmp (wbuf, L"aabcdabcjj", 10))
00445     FAIL ();
00446 
00447   wcscpy (wbuf + 4, wstr1 + 5);
00448   if (wmemcmp (wbuf, L"aabcEDCBA", 10))
00449     FAIL ();
00450 
00451   if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
00452     FAIL ();
00453 
00454   wcsncpy (wbuf + 6, L"X", l0 + 4);
00455   if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
00456     FAIL ();
00457 
00458   if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
00459       || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
00460     FAIL ();
00461 
00462   if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
00463       || wmemcmp (wbuf, L"aabcEcd98", 10))
00464     FAIL ();
00465 
00466   wbuf[l0 + 8] = L'\0';
00467   wcscat (wbuf, L"A");
00468   if (wmemcmp (wbuf, L"aabcEcd9A", 10))
00469     FAIL ();
00470 
00471   wbuf[l0 + 7] = L'\0';
00472   wcsncat (wbuf, L"ZYXWV", l0 + 2);
00473   if (wmemcmp (wbuf, L"aabcEcdZY", 10))
00474     FAIL ();
00475 
00476   wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
00477   wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
00478   if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
00479     FAIL ();
00480 
00481   if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
00482       || wmemcmp (wa.buf1, L"aabcdabcde", 10))
00483     FAIL ();
00484 
00485   wmemset (wa.buf1 + 8, L'j', l0 + 2);
00486   if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
00487     FAIL ();
00488 
00489 #if __USE_FORTIFY_LEVEL < 2
00490   /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
00491      and sufficient GCC support, as the string operations overflow
00492      from a.buf1 into a.buf2.  */
00493   wcscpy (wa.buf1 + 4, wstr1 + 5);
00494   if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
00495     FAIL ();
00496 
00497   if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
00498       || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
00499     FAIL ();
00500 
00501   wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
00502   if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
00503     FAIL ();
00504 
00505   if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
00506       || wmemcmp (wa.buf1, L"aabcEDX98", 10))
00507     FAIL ();
00508 
00509   wa.buf1[l0 + 8] = L'\0';
00510   wcscat (wa.buf1, L"A");
00511   if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
00512     FAIL ();
00513 
00514   wa.buf1[l0 + 7] = L'\0';
00515   wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
00516   if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
00517     FAIL ();
00518 
00519 #endif
00520 
00521 #if __USE_FORTIFY_LEVEL >= 1
00522   /* Now check if all buffer overflows are caught at runtime.  */
00523 
00524   CHK_FAIL_START
00525   wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
00526   CHK_FAIL_END
00527 
00528   CHK_FAIL_START
00529   wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
00530   CHK_FAIL_END
00531 
00532   CHK_FAIL_START
00533   wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
00534   CHK_FAIL_END
00535 
00536   CHK_FAIL_START
00537   wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
00538   CHK_FAIL_END
00539 
00540   CHK_FAIL_START
00541   wmemset (wbuf + 9, L'j', l0 + 2);
00542   CHK_FAIL_END
00543 
00544   CHK_FAIL_START
00545   wcscpy (wbuf + 5, wstr1 + 5);
00546   CHK_FAIL_END
00547 
00548   CHK_FAIL_START
00549   wp = wcpcpy (wbuf + 9, wstr2);
00550   CHK_FAIL_END
00551 
00552   CHK_FAIL_START
00553   wcsncpy (wbuf + 7, L"X", l0 + 4);
00554   CHK_FAIL_END
00555 
00556   CHK_FAIL_START
00557   wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
00558   CHK_FAIL_END
00559 
00560   CHK_FAIL_START
00561   wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
00562   CHK_FAIL_END
00563 
00564   CHK_FAIL_START
00565   wcpncpy (wbuf + 6, L"cd", l0 + 5);
00566   CHK_FAIL_END
00567 
00568   wmemcpy (wbuf, wstr1 + 2, l0 + 9);
00569   CHK_FAIL_START
00570   wcscat (wbuf, L"AB");
00571   CHK_FAIL_END
00572 
00573   wmemcpy (wbuf, wstr1 + 3, l0 + 8);
00574   CHK_FAIL_START
00575   wcsncat (wbuf, L"ZYXWV", l0 + 3);
00576   CHK_FAIL_END
00577 
00578   CHK_FAIL_START
00579   wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
00580   CHK_FAIL_END
00581 
00582   CHK_FAIL_START
00583   wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
00584   CHK_FAIL_END
00585 
00586   CHK_FAIL_START
00587   wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
00588   CHK_FAIL_END
00589 
00590   CHK_FAIL_START
00591   wmemset (wa.buf1 + 9, L'j', l0 + 2);
00592   CHK_FAIL_END
00593 
00594 #if __USE_FORTIFY_LEVEL >= 2
00595 # define O 0
00596 #else
00597 # define O 1
00598 #endif
00599 
00600   CHK_FAIL_START
00601   wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
00602   CHK_FAIL_END
00603 
00604   CHK_FAIL_START
00605   wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
00606   CHK_FAIL_END
00607 
00608   CHK_FAIL_START
00609   wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
00610   CHK_FAIL_END
00611 
00612   wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
00613   CHK_FAIL_START
00614   wcscat (wa.buf1, L"AB");
00615   CHK_FAIL_END
00616 
00617   wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
00618   CHK_FAIL_START
00619   wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
00620   CHK_FAIL_END
00621 #endif
00622 
00623 
00624   /* Now checks for %n protection.  */
00625 
00626   /* Constant literals passed directly are always ok
00627      (even with warnings about possible bugs from GCC).  */
00628   int n1, n2;
00629   if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
00630       || n1 != 1 || n2 != 2)
00631     FAIL ();
00632 
00633   /* In this case the format string is not known at compile time,
00634      but resides in read-only memory, so is ok.  */
00635   if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
00636       || n1 != 1 || n2 != 2)
00637     FAIL ();
00638 
00639   strcpy (buf2 + 2, "%n%s%n");
00640   /* When the format string is writable and contains %n,
00641      with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
00642   CHK_FAIL2_START
00643   if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
00644     FAIL ();
00645   CHK_FAIL2_END
00646 
00647   CHK_FAIL2_START
00648   if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
00649     FAIL ();
00650   CHK_FAIL2_END
00651 
00652   /* But if there is no %n, even writable format string
00653      should work.  */
00654   buf2[6] = '\0';
00655   if (sprintf (buf, buf2 + 4, str2) != 1)
00656     FAIL ();
00657 
00658   /* Constant literals passed directly are always ok
00659      (even with warnings about possible bugs from GCC).  */
00660   if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
00661       || n1 != 7 || n2 != 14)
00662     FAIL ();
00663 
00664   /* In this case the format string is not known at compile time,
00665      but resides in read-only memory, so is ok.  */
00666   if (printf (str3, str4, &n1, str5, &n2) != 14
00667       || n1 != 7 || n2 != 14)
00668     FAIL ();
00669 
00670   strcpy (buf2 + 2, "%n%s%n");
00671   /* When the format string is writable and contains %n,
00672      with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
00673   CHK_FAIL2_START
00674   if (printf (buf2, str4, &n1, str5, &n1) != 14)
00675     FAIL ();
00676   CHK_FAIL2_END
00677 
00678   /* But if there is no %n, even writable format string
00679      should work.  */
00680   buf2[6] = '\0';
00681   if (printf (buf2 + 4, str5) != 7)
00682     FAIL ();
00683 
00684   FILE *fp = stdout;
00685 
00686   /* Constant literals passed directly are always ok
00687      (even with warnings about possible bugs from GCC).  */
00688   if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
00689       || n1 != 7 || n2 != 14)
00690     FAIL ();
00691 
00692   /* In this case the format string is not known at compile time,
00693      but resides in read-only memory, so is ok.  */
00694   if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
00695       || n1 != 7 || n2 != 14)
00696     FAIL ();
00697 
00698   strcpy (buf2 + 2, "%n%s%n");
00699   /* When the format string is writable and contains %n,
00700      with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
00701   CHK_FAIL2_START
00702   if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
00703     FAIL ();
00704   CHK_FAIL2_END
00705 
00706   /* But if there is no %n, even writable format string
00707      should work.  */
00708   buf2[6] = '\0';
00709   if (fprintf (fp, buf2 + 4, str5) != 7)
00710     FAIL ();
00711 
00712   char *my_ptr = NULL;
00713   strcpy (buf2 + 2, "%n%s%n");
00714   /* When the format string is writable and contains %n,
00715      with -D_FORTIFY_SOURCE=2 it causes __chk_fail.  */
00716   CHK_FAIL2_START
00717   if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
00718     FAIL ();
00719   else
00720     free (my_ptr);
00721   CHK_FAIL2_END
00722 
00723   struct obstack obs;
00724   obstack_init (&obs);
00725   CHK_FAIL2_START
00726   if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
00727     FAIL ();
00728   CHK_FAIL2_END
00729   obstack_free (&obs, NULL);
00730 
00731   my_ptr = NULL;
00732   if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
00733     FAIL ();
00734   else
00735     free (my_ptr);
00736 
00737   obstack_init (&obs);
00738   if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
00739     FAIL ();
00740   obstack_free (&obs, NULL);
00741 
00742   if (freopen (temp_filename, "r", stdin) == NULL)
00743     {
00744       puts ("could not open temporary file");
00745       exit (1);
00746     }
00747 
00748   if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
00749     FAIL ();
00750   if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
00751     FAIL ();
00752 
00753 #if __USE_FORTIFY_LEVEL >= 1
00754   CHK_FAIL_START
00755   if (gets (buf) != buf)
00756     FAIL ();
00757   CHK_FAIL_END
00758 #endif
00759 
00760   rewind (stdin);
00761 
00762   if (fgets (buf, sizeof (buf), stdin) != buf
00763       || memcmp (buf, "abcdefgh\n", 10))
00764     FAIL ();
00765   if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
00766     FAIL ();
00767 
00768   rewind (stdin);
00769 
00770   if (fgets (buf, l0 + sizeof (buf), stdin) != buf
00771       || memcmp (buf, "abcdefgh\n", 10))
00772     FAIL ();
00773 
00774 #if __USE_FORTIFY_LEVEL >= 1
00775   CHK_FAIL_START
00776   if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
00777     FAIL ();
00778   CHK_FAIL_END
00779 
00780   CHK_FAIL_START
00781   if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
00782     FAIL ();
00783   CHK_FAIL_END
00784 #endif
00785 
00786   rewind (stdin);
00787 
00788   if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
00789       || memcmp (buf, "abcdefgh\n", 10))
00790     FAIL ();
00791   if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
00792       || memcmp (buf, "ABCDEFGHI", 10))
00793     FAIL ();
00794 
00795   rewind (stdin);
00796 
00797   if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
00798       || memcmp (buf, "abcdefgh\n", 10))
00799     FAIL ();
00800 
00801 #if __USE_FORTIFY_LEVEL >= 1
00802   CHK_FAIL_START
00803   if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
00804     FAIL ();
00805   CHK_FAIL_END
00806 
00807   CHK_FAIL_START
00808   if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
00809     FAIL ();
00810   CHK_FAIL_END
00811 #endif
00812 
00813   rewind (stdin);
00814 
00815   if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
00816       || memcmp (buf, "abcdefgh\nA", 10))
00817     FAIL ();
00818   if (fread (buf, sizeof (buf), 1, stdin) != 1
00819       || memcmp (buf, "BCDEFGHI\na", 10))
00820     FAIL ();
00821 
00822   rewind (stdin);
00823 
00824   if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
00825       || memcmp (buf, "abcdefgh\nA", 10))
00826     FAIL ();
00827   if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
00828       || memcmp (buf, "BCDEFGHI\na", 10))
00829     FAIL ();
00830 
00831 #if __USE_FORTIFY_LEVEL >= 1
00832   CHK_FAIL_START
00833   if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
00834     FAIL ();
00835   CHK_FAIL_END
00836 
00837   CHK_FAIL_START
00838   if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
00839     FAIL ();
00840   CHK_FAIL_END
00841 #endif
00842 
00843   rewind (stdin);
00844 
00845   if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
00846       || memcmp (buf, "abcdefgh\nA", 10))
00847     FAIL ();
00848   if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
00849       || memcmp (buf, "BCDEFGHI\na", 10))
00850     FAIL ();
00851 
00852   rewind (stdin);
00853 
00854   if (fread_unlocked (buf, 1, 4, stdin) != 4
00855       || memcmp (buf, "abcdFGHI\na", 10))
00856     FAIL ();
00857   if (fread_unlocked (buf, 4, 1, stdin) != 1
00858       || memcmp (buf, "efghFGHI\na", 10))
00859     FAIL ();
00860 
00861   rewind (stdin);
00862 
00863   if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
00864       || memcmp (buf, "abcdefgh\nA", 10))
00865     FAIL ();
00866   if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
00867       || memcmp (buf, "BCDEFGHI\na", 10))
00868     FAIL ();
00869 
00870 #if __USE_FORTIFY_LEVEL >= 1
00871   CHK_FAIL_START
00872   if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
00873     FAIL ();
00874   CHK_FAIL_END
00875 
00876   CHK_FAIL_START
00877   if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
00878     FAIL ();
00879   CHK_FAIL_END
00880 #endif
00881 
00882   lseek (fileno (stdin), 0, SEEK_SET);
00883 
00884   if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
00885       || memcmp (buf, "abcdefgh\n", 9))
00886     FAIL ();
00887   if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
00888       || memcmp (buf, "ABCDEFGHI", 9))
00889     FAIL ();
00890 
00891   lseek (fileno (stdin), 0, SEEK_SET);
00892 
00893   if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
00894       || memcmp (buf, "abcdefgh\n", 9))
00895     FAIL ();
00896 
00897 #if __USE_FORTIFY_LEVEL >= 1
00898   CHK_FAIL_START
00899   if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
00900     FAIL ();
00901   CHK_FAIL_END
00902 #endif
00903 
00904   if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
00905       != sizeof (buf) - 1
00906       || memcmp (buf, "\nABCDEFGH", 9))
00907     FAIL ();
00908   if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
00909       || memcmp (buf, "abcdefgh\n", 9))
00910     FAIL ();
00911   if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
00912       != sizeof (buf) - 1
00913       || memcmp (buf, "h\nABCDEFG", 9))
00914     FAIL ();
00915 
00916 #if __USE_FORTIFY_LEVEL >= 1
00917   CHK_FAIL_START
00918   if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
00919       != sizeof (buf) + 1)
00920     FAIL ();
00921   CHK_FAIL_END
00922 #endif
00923 
00924   if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
00925       != sizeof (buf) - 1
00926       || memcmp (buf, "\nABCDEFGH", 9))
00927     FAIL ();
00928   if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
00929       || memcmp (buf, "abcdefgh\n", 9))
00930     FAIL ();
00931   if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
00932       != sizeof (buf) - 1
00933       || memcmp (buf, "h\nABCDEFG", 9))
00934     FAIL ();
00935 
00936 #if __USE_FORTIFY_LEVEL >= 1
00937   CHK_FAIL_START
00938   if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
00939       != sizeof (buf) + 1)
00940     FAIL ();
00941   CHK_FAIL_END
00942 #endif
00943 
00944   if (freopen (temp_filename, "r", stdin) == NULL)
00945     {
00946       puts ("could not open temporary file");
00947       exit (1);
00948     }
00949 
00950   if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
00951     {
00952       puts ("could not seek in test file");
00953       exit (1);
00954     }
00955 
00956 #if __USE_FORTIFY_LEVEL >= 1
00957   CHK_FAIL_START
00958   if (gets (buf) != buf)
00959     FAIL ();
00960   CHK_FAIL_END
00961 #endif
00962 
00963   /* Check whether missing N$ formats are detected.  */
00964   CHK_FAIL2_START
00965   printf ("%3$d\n", 1, 2, 3, 4);
00966   CHK_FAIL2_END
00967 
00968   CHK_FAIL2_START
00969   fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
00970   CHK_FAIL2_END
00971 
00972   CHK_FAIL2_START
00973   sprintf (buf, "%3$d\n", 1, 2, 3, 4);
00974   CHK_FAIL2_END
00975 
00976   CHK_FAIL2_START
00977   snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
00978   CHK_FAIL2_END
00979 
00980   int sp[2];
00981   if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
00982     FAIL ();
00983   else
00984     {
00985       const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
00986       if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
00987          != strlen (sendstr))
00988        FAIL ();
00989 
00990       char recvbuf[12];
00991       if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
00992          != sizeof recvbuf
00993          || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
00994        FAIL ();
00995 
00996       if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
00997          != sizeof recvbuf - 7
00998          || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
00999        FAIL ();
01000 
01001 #if __USE_FORTIFY_LEVEL >= 1
01002       CHK_FAIL_START
01003       if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
01004          != sizeof recvbuf)
01005        FAIL ();
01006       CHK_FAIL_END
01007 
01008       CHK_FAIL_START
01009       if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
01010          != sizeof recvbuf - 3)
01011        FAIL ();
01012       CHK_FAIL_END
01013 #endif
01014 
01015       socklen_t sl;
01016       struct sockaddr_un sa_un;
01017 
01018       sl = sizeof (sa_un);
01019       if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
01020                   (struct sockaddr *) &sa_un, &sl)
01021          != sizeof recvbuf
01022          || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
01023        FAIL ();
01024 
01025       sl = sizeof (sa_un);
01026       if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
01027                   (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
01028          || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
01029        FAIL ();
01030 
01031 #if __USE_FORTIFY_LEVEL >= 1
01032       CHK_FAIL_START
01033       sl = sizeof (sa_un);
01034       if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
01035                   (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
01036        FAIL ();
01037       CHK_FAIL_END
01038 
01039       CHK_FAIL_START
01040       sl = sizeof (sa_un);
01041       if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
01042                   (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
01043        FAIL ();
01044       CHK_FAIL_END
01045 #endif
01046 
01047       close (sp[0]);
01048       close (sp[1]);
01049     }
01050 
01051   char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
01052   char *enddir = strchr (fname, '\0');
01053   if (mkdtemp (fname) == NULL)
01054     {
01055       printf ("mkdtemp failed: %m\n");
01056       return 1;
01057     }
01058   *enddir = '/';
01059   if (symlink ("bar", fname) != 0)
01060     FAIL ();
01061 
01062   char readlinkbuf[4];
01063   if (readlink (fname, readlinkbuf, 4) != 3
01064       || memcmp (readlinkbuf, "bar", 3) != 0)
01065     FAIL ();
01066   if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
01067       || memcmp (readlinkbuf, "bbar", 4) != 0)
01068     FAIL ();
01069 
01070 #if __USE_FORTIFY_LEVEL >= 1
01071   CHK_FAIL_START
01072   if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
01073     FAIL ();
01074   CHK_FAIL_END
01075 
01076   CHK_FAIL_START
01077   if (readlink (fname, readlinkbuf + 3, 4) != 3)
01078     FAIL ();
01079   CHK_FAIL_END
01080 #endif
01081 
01082   int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
01083   if (tmpfd < 0)
01084     FAIL ();
01085 
01086   if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
01087       || memcmp (readlinkbuf, "bar", 3) != 0)
01088     FAIL ();
01089   if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
01090                 l0 + 3) != 3
01091       || memcmp (readlinkbuf, "bbar", 4) != 0)
01092     FAIL ();
01093 
01094 #if __USE_FORTIFY_LEVEL >= 1
01095   CHK_FAIL_START
01096   if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
01097                 l0 + 3) != 3)
01098     FAIL ();
01099   CHK_FAIL_END
01100 
01101   CHK_FAIL_START
01102   if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
01103                 4) != 3)
01104     FAIL ();
01105   CHK_FAIL_END
01106 #endif
01107 
01108   close (tmpfd);
01109 
01110   char *cwd1 = getcwd (NULL, 0);
01111   if (cwd1 == NULL)
01112     FAIL ();
01113 
01114   char *cwd2 = getcwd (NULL, 250);
01115   if (cwd2 == NULL)
01116     FAIL ();
01117 
01118   if (cwd1 && cwd2)
01119     {
01120       if (strcmp (cwd1, cwd2) != 0)
01121        FAIL ();
01122 
01123       *enddir = '\0';
01124       if (chdir (fname))
01125        FAIL ();
01126 
01127       char *cwd3 = getcwd (NULL, 0);
01128       if (cwd3 == NULL)
01129        FAIL ();
01130       if (strcmp (fname, cwd3) != 0)
01131        printf ("getcwd after chdir is '%s' != '%s',"
01132               "get{c,}wd tests skipped\n", cwd3, fname);
01133       else
01134        {
01135          char getcwdbuf[sizeof fname - 3];
01136 
01137          char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
01138          if (cwd4 != getcwdbuf
01139              || strcmp (getcwdbuf, fname) != 0)
01140            FAIL ();
01141 
01142          cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
01143          if (cwd4 != getcwdbuf + 1
01144              || getcwdbuf[0] != fname[0]
01145              || strcmp (getcwdbuf + 1, fname) != 0)
01146            FAIL ();
01147 
01148 #if __USE_FORTIFY_LEVEL >= 1
01149          CHK_FAIL_START
01150          if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
01151              != getcwdbuf + 2)
01152            FAIL ();
01153          CHK_FAIL_END
01154 
01155          CHK_FAIL_START
01156          if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
01157              != getcwdbuf + 2)
01158            FAIL ();
01159          CHK_FAIL_END
01160 #endif
01161 
01162          if (getwd (getcwdbuf) != getcwdbuf
01163              || strcmp (getcwdbuf, fname) != 0)
01164            FAIL ();
01165 
01166          if (getwd (getcwdbuf + 1) != getcwdbuf + 1
01167              || strcmp (getcwdbuf + 1, fname) != 0)
01168            FAIL ();
01169 
01170 #if __USE_FORTIFY_LEVEL >= 1
01171          CHK_FAIL_START
01172          if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
01173            FAIL ();
01174          CHK_FAIL_END
01175 #endif
01176        }
01177 
01178       if (chdir (cwd1) != 0)
01179        FAIL ();
01180       free (cwd3);
01181     }
01182 
01183   free (cwd1);
01184   free (cwd2);
01185   *enddir = '/';
01186   if (unlink (fname) != 0)
01187     FAIL ();
01188 
01189   *enddir = '\0';
01190   if (rmdir (fname) != 0)
01191     FAIL ();
01192 
01193 
01194 #if PATH_MAX > 0
01195   char largebuf[PATH_MAX];
01196   char *realres = realpath (".", largebuf);
01197   if (realres != largebuf)
01198     FAIL ();
01199 
01200 # if __USE_FORTIFY_LEVEL >= 1
01201   CHK_FAIL_START
01202   char realbuf[1];
01203   realres = realpath (".", realbuf);
01204   if (realres != realbuf)
01205     FAIL ();
01206   CHK_FAIL_END
01207 # endif
01208 #endif
01209 
01210   if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
01211     {
01212       assert (MB_CUR_MAX <= 10);
01213 
01214       /* First a simple test.  */
01215       char enough[10];
01216       if (wctomb (enough, L'A') != 1)
01217        FAIL ();
01218 
01219 #if __USE_FORTIFY_LEVEL >= 1
01220       /* We know the wchar_t encoding is ISO 10646.  So pick a
01221         character which has a multibyte representation which does not
01222         fit.  */
01223       CHK_FAIL_START
01224       char smallbuf[2];
01225       if (wctomb (smallbuf, L'\x100') != 2)
01226        FAIL ();
01227       CHK_FAIL_END
01228 #endif
01229 
01230       mbstate_t s;
01231       memset (&s, '\0', sizeof (s));
01232       if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
01233        FAIL ();
01234 
01235 #if __USE_FORTIFY_LEVEL >= 1
01236       /* We know the wchar_t encoding is ISO 10646.  So pick a
01237         character which has a multibyte representation which does not
01238         fit.  */
01239       CHK_FAIL_START
01240       char smallbuf[2];
01241       if (wcrtomb (smallbuf, L'\x100', &s) != 2)
01242        FAIL ();
01243       CHK_FAIL_END
01244 #endif
01245 
01246       wchar_t wenough[10];
01247       memset (&s, '\0', sizeof (s));
01248       const char *cp = "A";
01249       if (mbsrtowcs (wenough, &cp, 10, &s) != 1
01250          || wcscmp (wenough, L"A") != 0)
01251        FAIL ();
01252 
01253       cp = "BC";
01254       if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
01255          || wcscmp (wenough, L"BC") != 0)
01256        FAIL ();
01257 
01258 #if __USE_FORTIFY_LEVEL >= 1
01259       CHK_FAIL_START
01260       wchar_t wsmallbuf[2];
01261       cp = "ABC";
01262       mbsrtowcs (wsmallbuf, &cp, 10, &s);
01263       CHK_FAIL_END
01264 #endif
01265 
01266       cp = "A";
01267       if (mbstowcs (wenough, cp, 10) != 1
01268          || wcscmp (wenough, L"A") != 0)
01269        FAIL ();
01270 
01271       cp = "DEF";
01272       if (mbstowcs (wenough, cp, l0 + 10) != 3
01273          || wcscmp (wenough, L"DEF") != 0)
01274        FAIL ();
01275 
01276 #if __USE_FORTIFY_LEVEL >= 1
01277       CHK_FAIL_START
01278       wchar_t wsmallbuf[2];
01279       cp = "ABC";
01280       mbstowcs (wsmallbuf, cp, 10);
01281       CHK_FAIL_END
01282 #endif
01283 
01284       memset (&s, '\0', sizeof (s));
01285       cp = "ABC";
01286       wcscpy (wenough, L"DEF");
01287       if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
01288          || wcscmp (wenough, L"AEF") != 0)
01289        FAIL ();
01290 
01291       cp = "IJ";
01292       if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
01293          || wcscmp (wenough, L"IEF") != 0)
01294        FAIL ();
01295 
01296 #if __USE_FORTIFY_LEVEL >= 1
01297       CHK_FAIL_START
01298       wchar_t wsmallbuf[2];
01299       cp = "ABC";
01300       mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
01301       CHK_FAIL_END
01302 #endif
01303 
01304       memset (&s, '\0', sizeof (s));
01305       const wchar_t *wcp = L"A";
01306       if (wcsrtombs (enough, &wcp, 10, &s) != 1
01307          || strcmp (enough, "A") != 0)
01308        FAIL ();
01309 
01310       wcp = L"BC";
01311       if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
01312          || strcmp (enough, "BC") != 0)
01313        FAIL ();
01314 
01315 #if __USE_FORTIFY_LEVEL >= 1
01316       CHK_FAIL_START
01317       char smallbuf[2];
01318       wcp = L"ABC";
01319       wcsrtombs (smallbuf, &wcp, 10, &s);
01320       CHK_FAIL_END
01321 #endif
01322 
01323       memset (enough, 'Z', sizeof (enough));
01324       wcp = L"EF";
01325       if (wcstombs (enough, wcp, 10) != 2
01326          || strcmp (enough, "EF") != 0)
01327        FAIL ();
01328 
01329       wcp = L"G";
01330       if (wcstombs (enough, wcp, l0 + 10) != 1
01331          || strcmp (enough, "G") != 0)
01332        FAIL ();
01333 
01334 #if __USE_FORTIFY_LEVEL >= 1
01335       CHK_FAIL_START
01336       char smallbuf[2];
01337       wcp = L"ABC";
01338       wcstombs (smallbuf, wcp, 10);
01339       CHK_FAIL_END
01340 #endif
01341 
01342       memset (&s, '\0', sizeof (s));
01343       wcp = L"AB";
01344       if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
01345          || strcmp (enough, "A") != 0)
01346        FAIL ();
01347 
01348       wcp = L"BCD";
01349       if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
01350          || strcmp (enough, "B") != 0)
01351        FAIL ();
01352 
01353 #if __USE_FORTIFY_LEVEL >= 1
01354       CHK_FAIL_START
01355       char smallbuf[2];
01356       wcp = L"ABC";
01357       wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
01358       CHK_FAIL_END
01359 #endif
01360     }
01361   else
01362     {
01363       puts ("cannot set locale");
01364       ret = 1;
01365     }
01366 
01367   fd = posix_openpt (O_RDWR);
01368   if (fd != -1)
01369     {
01370       char enough[1000];
01371       if (ptsname_r (fd, enough, sizeof (enough)) != 0)
01372        FAIL ();
01373 
01374 #if __USE_FORTIFY_LEVEL >= 1
01375       CHK_FAIL_START
01376       char smallbuf[2];
01377       if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
01378        FAIL ();
01379       CHK_FAIL_END
01380 #endif
01381       close (fd);
01382     }
01383 
01384 #if PATH_MAX > 0
01385   confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
01386 # if __USE_FORTIFY_LEVEL >= 1
01387   CHK_FAIL_START
01388   char smallbuf[1];
01389   confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
01390   CHK_FAIL_END
01391 # endif
01392 #endif
01393 
01394   gid_t grpslarge[5];
01395   int ngr = getgroups (5, grpslarge);
01396   asm volatile ("" : : "r" (ngr));
01397 #if __USE_FORTIFY_LEVEL >= 1
01398   CHK_FAIL_START
01399   char smallbuf[1];
01400   ngr = getgroups (5, (gid_t *) smallbuf);
01401   asm volatile ("" : : "r" (ngr));
01402   CHK_FAIL_END
01403 #endif
01404 
01405   fd = open (_PATH_TTY, O_RDONLY);
01406   if (fd != -1)
01407     {
01408       char enough[1000];
01409       if (ttyname_r (fd, enough, sizeof (enough)) != 0)
01410        FAIL ();
01411 
01412 #if __USE_FORTIFY_LEVEL >= 1
01413       CHK_FAIL_START
01414       char smallbuf[2];
01415       if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
01416        FAIL ();
01417       CHK_FAIL_END
01418 #endif
01419       close (fd);
01420     }
01421 
01422   char hostnamelarge[1000];
01423   gethostname (hostnamelarge, sizeof (hostnamelarge));
01424 #if __USE_FORTIFY_LEVEL >= 1
01425   CHK_FAIL_START
01426   char smallbuf[1];
01427   gethostname (smallbuf, sizeof (hostnamelarge));
01428   CHK_FAIL_END
01429 #endif
01430 
01431   char loginlarge[1000];
01432   getlogin_r (loginlarge, sizeof (hostnamelarge));
01433 #if __USE_FORTIFY_LEVEL >= 1
01434   CHK_FAIL_START
01435   char smallbuf[1];
01436   getlogin_r (smallbuf, sizeof (loginlarge));
01437   CHK_FAIL_END
01438 #endif
01439 
01440   char domainnamelarge[1000];
01441   int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
01442   asm volatile ("" : : "r" (res));
01443 #if __USE_FORTIFY_LEVEL >= 1
01444   CHK_FAIL_START
01445   char smallbuf[1];
01446   res = getdomainname (smallbuf, sizeof (domainnamelarge));
01447   asm volatile ("" : : "r" (res));
01448   CHK_FAIL_END
01449 #endif
01450 
01451   return ret;
01452 }