PageRenderTime 63ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/eglibc-2.14/libc/debug/tst-chk1.c

https://github.com/Scorpiion/Renux_cross_eglibc
C | 1459 lines | 1132 code | 261 blank | 66 comment | 467 complexity | 0570a24859f97fc286a0f96666200b1d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. /* Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <assert.h>
  17. #include <fcntl.h>
  18. #include <locale.h>
  19. #include <obstack.h>
  20. #include <paths.h>
  21. #include <setjmp.h>
  22. #include <signal.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <wchar.h>
  28. #include <sys/socket.h>
  29. #include <sys/un.h>
  30. #include <gnu/option-groups.h>
  31. #define obstack_chunk_alloc malloc
  32. #define obstack_chunk_free free
  33. char *temp_filename;
  34. static void do_prepare (void);
  35. static int do_test (void);
  36. #define PREPARE(argc, argv) do_prepare ()
  37. #define TEST_FUNCTION do_test ()
  38. #include "../test-skeleton.c"
  39. static void
  40. do_prepare (void)
  41. {
  42. int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
  43. if (temp_fd == -1)
  44. {
  45. printf ("cannot create temporary file: %m\n");
  46. exit (1);
  47. }
  48. const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
  49. if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
  50. {
  51. puts ("could not write test strings into file");
  52. unlink (temp_filename);
  53. exit (1);
  54. }
  55. }
  56. volatile int chk_fail_ok;
  57. volatile int ret;
  58. jmp_buf chk_fail_buf;
  59. static void
  60. handler (int sig)
  61. {
  62. if (chk_fail_ok)
  63. {
  64. chk_fail_ok = 0;
  65. longjmp (chk_fail_buf, 1);
  66. }
  67. else
  68. _exit (127);
  69. }
  70. char buf[10];
  71. wchar_t wbuf[10];
  72. volatile size_t l0;
  73. volatile char *p;
  74. volatile wchar_t *wp;
  75. const char *str1 = "JIHGFEDCBA";
  76. const char *str2 = "F";
  77. const char *str3 = "%s%n%s%n";
  78. const char *str4 = "Hello, ";
  79. const char *str5 = "World!\n";
  80. const wchar_t *wstr1 = L"JIHGFEDCBA";
  81. const wchar_t *wstr2 = L"F";
  82. const wchar_t *wstr3 = L"%s%n%s%n";
  83. const wchar_t *wstr4 = L"Hello, ";
  84. const wchar_t *wstr5 = L"World!\n";
  85. char buf2[10] = "%s";
  86. int num1 = 67;
  87. int num2 = 987654;
  88. #define FAIL() \
  89. do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
  90. #define CHK_FAIL_START \
  91. chk_fail_ok = 1; \
  92. if (! setjmp (chk_fail_buf)) \
  93. {
  94. #define CHK_FAIL_END \
  95. chk_fail_ok = 0; \
  96. FAIL (); \
  97. }
  98. #if __USE_FORTIFY_LEVEL >= 2 && (!defined __cplusplus || defined __va_arg_pack)
  99. #define CHK_FAIL2_START CHK_FAIL_START
  100. #define CHK_FAIL2_END CHK_FAIL_END
  101. #else
  102. #define CHK_FAIL2_START
  103. #define CHK_FAIL2_END
  104. #endif
  105. static int
  106. do_test (void)
  107. {
  108. struct sigaction sa;
  109. sa.sa_handler = handler;
  110. sa.sa_flags = 0;
  111. sigemptyset (&sa.sa_mask);
  112. sigaction (SIGABRT, &sa, NULL);
  113. /* Avoid all the buffer overflow messages on stderr. */
  114. int fd = open (_PATH_DEVNULL, O_WRONLY);
  115. if (fd == -1)
  116. close (STDERR_FILENO);
  117. else
  118. {
  119. dup2 (fd, STDERR_FILENO);
  120. close (fd);
  121. }
  122. setenv ("LIBC_FATAL_STDERR_", "1", 1);
  123. struct A { char buf1[9]; char buf2[1]; } a;
  124. struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
  125. printf ("Test checking routines at fortify level %d\n",
  126. #ifdef __USE_FORTIFY_LEVEL
  127. (int) __USE_FORTIFY_LEVEL
  128. #else
  129. 0
  130. #endif
  131. );
  132. #if defined __USE_FORTIFY_LEVEL && !defined __extern_always_inline
  133. printf ("Test skipped");
  134. if (l0 == 0)
  135. return 0;
  136. #endif
  137. /* These ops can be done without runtime checking of object size. */
  138. memcpy (buf, "abcdefghij", 10);
  139. memmove (buf + 1, buf, 9);
  140. if (memcmp (buf, "aabcdefghi", 10))
  141. FAIL ();
  142. if (mempcpy (buf + 5, "abcde", 5) != buf + 10
  143. || memcmp (buf, "aabcdabcde", 10))
  144. FAIL ();
  145. memset (buf + 8, 'j', 2);
  146. if (memcmp (buf, "aabcdabcjj", 10))
  147. FAIL ();
  148. strcpy (buf + 4, "EDCBA");
  149. if (memcmp (buf, "aabcEDCBA", 10))
  150. FAIL ();
  151. if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
  152. FAIL ();
  153. strncpy (buf + 6, "X", 4);
  154. if (memcmp (buf, "aabcEDX\0\0", 10))
  155. FAIL ();
  156. if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
  157. FAIL ();
  158. if (snprintf (buf + 7, 3, "%s", "987654") != 6
  159. || memcmp (buf, "aabcEDX98", 10))
  160. FAIL ();
  161. /* These ops need runtime checking, but shouldn't __chk_fail. */
  162. memcpy (buf, "abcdefghij", l0 + 10);
  163. memmove (buf + 1, buf, l0 + 9);
  164. if (memcmp (buf, "aabcdefghi", 10))
  165. FAIL ();
  166. if (mempcpy (buf + 5, "abcde", l0 + 5) != buf + 10
  167. || memcmp (buf, "aabcdabcde", 10))
  168. FAIL ();
  169. memset (buf + 8, 'j', l0 + 2);
  170. if (memcmp (buf, "aabcdabcjj", 10))
  171. FAIL ();
  172. strcpy (buf + 4, str1 + 5);
  173. if (memcmp (buf, "aabcEDCBA", 10))
  174. FAIL ();
  175. if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
  176. FAIL ();
  177. strncpy (buf + 6, "X", l0 + 4);
  178. if (memcmp (buf, "aabcEDX\0\0", 10))
  179. FAIL ();
  180. if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
  181. || memcmp (buf, "aabcEcd\0\0", 10))
  182. FAIL ();
  183. if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
  184. FAIL ();
  185. if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
  186. FAIL ();
  187. buf[l0 + 8] = '\0';
  188. strcat (buf, "A");
  189. if (memcmp (buf, "aabcEcd9A", 10))
  190. FAIL ();
  191. buf[l0 + 7] = '\0';
  192. strncat (buf, "ZYXWV", l0 + 2);
  193. if (memcmp (buf, "aabcEcdZY", 10))
  194. FAIL ();
  195. memcpy (a.buf1, "abcdefghij", l0 + 10);
  196. memmove (a.buf1 + 1, a.buf1, l0 + 9);
  197. if (memcmp (a.buf1, "aabcdefghi", 10))
  198. FAIL ();
  199. if (mempcpy (a.buf1 + 5, "abcde", l0 + 5) != a.buf1 + 10
  200. || memcmp (a.buf1, "aabcdabcde", 10))
  201. FAIL ();
  202. memset (a.buf1 + 8, 'j', l0 + 2);
  203. if (memcmp (a.buf1, "aabcdabcjj", 10))
  204. FAIL ();
  205. #if __USE_FORTIFY_LEVEL < 2
  206. /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
  207. and sufficient GCC support, as the string operations overflow
  208. from a.buf1 into a.buf2. */
  209. strcpy (a.buf1 + 4, str1 + 5);
  210. if (memcmp (a.buf1, "aabcEDCBA", 10))
  211. FAIL ();
  212. if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
  213. || memcmp (a.buf1, "aabcEDCBF", 10))
  214. FAIL ();
  215. strncpy (a.buf1 + 6, "X", l0 + 4);
  216. if (memcmp (a.buf1, "aabcEDX\0\0", 10))
  217. FAIL ();
  218. if (sprintf (a.buf1 + 7, "%d", num1) != 2
  219. || memcmp (a.buf1, "aabcEDX67", 10))
  220. FAIL ();
  221. if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
  222. || memcmp (a.buf1, "aabcEDX98", 10))
  223. FAIL ();
  224. a.buf1[l0 + 8] = '\0';
  225. strcat (a.buf1, "A");
  226. if (memcmp (a.buf1, "aabcEDX9A", 10))
  227. FAIL ();
  228. a.buf1[l0 + 7] = '\0';
  229. strncat (a.buf1, "ZYXWV", l0 + 2);
  230. if (memcmp (a.buf1, "aabcEDXZY", 10))
  231. FAIL ();
  232. #endif
  233. #if __USE_FORTIFY_LEVEL >= 1
  234. /* Now check if all buffer overflows are caught at runtime. */
  235. CHK_FAIL_START
  236. memcpy (buf + 1, "abcdefghij", l0 + 10);
  237. CHK_FAIL_END
  238. CHK_FAIL_START
  239. memmove (buf + 2, buf + 1, l0 + 9);
  240. CHK_FAIL_END
  241. CHK_FAIL_START
  242. p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
  243. CHK_FAIL_END
  244. CHK_FAIL_START
  245. memset (buf + 9, 'j', l0 + 2);
  246. CHK_FAIL_END
  247. CHK_FAIL_START
  248. strcpy (buf + 5, str1 + 5);
  249. CHK_FAIL_END
  250. CHK_FAIL_START
  251. p = stpcpy (buf + 9, str2);
  252. CHK_FAIL_END
  253. CHK_FAIL_START
  254. strncpy (buf + 7, "X", l0 + 4);
  255. CHK_FAIL_END
  256. CHK_FAIL_START
  257. stpncpy (buf + 6, "cd", l0 + 5);
  258. CHK_FAIL_END
  259. # if !defined __cplusplus || defined __va_arg_pack
  260. CHK_FAIL_START
  261. sprintf (buf + 8, "%d", num1);
  262. CHK_FAIL_END
  263. CHK_FAIL_START
  264. snprintf (buf + 8, l0 + 3, "%d", num2);
  265. CHK_FAIL_END
  266. #if __OPTION_POSIX_C_LANG_WIDE_CHAR
  267. CHK_FAIL_START
  268. swprintf (wbuf + 8, 3, L"%d", num1);
  269. CHK_FAIL_END
  270. CHK_FAIL_START
  271. swprintf (wbuf + 8, l0 + 3, L"%d", num1);
  272. CHK_FAIL_END
  273. #endif /* __OPTION_POSIX_C_LANG_WIDE_CHAR */
  274. # endif
  275. memcpy (buf, str1 + 2, l0 + 9);
  276. CHK_FAIL_START
  277. strcat (buf, "AB");
  278. CHK_FAIL_END
  279. memcpy (buf, str1 + 3, l0 + 8);
  280. CHK_FAIL_START
  281. strncat (buf, "ZYXWV", l0 + 3);
  282. CHK_FAIL_END
  283. CHK_FAIL_START
  284. memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
  285. CHK_FAIL_END
  286. CHK_FAIL_START
  287. memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
  288. CHK_FAIL_END
  289. CHK_FAIL_START
  290. p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
  291. CHK_FAIL_END
  292. CHK_FAIL_START
  293. memset (a.buf1 + 9, 'j', l0 + 2);
  294. CHK_FAIL_END
  295. # if __USE_FORTIFY_LEVEL >= 2
  296. # define O 0
  297. # else
  298. # define O 1
  299. # endif
  300. CHK_FAIL_START
  301. strcpy (a.buf1 + (O + 4), str1 + 5);
  302. CHK_FAIL_END
  303. CHK_FAIL_START
  304. p = stpcpy (a.buf1 + (O + 8), str2);
  305. CHK_FAIL_END
  306. CHK_FAIL_START
  307. strncpy (a.buf1 + (O + 6), "X", l0 + 4);
  308. CHK_FAIL_END
  309. # if !defined __cplusplus || defined __va_arg_pack
  310. CHK_FAIL_START
  311. sprintf (a.buf1 + (O + 7), "%d", num1);
  312. CHK_FAIL_END
  313. CHK_FAIL_START
  314. snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
  315. CHK_FAIL_END
  316. # endif
  317. memcpy (a.buf1, str1 + (3 - O), l0 + 8 + O);
  318. CHK_FAIL_START
  319. strcat (a.buf1, "AB");
  320. CHK_FAIL_END
  321. memcpy (a.buf1, str1 + (4 - O), l0 + 7 + O);
  322. CHK_FAIL_START
  323. strncat (a.buf1, "ZYXWV", l0 + 3);
  324. CHK_FAIL_END
  325. #endif
  326. #if __OPTION_POSIX_C_LANG_WIDE_CHAR
  327. /* These ops can be done without runtime checking of object size. */
  328. wmemcpy (wbuf, L"abcdefghij", 10);
  329. wmemmove (wbuf + 1, wbuf, 9);
  330. if (wmemcmp (wbuf, L"aabcdefghi", 10))
  331. FAIL ();
  332. if (wmempcpy (wbuf + 5, L"abcde", 5) != wbuf + 10
  333. || wmemcmp (wbuf, L"aabcdabcde", 10))
  334. FAIL ();
  335. wmemset (wbuf + 8, L'j', 2);
  336. if (wmemcmp (wbuf, L"aabcdabcjj", 10))
  337. FAIL ();
  338. wcscpy (wbuf + 4, L"EDCBA");
  339. if (wmemcmp (wbuf, L"aabcEDCBA", 10))
  340. FAIL ();
  341. if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
  342. FAIL ();
  343. wcsncpy (wbuf + 6, L"X", 4);
  344. if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
  345. FAIL ();
  346. if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
  347. || wmemcmp (wbuf, L"aabcEDX98", 10))
  348. FAIL ();
  349. if (swprintf (wbuf + 7, 3, L"64") != 2
  350. || wmemcmp (wbuf, L"aabcEDX64", 10))
  351. FAIL ();
  352. /* These ops need runtime checking, but shouldn't __chk_fail. */
  353. wmemcpy (wbuf, L"abcdefghij", l0 + 10);
  354. wmemmove (wbuf + 1, wbuf, l0 + 9);
  355. if (wmemcmp (wbuf, L"aabcdefghi", 10))
  356. FAIL ();
  357. if (wmempcpy (wbuf + 5, L"abcde", l0 + 5) != wbuf + 10
  358. || wmemcmp (wbuf, L"aabcdabcde", 10))
  359. FAIL ();
  360. wmemset (wbuf + 8, L'j', l0 + 2);
  361. if (wmemcmp (wbuf, L"aabcdabcjj", 10))
  362. FAIL ();
  363. wcscpy (wbuf + 4, wstr1 + 5);
  364. if (wmemcmp (wbuf, L"aabcEDCBA", 10))
  365. FAIL ();
  366. if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
  367. FAIL ();
  368. wcsncpy (wbuf + 6, L"X", l0 + 4);
  369. if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
  370. FAIL ();
  371. if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
  372. || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
  373. FAIL ();
  374. if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
  375. || wmemcmp (wbuf, L"aabcEcd98", 10))
  376. FAIL ();
  377. wbuf[l0 + 8] = L'\0';
  378. wcscat (wbuf, L"A");
  379. if (wmemcmp (wbuf, L"aabcEcd9A", 10))
  380. FAIL ();
  381. wbuf[l0 + 7] = L'\0';
  382. wcsncat (wbuf, L"ZYXWV", l0 + 2);
  383. if (wmemcmp (wbuf, L"aabcEcdZY", 10))
  384. FAIL ();
  385. wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
  386. wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
  387. if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
  388. FAIL ();
  389. if (wmempcpy (wa.buf1 + 5, L"abcde", l0 + 5) != wa.buf1 + 10
  390. || wmemcmp (wa.buf1, L"aabcdabcde", 10))
  391. FAIL ();
  392. wmemset (wa.buf1 + 8, L'j', l0 + 2);
  393. if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
  394. FAIL ();
  395. #if __USE_FORTIFY_LEVEL < 2
  396. /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
  397. and sufficient GCC support, as the string operations overflow
  398. from a.buf1 into a.buf2. */
  399. wcscpy (wa.buf1 + 4, wstr1 + 5);
  400. if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
  401. FAIL ();
  402. if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
  403. || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
  404. FAIL ();
  405. wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
  406. if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
  407. FAIL ();
  408. if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
  409. || wmemcmp (wa.buf1, L"aabcEDX98", 10))
  410. FAIL ();
  411. wa.buf1[l0 + 8] = L'\0';
  412. wcscat (wa.buf1, L"A");
  413. if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
  414. FAIL ();
  415. wa.buf1[l0 + 7] = L'\0';
  416. wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
  417. if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
  418. FAIL ();
  419. #endif
  420. #if __USE_FORTIFY_LEVEL >= 1
  421. /* Now check if all buffer overflows are caught at runtime. */
  422. CHK_FAIL_START
  423. wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
  424. CHK_FAIL_END
  425. CHK_FAIL_START
  426. wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
  427. CHK_FAIL_END
  428. CHK_FAIL_START
  429. wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
  430. CHK_FAIL_END
  431. CHK_FAIL_START
  432. wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
  433. CHK_FAIL_END
  434. CHK_FAIL_START
  435. wmemset (wbuf + 9, L'j', l0 + 2);
  436. CHK_FAIL_END
  437. CHK_FAIL_START
  438. wcscpy (wbuf + 5, wstr1 + 5);
  439. CHK_FAIL_END
  440. CHK_FAIL_START
  441. wp = wcpcpy (wbuf + 9, wstr2);
  442. CHK_FAIL_END
  443. CHK_FAIL_START
  444. wcsncpy (wbuf + 7, L"X", l0 + 4);
  445. CHK_FAIL_END
  446. CHK_FAIL_START
  447. wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
  448. CHK_FAIL_END
  449. CHK_FAIL_START
  450. wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
  451. CHK_FAIL_END
  452. CHK_FAIL_START
  453. wcpncpy (wbuf + 6, L"cd", l0 + 5);
  454. CHK_FAIL_END
  455. wmemcpy (wbuf, wstr1 + 2, l0 + 9);
  456. CHK_FAIL_START
  457. wcscat (wbuf, L"AB");
  458. CHK_FAIL_END
  459. wmemcpy (wbuf, wstr1 + 3, l0 + 8);
  460. CHK_FAIL_START
  461. wcsncat (wbuf, L"ZYXWV", l0 + 3);
  462. CHK_FAIL_END
  463. CHK_FAIL_START
  464. wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
  465. CHK_FAIL_END
  466. CHK_FAIL_START
  467. wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
  468. CHK_FAIL_END
  469. CHK_FAIL_START
  470. wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
  471. CHK_FAIL_END
  472. CHK_FAIL_START
  473. wmemset (wa.buf1 + 9, L'j', l0 + 2);
  474. CHK_FAIL_END
  475. #if __USE_FORTIFY_LEVEL >= 2
  476. # define O 0
  477. #else
  478. # define O 1
  479. #endif
  480. CHK_FAIL_START
  481. wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
  482. CHK_FAIL_END
  483. CHK_FAIL_START
  484. wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
  485. CHK_FAIL_END
  486. CHK_FAIL_START
  487. wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
  488. CHK_FAIL_END
  489. wmemcpy (wa.buf1, wstr1 + (3 - O), l0 + 8 + O);
  490. CHK_FAIL_START
  491. wcscat (wa.buf1, L"AB");
  492. CHK_FAIL_END
  493. wmemcpy (wa.buf1, wstr1 + (4 - O), l0 + 7 + O);
  494. CHK_FAIL_START
  495. wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
  496. CHK_FAIL_END
  497. #endif
  498. #endif /* __OPTION_POSIX_C_LANG_WIDE_CHAR */
  499. /* Now checks for %n protection. */
  500. /* Constant literals passed directly are always ok
  501. (even with warnings about possible bugs from GCC). */
  502. int n1, n2;
  503. if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
  504. || n1 != 1 || n2 != 2)
  505. FAIL ();
  506. /* In this case the format string is not known at compile time,
  507. but resides in read-only memory, so is ok. */
  508. if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
  509. || n1 != 1 || n2 != 2)
  510. FAIL ();
  511. strcpy (buf2 + 2, "%n%s%n");
  512. /* When the format string is writable and contains %n,
  513. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  514. CHK_FAIL2_START
  515. if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
  516. FAIL ();
  517. CHK_FAIL2_END
  518. CHK_FAIL2_START
  519. if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
  520. FAIL ();
  521. CHK_FAIL2_END
  522. /* But if there is no %n, even writable format string
  523. should work. */
  524. buf2[6] = '\0';
  525. if (sprintf (buf, buf2 + 4, str2) != 1)
  526. FAIL ();
  527. /* Constant literals passed directly are always ok
  528. (even with warnings about possible bugs from GCC). */
  529. if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
  530. || n1 != 7 || n2 != 14)
  531. FAIL ();
  532. /* In this case the format string is not known at compile time,
  533. but resides in read-only memory, so is ok. */
  534. if (printf (str3, str4, &n1, str5, &n2) != 14
  535. || n1 != 7 || n2 != 14)
  536. FAIL ();
  537. strcpy (buf2 + 2, "%n%s%n");
  538. /* When the format string is writable and contains %n,
  539. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  540. CHK_FAIL2_START
  541. if (printf (buf2, str4, &n1, str5, &n1) != 14)
  542. FAIL ();
  543. CHK_FAIL2_END
  544. /* But if there is no %n, even writable format string
  545. should work. */
  546. buf2[6] = '\0';
  547. if (printf (buf2 + 4, str5) != 7)
  548. FAIL ();
  549. FILE *fp = stdout;
  550. /* Constant literals passed directly are always ok
  551. (even with warnings about possible bugs from GCC). */
  552. if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
  553. || n1 != 7 || n2 != 14)
  554. FAIL ();
  555. /* In this case the format string is not known at compile time,
  556. but resides in read-only memory, so is ok. */
  557. if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
  558. || n1 != 7 || n2 != 14)
  559. FAIL ();
  560. strcpy (buf2 + 2, "%n%s%n");
  561. /* When the format string is writable and contains %n,
  562. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  563. CHK_FAIL2_START
  564. if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
  565. FAIL ();
  566. CHK_FAIL2_END
  567. /* But if there is no %n, even writable format string
  568. should work. */
  569. buf2[6] = '\0';
  570. if (fprintf (fp, buf2 + 4, str5) != 7)
  571. FAIL ();
  572. char *my_ptr = NULL;
  573. strcpy (buf2 + 2, "%n%s%n");
  574. /* When the format string is writable and contains %n,
  575. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  576. CHK_FAIL2_START
  577. if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
  578. FAIL ();
  579. else
  580. free (my_ptr);
  581. CHK_FAIL2_END
  582. struct obstack obs;
  583. obstack_init (&obs);
  584. CHK_FAIL2_START
  585. if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
  586. FAIL ();
  587. CHK_FAIL2_END
  588. obstack_free (&obs, NULL);
  589. my_ptr = NULL;
  590. if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
  591. FAIL ();
  592. else
  593. free (my_ptr);
  594. obstack_init (&obs);
  595. if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
  596. FAIL ();
  597. obstack_free (&obs, NULL);
  598. if (freopen (temp_filename, "r", stdin) == NULL)
  599. {
  600. puts ("could not open temporary file");
  601. exit (1);
  602. }
  603. if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
  604. FAIL ();
  605. if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
  606. FAIL ();
  607. #if __USE_FORTIFY_LEVEL >= 1
  608. CHK_FAIL_START
  609. if (gets (buf) != buf)
  610. FAIL ();
  611. CHK_FAIL_END
  612. #endif
  613. rewind (stdin);
  614. if (fgets (buf, sizeof (buf), stdin) != buf
  615. || memcmp (buf, "abcdefgh\n", 10))
  616. FAIL ();
  617. if (fgets (buf, sizeof (buf), stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
  618. FAIL ();
  619. rewind (stdin);
  620. if (fgets (buf, l0 + sizeof (buf), stdin) != buf
  621. || memcmp (buf, "abcdefgh\n", 10))
  622. FAIL ();
  623. #if __USE_FORTIFY_LEVEL >= 1
  624. CHK_FAIL_START
  625. if (fgets (buf, sizeof (buf) + 1, stdin) != buf)
  626. FAIL ();
  627. CHK_FAIL_END
  628. CHK_FAIL_START
  629. if (fgets (buf, l0 + sizeof (buf) + 1, stdin) != buf)
  630. FAIL ();
  631. CHK_FAIL_END
  632. #endif
  633. rewind (stdin);
  634. if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
  635. || memcmp (buf, "abcdefgh\n", 10))
  636. FAIL ();
  637. if (fgets_unlocked (buf, sizeof (buf), stdin) != buf
  638. || memcmp (buf, "ABCDEFGHI", 10))
  639. FAIL ();
  640. rewind (stdin);
  641. if (fgets_unlocked (buf, l0 + sizeof (buf), stdin) != buf
  642. || memcmp (buf, "abcdefgh\n", 10))
  643. FAIL ();
  644. #if __USE_FORTIFY_LEVEL >= 1
  645. CHK_FAIL_START
  646. if (fgets_unlocked (buf, sizeof (buf) + 1, stdin) != buf)
  647. FAIL ();
  648. CHK_FAIL_END
  649. CHK_FAIL_START
  650. if (fgets_unlocked (buf, l0 + sizeof (buf) + 1, stdin) != buf)
  651. FAIL ();
  652. CHK_FAIL_END
  653. #endif
  654. rewind (stdin);
  655. if (fread (buf, 1, sizeof (buf), stdin) != sizeof (buf)
  656. || memcmp (buf, "abcdefgh\nA", 10))
  657. FAIL ();
  658. if (fread (buf, sizeof (buf), 1, stdin) != 1
  659. || memcmp (buf, "BCDEFGHI\na", 10))
  660. FAIL ();
  661. rewind (stdin);
  662. if (fread (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
  663. || memcmp (buf, "abcdefgh\nA", 10))
  664. FAIL ();
  665. if (fread (buf, sizeof (buf), l0 + 1, stdin) != 1
  666. || memcmp (buf, "BCDEFGHI\na", 10))
  667. FAIL ();
  668. #if __USE_FORTIFY_LEVEL >= 1
  669. CHK_FAIL_START
  670. if (fread (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
  671. FAIL ();
  672. CHK_FAIL_END
  673. CHK_FAIL_START
  674. if (fread (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
  675. FAIL ();
  676. CHK_FAIL_END
  677. #endif
  678. rewind (stdin);
  679. if (fread_unlocked (buf, 1, sizeof (buf), stdin) != sizeof (buf)
  680. || memcmp (buf, "abcdefgh\nA", 10))
  681. FAIL ();
  682. if (fread_unlocked (buf, sizeof (buf), 1, stdin) != 1
  683. || memcmp (buf, "BCDEFGHI\na", 10))
  684. FAIL ();
  685. rewind (stdin);
  686. if (fread_unlocked (buf, 1, 4, stdin) != 4
  687. || memcmp (buf, "abcdFGHI\na", 10))
  688. FAIL ();
  689. if (fread_unlocked (buf, 4, 1, stdin) != 1
  690. || memcmp (buf, "efghFGHI\na", 10))
  691. FAIL ();
  692. rewind (stdin);
  693. if (fread_unlocked (buf, l0 + 1, sizeof (buf), stdin) != sizeof (buf)
  694. || memcmp (buf, "abcdefgh\nA", 10))
  695. FAIL ();
  696. if (fread_unlocked (buf, sizeof (buf), l0 + 1, stdin) != 1
  697. || memcmp (buf, "BCDEFGHI\na", 10))
  698. FAIL ();
  699. #if __USE_FORTIFY_LEVEL >= 1
  700. CHK_FAIL_START
  701. if (fread_unlocked (buf, 1, sizeof (buf) + 1, stdin) != sizeof (buf) + 1)
  702. FAIL ();
  703. CHK_FAIL_END
  704. CHK_FAIL_START
  705. if (fread_unlocked (buf, sizeof (buf) + 1, l0 + 1, stdin) != 1)
  706. FAIL ();
  707. CHK_FAIL_END
  708. #endif
  709. lseek (fileno (stdin), 0, SEEK_SET);
  710. if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
  711. || memcmp (buf, "abcdefgh\n", 9))
  712. FAIL ();
  713. if (read (fileno (stdin), buf, sizeof (buf) - 1) != sizeof (buf) - 1
  714. || memcmp (buf, "ABCDEFGHI", 9))
  715. FAIL ();
  716. lseek (fileno (stdin), 0, SEEK_SET);
  717. if (read (fileno (stdin), buf, l0 + sizeof (buf) - 1) != sizeof (buf) - 1
  718. || memcmp (buf, "abcdefgh\n", 9))
  719. FAIL ();
  720. #if __USE_FORTIFY_LEVEL >= 1
  721. CHK_FAIL_START
  722. if (read (fileno (stdin), buf, sizeof (buf) + 1) != sizeof (buf) + 1)
  723. FAIL ();
  724. CHK_FAIL_END
  725. #endif
  726. if (pread (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
  727. != sizeof (buf) - 1
  728. || memcmp (buf, "\nABCDEFGH", 9))
  729. FAIL ();
  730. if (pread (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
  731. || memcmp (buf, "abcdefgh\n", 9))
  732. FAIL ();
  733. if (pread (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
  734. != sizeof (buf) - 1
  735. || memcmp (buf, "h\nABCDEFG", 9))
  736. FAIL ();
  737. #if __USE_FORTIFY_LEVEL >= 1
  738. CHK_FAIL_START
  739. if (pread (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
  740. != sizeof (buf) + 1)
  741. FAIL ();
  742. CHK_FAIL_END
  743. #endif
  744. if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, sizeof (buf) - 2)
  745. != sizeof (buf) - 1
  746. || memcmp (buf, "\nABCDEFGH", 9))
  747. FAIL ();
  748. if (pread64 (fileno (stdin), buf, sizeof (buf) - 1, 0) != sizeof (buf) - 1
  749. || memcmp (buf, "abcdefgh\n", 9))
  750. FAIL ();
  751. if (pread64 (fileno (stdin), buf, l0 + sizeof (buf) - 1, sizeof (buf) - 3)
  752. != sizeof (buf) - 1
  753. || memcmp (buf, "h\nABCDEFG", 9))
  754. FAIL ();
  755. #if __USE_FORTIFY_LEVEL >= 1
  756. CHK_FAIL_START
  757. if (pread64 (fileno (stdin), buf, sizeof (buf) + 1, 2 * sizeof (buf))
  758. != sizeof (buf) + 1)
  759. FAIL ();
  760. CHK_FAIL_END
  761. #endif
  762. if (freopen (temp_filename, "r", stdin) == NULL)
  763. {
  764. puts ("could not open temporary file");
  765. exit (1);
  766. }
  767. if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
  768. {
  769. puts ("could not seek in test file");
  770. exit (1);
  771. }
  772. #if __USE_FORTIFY_LEVEL >= 1
  773. CHK_FAIL_START
  774. if (gets (buf) != buf)
  775. FAIL ();
  776. CHK_FAIL_END
  777. #endif
  778. /* Check whether missing N$ formats are detected. */
  779. CHK_FAIL2_START
  780. printf ("%3$d\n", 1, 2, 3, 4);
  781. CHK_FAIL2_END
  782. CHK_FAIL2_START
  783. fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
  784. CHK_FAIL2_END
  785. CHK_FAIL2_START
  786. sprintf (buf, "%3$d\n", 1, 2, 3, 4);
  787. CHK_FAIL2_END
  788. CHK_FAIL2_START
  789. snprintf (buf, sizeof (buf), "%3$d\n", 1, 2, 3, 4);
  790. CHK_FAIL2_END
  791. int sp[2];
  792. if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
  793. FAIL ();
  794. else
  795. {
  796. const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
  797. if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
  798. != strlen (sendstr))
  799. FAIL ();
  800. char recvbuf[12];
  801. if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
  802. != sizeof recvbuf
  803. || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
  804. FAIL ();
  805. if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
  806. != sizeof recvbuf - 7
  807. || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
  808. FAIL ();
  809. #if __USE_FORTIFY_LEVEL >= 1
  810. CHK_FAIL_START
  811. if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
  812. != sizeof recvbuf)
  813. FAIL ();
  814. CHK_FAIL_END
  815. CHK_FAIL_START
  816. if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
  817. != sizeof recvbuf - 3)
  818. FAIL ();
  819. CHK_FAIL_END
  820. #endif
  821. socklen_t sl;
  822. struct sockaddr_un sa_un;
  823. sl = sizeof (sa_un);
  824. if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
  825. (struct sockaddr *) &sa_un, &sl)
  826. != sizeof recvbuf
  827. || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
  828. FAIL ();
  829. sl = sizeof (sa_un);
  830. if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
  831. (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
  832. || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
  833. FAIL ();
  834. #if __USE_FORTIFY_LEVEL >= 1
  835. CHK_FAIL_START
  836. sl = sizeof (sa_un);
  837. if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
  838. (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
  839. FAIL ();
  840. CHK_FAIL_END
  841. CHK_FAIL_START
  842. sl = sizeof (sa_un);
  843. if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
  844. (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
  845. FAIL ();
  846. CHK_FAIL_END
  847. #endif
  848. close (sp[0]);
  849. close (sp[1]);
  850. }
  851. char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
  852. char *enddir = strchr (fname, '\0');
  853. if (mkdtemp (fname) == NULL)
  854. {
  855. printf ("mkdtemp failed: %m\n");
  856. return 1;
  857. }
  858. *enddir = '/';
  859. if (symlink ("bar", fname) != 0)
  860. FAIL ();
  861. char readlinkbuf[4];
  862. if (readlink (fname, readlinkbuf, 4) != 3
  863. || memcmp (readlinkbuf, "bar", 3) != 0)
  864. FAIL ();
  865. if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
  866. || memcmp (readlinkbuf, "bbar", 4) != 0)
  867. FAIL ();
  868. #if __USE_FORTIFY_LEVEL >= 1
  869. CHK_FAIL_START
  870. if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
  871. FAIL ();
  872. CHK_FAIL_END
  873. CHK_FAIL_START
  874. if (readlink (fname, readlinkbuf + 3, 4) != 3)
  875. FAIL ();
  876. CHK_FAIL_END
  877. #endif
  878. int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
  879. if (tmpfd < 0)
  880. FAIL ();
  881. if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
  882. || memcmp (readlinkbuf, "bar", 3) != 0)
  883. FAIL ();
  884. if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
  885. l0 + 3) != 3
  886. || memcmp (readlinkbuf, "bbar", 4) != 0)
  887. FAIL ();
  888. #if __USE_FORTIFY_LEVEL >= 1
  889. CHK_FAIL_START
  890. if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
  891. l0 + 3) != 3)
  892. FAIL ();
  893. CHK_FAIL_END
  894. CHK_FAIL_START
  895. if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
  896. 4) != 3)
  897. FAIL ();
  898. CHK_FAIL_END
  899. #endif
  900. close (tmpfd);
  901. char *cwd1 = getcwd (NULL, 0);
  902. if (cwd1 == NULL)
  903. FAIL ();
  904. char *cwd2 = getcwd (NULL, 250);
  905. if (cwd2 == NULL)
  906. FAIL ();
  907. if (cwd1 && cwd2)
  908. {
  909. if (strcmp (cwd1, cwd2) != 0)
  910. FAIL ();
  911. *enddir = '\0';
  912. if (chdir (fname))
  913. FAIL ();
  914. char *cwd3 = getcwd (NULL, 0);
  915. if (cwd3 == NULL)
  916. FAIL ();
  917. if (strcmp (fname, cwd3) != 0)
  918. printf ("getcwd after chdir is '%s' != '%s',"
  919. "get{c,}wd tests skipped\n", cwd3, fname);
  920. else
  921. {
  922. char getcwdbuf[sizeof fname - 3];
  923. char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
  924. if (cwd4 != getcwdbuf
  925. || strcmp (getcwdbuf, fname) != 0)
  926. FAIL ();
  927. cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
  928. if (cwd4 != getcwdbuf + 1
  929. || getcwdbuf[0] != fname[0]
  930. || strcmp (getcwdbuf + 1, fname) != 0)
  931. FAIL ();
  932. #if __USE_FORTIFY_LEVEL >= 1
  933. CHK_FAIL_START
  934. if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
  935. != getcwdbuf + 2)
  936. FAIL ();
  937. CHK_FAIL_END
  938. CHK_FAIL_START
  939. if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
  940. != getcwdbuf + 2)
  941. FAIL ();
  942. CHK_FAIL_END
  943. #endif
  944. if (getwd (getcwdbuf) != getcwdbuf
  945. || strcmp (getcwdbuf, fname) != 0)
  946. FAIL ();
  947. if (getwd (getcwdbuf + 1) != getcwdbuf + 1
  948. || strcmp (getcwdbuf + 1, fname) != 0)
  949. FAIL ();
  950. #if __USE_FORTIFY_LEVEL >= 1
  951. CHK_FAIL_START
  952. if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
  953. FAIL ();
  954. CHK_FAIL_END
  955. #endif
  956. }
  957. if (chdir (cwd1) != 0)
  958. FAIL ();
  959. free (cwd3);
  960. }
  961. free (cwd1);
  962. free (cwd2);
  963. *enddir = '/';
  964. if (unlink (fname) != 0)
  965. FAIL ();
  966. *enddir = '\0';
  967. if (rmdir (fname) != 0)
  968. FAIL ();
  969. #if PATH_MAX > 0
  970. char largebuf[PATH_MAX];
  971. char *realres = realpath (".", largebuf);
  972. if (realres != largebuf)
  973. FAIL ();
  974. # if __USE_FORTIFY_LEVEL >= 1
  975. CHK_FAIL_START
  976. char realbuf[1];
  977. realres = realpath (".", realbuf);
  978. if (realres != realbuf)
  979. FAIL ();
  980. CHK_FAIL_END
  981. # endif
  982. #endif
  983. #if __OPTION_POSIX_C_LANG_WIDE_CHAR
  984. if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
  985. {
  986. assert (MB_CUR_MAX <= 10);
  987. /* First a simple test. */
  988. char enough[10];
  989. if (wctomb (enough, L'A') != 1)
  990. FAIL ();
  991. #if __USE_FORTIFY_LEVEL >= 1
  992. /* We know the wchar_t encoding is ISO 10646. So pick a
  993. character which has a multibyte representation which does not
  994. fit. */
  995. CHK_FAIL_START
  996. char smallbuf[2];
  997. if (wctomb (smallbuf, L'\x100') != 2)
  998. FAIL ();
  999. CHK_FAIL_END
  1000. #endif
  1001. mbstate_t s;
  1002. memset (&s, '\0', sizeof (s));
  1003. if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
  1004. FAIL ();
  1005. #if __USE_FORTIFY_LEVEL >= 1
  1006. /* We know the wchar_t encoding is ISO 10646. So pick a
  1007. character which has a multibyte representation which does not
  1008. fit. */
  1009. CHK_FAIL_START
  1010. char smallbuf[2];
  1011. if (wcrtomb (smallbuf, L'\x100', &s) != 2)
  1012. FAIL ();
  1013. CHK_FAIL_END
  1014. #endif
  1015. wchar_t wenough[10];
  1016. memset (&s, '\0', sizeof (s));
  1017. const char *cp = "A";
  1018. if (mbsrtowcs (wenough, &cp, 10, &s) != 1
  1019. || wcscmp (wenough, L"A") != 0)
  1020. FAIL ();
  1021. cp = "BC";
  1022. if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
  1023. || wcscmp (wenough, L"BC") != 0)
  1024. FAIL ();
  1025. #if __USE_FORTIFY_LEVEL >= 1
  1026. CHK_FAIL_START
  1027. wchar_t wsmallbuf[2];
  1028. cp = "ABC";
  1029. mbsrtowcs (wsmallbuf, &cp, 10, &s);
  1030. CHK_FAIL_END
  1031. #endif
  1032. cp = "A";
  1033. if (mbstowcs (wenough, cp, 10) != 1
  1034. || wcscmp (wenough, L"A") != 0)
  1035. FAIL ();
  1036. cp = "DEF";
  1037. if (mbstowcs (wenough, cp, l0 + 10) != 3
  1038. || wcscmp (wenough, L"DEF") != 0)
  1039. FAIL ();
  1040. #if __USE_FORTIFY_LEVEL >= 1
  1041. CHK_FAIL_START
  1042. wchar_t wsmallbuf[2];
  1043. cp = "ABC";
  1044. mbstowcs (wsmallbuf, cp, 10);
  1045. CHK_FAIL_END
  1046. #endif
  1047. memset (&s, '\0', sizeof (s));
  1048. cp = "ABC";
  1049. wcscpy (wenough, L"DEF");
  1050. if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
  1051. || wcscmp (wenough, L"AEF") != 0)
  1052. FAIL ();
  1053. cp = "IJ";
  1054. if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
  1055. || wcscmp (wenough, L"IEF") != 0)
  1056. FAIL ();
  1057. #if __USE_FORTIFY_LEVEL >= 1
  1058. CHK_FAIL_START
  1059. wchar_t wsmallbuf[2];
  1060. cp = "ABC";
  1061. mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
  1062. CHK_FAIL_END
  1063. #endif
  1064. memset (&s, '\0', sizeof (s));
  1065. const wchar_t *wcp = L"A";
  1066. if (wcsrtombs (enough, &wcp, 10, &s) != 1
  1067. || strcmp (enough, "A") != 0)
  1068. FAIL ();
  1069. wcp = L"BC";
  1070. if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
  1071. || strcmp (enough, "BC") != 0)
  1072. FAIL ();
  1073. #if __USE_FORTIFY_LEVEL >= 1
  1074. CHK_FAIL_START
  1075. char smallbuf[2];
  1076. wcp = L"ABC";
  1077. wcsrtombs (smallbuf, &wcp, 10, &s);
  1078. CHK_FAIL_END
  1079. #endif
  1080. memset (enough, 'Z', sizeof (enough));
  1081. wcp = L"EF";
  1082. if (wcstombs (enough, wcp, 10) != 2
  1083. || strcmp (enough, "EF") != 0)
  1084. FAIL ();
  1085. wcp = L"G";
  1086. if (wcstombs (enough, wcp, l0 + 10) != 1
  1087. || strcmp (enough, "G") != 0)
  1088. FAIL ();
  1089. #if __USE_FORTIFY_LEVEL >= 1
  1090. CHK_FAIL_START
  1091. char smallbuf[2];
  1092. wcp = L"ABC";
  1093. wcstombs (smallbuf, wcp, 10);
  1094. CHK_FAIL_END
  1095. #endif
  1096. memset (&s, '\0', sizeof (s));
  1097. wcp = L"AB";
  1098. if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
  1099. || strcmp (enough, "A") != 0)
  1100. FAIL ();
  1101. wcp = L"BCD";
  1102. if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
  1103. || strcmp (enough, "B") != 0)
  1104. FAIL ();
  1105. #if __USE_FORTIFY_LEVEL >= 1
  1106. CHK_FAIL_START
  1107. char smallbuf[2];
  1108. wcp = L"ABC";
  1109. wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
  1110. CHK_FAIL_END
  1111. #endif
  1112. }
  1113. else
  1114. {
  1115. puts ("cannot set locale");
  1116. ret = 1;
  1117. }
  1118. #endif /* __OPTION_POSIX_C_LANG_WIDE_CHAR */
  1119. fd = posix_openpt (O_RDWR);
  1120. if (fd != -1)
  1121. {
  1122. char enough[1000];
  1123. if (ptsname_r (fd, enough, sizeof (enough)) != 0)
  1124. FAIL ();
  1125. #if __USE_FORTIFY_LEVEL >= 1
  1126. CHK_FAIL_START
  1127. char smallbuf[2];
  1128. if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
  1129. FAIL ();
  1130. CHK_FAIL_END
  1131. #endif
  1132. close (fd);
  1133. }
  1134. #if PATH_MAX > 0
  1135. confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
  1136. # if __USE_FORTIFY_LEVEL >= 1
  1137. CHK_FAIL_START
  1138. char smallbuf[1];
  1139. confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
  1140. CHK_FAIL_END
  1141. # endif
  1142. #endif
  1143. gid_t grpslarge[5];
  1144. int ngr = getgroups (5, grpslarge);
  1145. asm volatile ("" : : "r" (ngr));
  1146. #if __USE_FORTIFY_LEVEL >= 1
  1147. CHK_FAIL_START
  1148. char smallbuf[1];
  1149. ngr = getgroups (5, (gid_t *) smallbuf);
  1150. asm volatile ("" : : "r" (ngr));
  1151. CHK_FAIL_END
  1152. #endif
  1153. fd = open (_PATH_TTY, O_RDONLY);
  1154. if (fd != -1)
  1155. {
  1156. char enough[1000];
  1157. if (ttyname_r (fd, enough, sizeof (enough)) != 0)
  1158. FAIL ();
  1159. #if __USE_FORTIFY_LEVEL >= 1
  1160. CHK_FAIL_START
  1161. char smallbuf[2];
  1162. if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
  1163. FAIL ();
  1164. CHK_FAIL_END
  1165. #endif
  1166. close (fd);
  1167. }
  1168. char hostnamelarge[1000];
  1169. gethostname (hostnamelarge, sizeof (hostnamelarge));
  1170. #if __USE_FORTIFY_LEVEL >= 1
  1171. CHK_FAIL_START
  1172. char smallbuf[1];
  1173. gethostname (smallbuf, sizeof (hostnamelarge));
  1174. CHK_FAIL_END
  1175. #endif
  1176. char loginlarge[1000];
  1177. getlogin_r (loginlarge, sizeof (hostnamelarge));
  1178. #if __USE_FORTIFY_LEVEL >= 1
  1179. CHK_FAIL_START
  1180. char smallbuf[1];
  1181. getlogin_r (smallbuf, sizeof (loginlarge));
  1182. CHK_FAIL_END
  1183. #endif
  1184. char domainnamelarge[1000];
  1185. int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
  1186. asm volatile ("" : : "r" (res));
  1187. #if __USE_FORTIFY_LEVEL >= 1
  1188. CHK_FAIL_START
  1189. char smallbuf[1];
  1190. res = getdomainname (smallbuf, sizeof (domainnamelarge));
  1191. asm volatile ("" : : "r" (res));
  1192. CHK_FAIL_END
  1193. #endif
  1194. return ret;
  1195. }