tst-fortify.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /* Copyright (C) 2004-2026 Free Software Foundation, Inc.
  2. Copyright The GNU Toolchain Authors.
  3. This file is part of the GNU C Library.
  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, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* This file tests gets. Force it to be declared. */
  16. #include <features.h>
  17. #undef __GLIBC_USE_DEPRECATED_GETS
  18. #define __GLIBC_USE_DEPRECATED_GETS 1
  19. #include <assert.h>
  20. #include <fcntl.h>
  21. #include <arpa/inet.h>
  22. #include <limits.h>
  23. #include <locale.h>
  24. #include <obstack.h>
  25. #include <setjmp.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <wchar.h>
  32. #include <sys/poll.h>
  33. #include <sys/select.h>
  34. #include <sys/socket.h>
  35. #include <sys/un.h>
  36. #include <paths.h>
  37. #include <support/temp_file.h>
  38. #include <support/support.h>
  39. #ifndef _GNU_SOURCE
  40. # define MEMPCPY memcpy
  41. # define WMEMPCPY wmemcpy
  42. # define MEMPCPY_RET(x) 0
  43. # define WMEMPCPY_RET(x) 0
  44. #else
  45. # define MEMPCPY mempcpy
  46. # define WMEMPCPY wmempcpy
  47. # define MEMPCPY_RET(x) __builtin_strlen (x)
  48. # define WMEMPCPY_RET(x) wcslen (x)
  49. #endif
  50. #define obstack_chunk_alloc malloc
  51. #define obstack_chunk_free free
  52. static char *temp_filename;
  53. static int temp_fd_dprintf;
  54. static void
  55. do_prepare (int argc, char *argv[])
  56. {
  57. int temp_fd = create_temp_file ("tst-chk1.", &temp_filename);
  58. if (temp_fd == -1)
  59. {
  60. printf ("cannot create temporary file: %m\n");
  61. exit (1);
  62. }
  63. const char *strs = "abcdefgh\nABCDEFGHI\nabcdefghij\nABCDEFGHIJ";
  64. if ((size_t) write (temp_fd, strs, strlen (strs)) != strlen (strs))
  65. {
  66. puts ("could not write test strings into file");
  67. unlink (temp_filename);
  68. exit (1);
  69. }
  70. temp_fd_dprintf = create_temp_file ("tst-chk2.", NULL);
  71. if (temp_fd_dprintf == -1)
  72. {
  73. printf ("cannot create temporary file: %m\n");
  74. exit (1);
  75. }
  76. }
  77. #define PREPARE do_prepare
  78. static volatile int chk_fail_ok;
  79. static volatile int ret;
  80. static sigjmp_buf chk_fail_buf;
  81. static void
  82. handler (int sig)
  83. {
  84. if (chk_fail_ok)
  85. {
  86. chk_fail_ok = 0;
  87. longjmp (chk_fail_buf, 1);
  88. }
  89. else
  90. _exit (127);
  91. }
  92. #if __USE_FORTIFY_LEVEL == 3
  93. volatile size_t buf_size = 10;
  94. #else
  95. char buf[10];
  96. wchar_t wbuf[10];
  97. #define buf_size sizeof (buf)
  98. #endif
  99. static volatile size_t l0;
  100. static volatile char *p;
  101. static volatile wchar_t *wp;
  102. static const char *str1 = "JIHGFEDCBA";
  103. static const char *str2 = "F";
  104. static const char *str3 = "%s%n%s%n";
  105. static const char *str4 = "Hello, ";
  106. static const char *str5 = "World!\n";
  107. static const wchar_t *wstr1 = L"JIHGFEDCBA";
  108. static const wchar_t *wstr2 = L"F";
  109. static const wchar_t *wstr3 = L"%s%n%s%n";
  110. static const wchar_t *wstr4 = L"Hello, ";
  111. static const wchar_t *wstr5 = L"World!\n";
  112. static char buf2[10] = "%s";
  113. static int num1 = 67;
  114. static int num2 = 987654;
  115. #define FAIL() \
  116. do { printf ("Failure on line %d\n", __LINE__); ret = 1; } while (0)
  117. #define CHK_FAIL_START \
  118. chk_fail_ok = 1; \
  119. if (! sigsetjmp (chk_fail_buf, 1)) \
  120. {
  121. #define CHK_FAIL_END \
  122. chk_fail_ok = 0; \
  123. FAIL (); \
  124. }
  125. #if __USE_FORTIFY_LEVEL >= 2
  126. # define CHK_FAIL2_START CHK_FAIL_START
  127. # define CHK_FAIL2_END CHK_FAIL_END
  128. #else
  129. # define CHK_FAIL2_START
  130. # define CHK_FAIL2_END
  131. #endif
  132. static int
  133. do_test (void)
  134. {
  135. #if __USE_FORTIFY_LEVEL == 3
  136. char *buf = (char *) malloc (buf_size);
  137. wchar_t *wbuf = (wchar_t *) malloc (buf_size * sizeof (wchar_t));
  138. #endif
  139. set_fortify_handler (handler);
  140. struct A { char buf1[9]; char buf2[1]; } a;
  141. struct wA { wchar_t buf1[9]; wchar_t buf2[1]; } wa;
  142. printf ("Test checking routines at fortify level %d\n",
  143. #ifdef __USE_FORTIFY_LEVEL
  144. (int) __USE_FORTIFY_LEVEL
  145. #else
  146. 0
  147. #endif
  148. );
  149. #if defined __USE_FORTIFY_LEVEL && !defined __fortify_function
  150. printf ("Test skipped");
  151. if (l0 == 0)
  152. return 0;
  153. #endif
  154. /* These ops can be done without runtime checking of object size. */
  155. memcpy (buf, "abcdefghij", 10);
  156. memmove (buf + 1, buf, 9);
  157. if (memcmp (buf, "aabcdefghi", 10))
  158. FAIL ();
  159. memcpy (buf, "abcdefghij", 10);
  160. bcopy (buf, buf + 1, 9);
  161. if (memcmp (buf, "aabcdefghi", 10))
  162. FAIL ();
  163. if (MEMPCPY (buf + 5, "abcde", 5) != buf + 5 + MEMPCPY_RET ("abcde")
  164. || memcmp (buf, "aabcdabcde", 10))
  165. FAIL ();
  166. memset (buf + 8, 'j', 2);
  167. if (memcmp (buf, "aabcdabcjj", 10))
  168. FAIL ();
  169. bzero (buf + 8, 2);
  170. if (memcmp (buf, "aabcdabc\0\0", 10))
  171. FAIL ();
  172. explicit_bzero (buf + 6, 4);
  173. if (memcmp (buf, "aabcda\0\0\0\0", 10))
  174. FAIL ();
  175. memset_explicit (buf + 5, 0x1234, 3);
  176. if (memcmp (buf, "aabcd444\0\0", 10))
  177. FAIL ();
  178. strcpy (buf + 4, "EDCBA");
  179. if (memcmp (buf, "aabcEDCBA", 10))
  180. FAIL ();
  181. if (stpcpy (buf + 8, "F") != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
  182. FAIL ();
  183. strncpy (buf + 6, "X", 4);
  184. if (memcmp (buf, "aabcEDX\0\0", 10))
  185. FAIL ();
  186. if (sprintf (buf + 7, "%s", "67") != 2 || memcmp (buf, "aabcEDX67", 10))
  187. FAIL ();
  188. if (snprintf (buf + 7, 3, "%s", "987654") != 6
  189. || memcmp (buf, "aabcEDX98", 10))
  190. FAIL ();
  191. /* These ops need runtime checking, but shouldn't __chk_fail. */
  192. memcpy (buf, "abcdefghij", l0 + 10);
  193. memmove (buf + 1, buf, l0 + 9);
  194. if (memcmp (buf, "aabcdefghi", 10))
  195. FAIL ();
  196. memcpy (buf, "abcdefghij", l0 + 10);
  197. bcopy (buf, buf + 1, l0 + 9);
  198. if (memcmp (buf, "aabcdefghi", 10))
  199. FAIL ();
  200. if (MEMPCPY (buf + 5, "abcde", l0 + 5) != buf + 5 + MEMPCPY_RET ("abcde")
  201. || memcmp (buf, "aabcdabcde", 10))
  202. FAIL ();
  203. memset (buf + 8, 'j', l0 + 2);
  204. if (memcmp (buf, "aabcdabcjj", 10))
  205. FAIL ();
  206. bzero (buf + 8, l0 + 2);
  207. if (memcmp (buf, "aabcdabc\0\0", 10))
  208. FAIL ();
  209. explicit_bzero (buf + 6, l0 + 4);
  210. if (memcmp (buf, "aabcda\0\0\0\0", 10))
  211. FAIL ();
  212. memset_explicit (buf + 5, 0x1234, l0 + 3);
  213. if (memcmp (buf, "aabcd444\0\0", 10))
  214. FAIL ();
  215. strcpy (buf + 4, str1 + 5);
  216. if (memcmp (buf, "aabcEDCBA", 10))
  217. FAIL ();
  218. if (stpcpy (buf + 8, str2) != buf + 9 || memcmp (buf, "aabcEDCBF", 10))
  219. FAIL ();
  220. strncpy (buf + 6, "X", l0 + 4);
  221. if (memcmp (buf, "aabcEDX\0\0", 10))
  222. FAIL ();
  223. if (stpncpy (buf + 5, "cd", l0 + 5) != buf + 7
  224. || memcmp (buf, "aabcEcd\0\0", 10))
  225. FAIL ();
  226. if (sprintf (buf + 7, "%d", num1) != 2 || memcmp (buf, "aabcEcd67", 10))
  227. FAIL ();
  228. if (snprintf (buf + 7, 3, "%d", num2) != 6 || memcmp (buf, "aabcEcd98", 10))
  229. FAIL ();
  230. buf[l0 + 8] = '\0';
  231. strcat (buf, "A");
  232. if (memcmp (buf, "aabcEcd9A", 10))
  233. FAIL ();
  234. buf[l0 + 7] = '\0';
  235. strncat (buf, "ZYXWV", l0 + 2);
  236. if (memcmp (buf, "aabcEcdZY", 10))
  237. FAIL ();
  238. /* The following tests are supposed to succeed at all fortify
  239. levels, even though they overflow a.buf1 into a.buf2. */
  240. memcpy (a.buf1, "abcdefghij", l0 + 10);
  241. memmove (a.buf1 + 1, a.buf1, l0 + 9);
  242. if (memcmp (a.buf1, "aabcdefghi", 10))
  243. FAIL ();
  244. memcpy (a.buf1, "abcdefghij", l0 + 10);
  245. bcopy (a.buf1, a.buf1 + 1, l0 + 9);
  246. if (memcmp (a.buf1, "aabcdefghi", 10))
  247. FAIL ();
  248. if (MEMPCPY (a.buf1 + 5, "abcde", l0 + 5)
  249. != a.buf1 + 5 + MEMPCPY_RET ("abcde")
  250. || memcmp (a.buf1, "aabcdabcde", 10))
  251. FAIL ();
  252. memset (a.buf1 + 8, 'j', l0 + 2);
  253. if (memcmp (a.buf1, "aabcdabcjj", 10))
  254. FAIL ();
  255. bzero (a.buf1 + 8, l0 + 2);
  256. if (memcmp (a.buf1, "aabcdabc\0\0", 10))
  257. FAIL ();
  258. explicit_bzero (a.buf1 + 6, l0 + 4);
  259. if (memcmp (a.buf1, "aabcda\0\0\0\0", 10))
  260. FAIL ();
  261. memset_explicit (a.buf1 + 5, 0x1234, l0 + 3);
  262. if (memcmp (a.buf1, "aabcd444\0\0", 10))
  263. FAIL ();
  264. #if __USE_FORTIFY_LEVEL < 2
  265. /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
  266. and sufficient GCC support, as the string operations overflow
  267. from a.buf1 into a.buf2. */
  268. strcpy (a.buf1 + 4, str1 + 5);
  269. if (memcmp (a.buf1, "aabcEDCBA", 10))
  270. FAIL ();
  271. if (stpcpy (a.buf1 + 8, str2) != a.buf1 + 9
  272. || memcmp (a.buf1, "aabcEDCBF", 10))
  273. FAIL ();
  274. strncpy (a.buf1 + 6, "X", l0 + 4);
  275. if (memcmp (a.buf1, "aabcEDX\0\0", 10))
  276. FAIL ();
  277. if (sprintf (a.buf1 + 7, "%d", num1) != 2
  278. || memcmp (a.buf1, "aabcEDX67", 10))
  279. FAIL ();
  280. if (snprintf (a.buf1 + 7, 3, "%d", num2) != 6
  281. || memcmp (a.buf1, "aabcEDX98", 10))
  282. FAIL ();
  283. a.buf1[l0 + 8] = '\0';
  284. strcat (a.buf1, "A");
  285. if (memcmp (a.buf1, "aabcEDX9A", 10))
  286. FAIL ();
  287. a.buf1[l0 + 7] = '\0';
  288. strncat (a.buf1, "ZYXWV", l0 + 2);
  289. if (memcmp (a.buf1, "aabcEDXZY", 10))
  290. FAIL ();
  291. #endif
  292. #if __USE_FORTIFY_LEVEL >= 1
  293. /* Now check if all buffer overflows are caught at runtime.
  294. N.B. All tests involving a length parameter need to be done
  295. twice: once with the length a compile-time constant, once without. */
  296. CHK_FAIL_START
  297. memcpy (buf + 1, "abcdefghij", 10);
  298. CHK_FAIL_END
  299. CHK_FAIL_START
  300. memcpy (buf + 1, "abcdefghij", l0 + 10);
  301. CHK_FAIL_END
  302. CHK_FAIL_START
  303. memmove (buf + 2, buf + 1, 9);
  304. CHK_FAIL_END
  305. CHK_FAIL_START
  306. memmove (buf + 2, buf + 1, l0 + 9);
  307. CHK_FAIL_END
  308. CHK_FAIL_START
  309. bcopy (buf + 1, buf + 2, 9);
  310. CHK_FAIL_END
  311. CHK_FAIL_START
  312. bcopy (buf + 1, buf + 2, l0 + 9);
  313. CHK_FAIL_END
  314. #ifdef _GNU_SOURCE
  315. CHK_FAIL_START
  316. p = (char *) mempcpy (buf + 6, "abcde", 5);
  317. CHK_FAIL_END
  318. CHK_FAIL_START
  319. p = (char *) mempcpy (buf + 6, "abcde", l0 + 5);
  320. CHK_FAIL_END
  321. #endif
  322. CHK_FAIL_START
  323. memset (buf + 9, 'j', 2);
  324. CHK_FAIL_END
  325. CHK_FAIL_START
  326. memset (buf + 9, 'j', l0 + 2);
  327. CHK_FAIL_END
  328. CHK_FAIL_START
  329. bzero (buf + 9, 2);
  330. CHK_FAIL_END
  331. CHK_FAIL_START
  332. bzero (buf + 9, l0 + 2);
  333. CHK_FAIL_END
  334. CHK_FAIL_START
  335. explicit_bzero (buf + 9, 2);
  336. CHK_FAIL_END
  337. CHK_FAIL_START
  338. explicit_bzero (buf + 9, l0 + 2);
  339. CHK_FAIL_END
  340. CHK_FAIL_START
  341. memset_explicit (buf + 9, 1, 2);
  342. CHK_FAIL_END
  343. CHK_FAIL_START
  344. memset_explicit (buf + 9, 4, l0 + 2);
  345. CHK_FAIL_END
  346. CHK_FAIL_START
  347. strcpy (buf + 5, str1 + 5);
  348. CHK_FAIL_END
  349. CHK_FAIL_START
  350. p = stpcpy (buf + 9, str2);
  351. CHK_FAIL_END
  352. CHK_FAIL_START
  353. strncpy (buf + 7, "X", 4);
  354. CHK_FAIL_END
  355. CHK_FAIL_START
  356. strncpy (buf + 7, "X", l0 + 4);
  357. CHK_FAIL_END
  358. CHK_FAIL_START
  359. stpncpy (buf + 6, "cd", 5);
  360. CHK_FAIL_END
  361. CHK_FAIL_START
  362. stpncpy (buf + 6, "cd", l0 + 5);
  363. CHK_FAIL_END
  364. CHK_FAIL_START
  365. sprintf (buf + 8, "%d", num1);
  366. CHK_FAIL_END
  367. CHK_FAIL_START
  368. snprintf (buf + 8, 3, "%d", num2);
  369. CHK_FAIL_END
  370. CHK_FAIL_START
  371. snprintf (buf + 8, l0 + 3, "%d", num2);
  372. CHK_FAIL_END
  373. CHK_FAIL_START
  374. swprintf (wbuf + 8, 3, L"%d", num1);
  375. CHK_FAIL_END
  376. CHK_FAIL_START
  377. swprintf (wbuf + 8, l0 + 3, L"%d", num1);
  378. CHK_FAIL_END
  379. memcpy (buf, str1 + 2, 9);
  380. CHK_FAIL_START
  381. strcat (buf, "AB");
  382. CHK_FAIL_END
  383. memcpy (buf, str1 + 3, 8);
  384. CHK_FAIL_START
  385. strncat (buf, "ZYXWV", 3);
  386. CHK_FAIL_END
  387. memcpy (buf, str1 + 3, 8);
  388. CHK_FAIL_START
  389. strncat (buf, "ZYXWV", l0 + 3);
  390. CHK_FAIL_END
  391. CHK_FAIL_START
  392. memcpy (a.buf1 + 1, "abcdefghij", 10);
  393. CHK_FAIL_END
  394. CHK_FAIL_START
  395. memcpy (a.buf1 + 1, "abcdefghij", l0 + 10);
  396. CHK_FAIL_END
  397. CHK_FAIL_START
  398. memmove (a.buf1 + 2, a.buf1 + 1, 9);
  399. CHK_FAIL_END
  400. CHK_FAIL_START
  401. memmove (a.buf1 + 2, a.buf1 + 1, l0 + 9);
  402. CHK_FAIL_END
  403. CHK_FAIL_START
  404. bcopy (a.buf1 + 1, a.buf1 + 2, 9);
  405. CHK_FAIL_END
  406. CHK_FAIL_START
  407. bcopy (a.buf1 + 1, a.buf1 + 2, l0 + 9);
  408. CHK_FAIL_END
  409. #ifdef _GNU_SOURCE
  410. CHK_FAIL_START
  411. p = (char *) mempcpy (a.buf1 + 6, "abcde", 5);
  412. CHK_FAIL_END
  413. CHK_FAIL_START
  414. p = (char *) mempcpy (a.buf1 + 6, "abcde", l0 + 5);
  415. CHK_FAIL_END
  416. #endif
  417. CHK_FAIL_START
  418. memset (a.buf1 + 9, 'j', 2);
  419. CHK_FAIL_END
  420. CHK_FAIL_START
  421. memset (a.buf1 + 9, 'j', l0 + 2);
  422. CHK_FAIL_END
  423. CHK_FAIL_START
  424. bzero (a.buf1 + 9, 2);
  425. CHK_FAIL_END
  426. CHK_FAIL_START
  427. bzero (a.buf1 + 9, l0 + 2);
  428. CHK_FAIL_END
  429. CHK_FAIL_START
  430. explicit_bzero (a.buf1 + 9, 2);
  431. CHK_FAIL_END
  432. CHK_FAIL_START
  433. explicit_bzero (a.buf1 + 9, l0 + 2);
  434. CHK_FAIL_END
  435. CHK_FAIL_START
  436. memset_explicit (a.buf1 + 9, 0, 2);
  437. CHK_FAIL_END
  438. CHK_FAIL_START
  439. memset_explicit (a.buf1 + 9, 128, l0 + 2);
  440. CHK_FAIL_END
  441. # if __USE_FORTIFY_LEVEL >= 2
  442. # define O 0
  443. # else
  444. # define O 1
  445. # endif
  446. CHK_FAIL_START
  447. strcpy (a.buf1 + (O + 4), str1 + 5);
  448. CHK_FAIL_END
  449. CHK_FAIL_START
  450. p = stpcpy (a.buf1 + (O + 8), str2);
  451. CHK_FAIL_END
  452. CHK_FAIL_START
  453. strncpy (a.buf1 + (O + 6), "X", 4);
  454. CHK_FAIL_END
  455. CHK_FAIL_START
  456. strncpy (a.buf1 + (O + 6), "X", l0 + 4);
  457. CHK_FAIL_END
  458. CHK_FAIL_START
  459. strlcpy (a.buf1 + (O + 6), "X", 4);
  460. CHK_FAIL_END
  461. CHK_FAIL_START
  462. strlcpy (a.buf1 + (O + 6), "X", l0 + 4);
  463. CHK_FAIL_END
  464. {
  465. char *volatile buf2 = buf;
  466. if (strlcpy (buf2, "a", sizeof (buf) + 1) != 1)
  467. FAIL ();
  468. }
  469. CHK_FAIL_START
  470. sprintf (a.buf1 + (O + 7), "%d", num1);
  471. CHK_FAIL_END
  472. CHK_FAIL_START
  473. snprintf (a.buf1 + (O + 7), 3, "%d", num2);
  474. CHK_FAIL_END
  475. CHK_FAIL_START
  476. snprintf (a.buf1 + (O + 7), l0 + 3, "%d", num2);
  477. CHK_FAIL_END
  478. memcpy (a.buf1, str1 + (3 - O), 8 + O);
  479. CHK_FAIL_START
  480. strcat (a.buf1, "AB");
  481. CHK_FAIL_END
  482. memcpy (a.buf1, str1 + (4 - O), 7 + O);
  483. CHK_FAIL_START
  484. strncat (a.buf1, "ZYXWV", l0 + 3);
  485. CHK_FAIL_END
  486. memset (a.buf1, 0, sizeof (a.buf1));
  487. CHK_FAIL_START
  488. strlcat (a.buf1 + (O + 6), "X", 4);
  489. CHK_FAIL_END
  490. memset (a.buf1, 0, sizeof (a.buf1));
  491. CHK_FAIL_START
  492. strlcat (a.buf1 + (O + 6), "X", l0 + 4);
  493. CHK_FAIL_END
  494. {
  495. buf[0] = '\0';
  496. char *volatile buf2 = buf;
  497. if (strlcat (buf2, "a", sizeof (buf) + 1) != 1)
  498. FAIL ();
  499. }
  500. #endif
  501. /* These ops can be done without runtime checking of object size. */
  502. wmemcpy (wbuf, L"abcdefghij", 10);
  503. wmemmove (wbuf + 1, wbuf, 9);
  504. if (wmemcmp (wbuf, L"aabcdefghi", 10))
  505. FAIL ();
  506. if (WMEMPCPY (wbuf + 5, L"abcde", 5) != wbuf + 5 + WMEMPCPY_RET (L"abcde")
  507. || wmemcmp (wbuf, L"aabcdabcde", 10))
  508. FAIL ();
  509. wmemset (wbuf + 8, L'j', 2);
  510. if (wmemcmp (wbuf, L"aabcdabcjj", 10))
  511. FAIL ();
  512. wcscpy (wbuf + 4, L"EDCBA");
  513. if (wmemcmp (wbuf, L"aabcEDCBA", 10))
  514. FAIL ();
  515. if (wcpcpy (wbuf + 8, L"F") != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
  516. FAIL ();
  517. wcsncpy (wbuf + 6, L"X", 4);
  518. if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
  519. FAIL ();
  520. if (swprintf (wbuf + 7, 3, L"%ls", L"987654") >= 0
  521. || wmemcmp (wbuf, L"aabcEDX98", 10))
  522. FAIL ();
  523. if (swprintf (wbuf + 7, 3, L"64") != 2
  524. || wmemcmp (wbuf, L"aabcEDX64", 10))
  525. FAIL ();
  526. /* These ops need runtime checking, but shouldn't __chk_fail. */
  527. wmemcpy (wbuf, L"abcdefghij", l0 + 10);
  528. wmemmove (wbuf + 1, wbuf, l0 + 9);
  529. if (wmemcmp (wbuf, L"aabcdefghi", 10))
  530. FAIL ();
  531. if (WMEMPCPY (wbuf + 5, L"abcde", l0 + 5)
  532. != wbuf + 5 + WMEMPCPY_RET (L"abcde")
  533. || wmemcmp (wbuf, L"aabcdabcde", 10))
  534. FAIL ();
  535. wmemset (wbuf + 8, L'j', l0 + 2);
  536. if (wmemcmp (wbuf, L"aabcdabcjj", 10))
  537. FAIL ();
  538. wcscpy (wbuf + 4, wstr1 + 5);
  539. if (wmemcmp (wbuf, L"aabcEDCBA", 10))
  540. FAIL ();
  541. if (wcpcpy (wbuf + 8, wstr2) != wbuf + 9 || wmemcmp (wbuf, L"aabcEDCBF", 10))
  542. FAIL ();
  543. wcsncpy (wbuf + 6, L"X", l0 + 4);
  544. if (wmemcmp (wbuf, L"aabcEDX\0\0", 10))
  545. FAIL ();
  546. if (wcpncpy (wbuf + 5, L"cd", l0 + 5) != wbuf + 7
  547. || wmemcmp (wbuf, L"aabcEcd\0\0", 10))
  548. FAIL ();
  549. if (swprintf (wbuf + 7, 3, L"%d", num2) >= 0
  550. || wmemcmp (wbuf, L"aabcEcd98", 10))
  551. FAIL ();
  552. wbuf[l0 + 8] = L'\0';
  553. wcscat (wbuf, L"A");
  554. if (wmemcmp (wbuf, L"aabcEcd9A", 10))
  555. FAIL ();
  556. wbuf[l0 + 7] = L'\0';
  557. wcsncat (wbuf, L"ZYXWV", l0 + 2);
  558. if (wmemcmp (wbuf, L"aabcEcdZY", 10))
  559. FAIL ();
  560. wmemcpy (wa.buf1, L"abcdefghij", l0 + 10);
  561. wmemmove (wa.buf1 + 1, wa.buf1, l0 + 9);
  562. if (wmemcmp (wa.buf1, L"aabcdefghi", 10))
  563. FAIL ();
  564. if (WMEMPCPY (wa.buf1 + 5, L"abcde", l0 + 5)
  565. != wa.buf1 + 5 + WMEMPCPY_RET (L"abcde")
  566. || wmemcmp (wa.buf1, L"aabcdabcde", 10))
  567. FAIL ();
  568. wmemset (wa.buf1 + 8, L'j', l0 + 2);
  569. if (wmemcmp (wa.buf1, L"aabcdabcjj", 10))
  570. FAIL ();
  571. #if __USE_FORTIFY_LEVEL < 2
  572. /* The following tests are supposed to crash with -D_FORTIFY_SOURCE=2
  573. and sufficient GCC support, as the string operations overflow
  574. from a.buf1 into a.buf2. */
  575. wcscpy (wa.buf1 + 4, wstr1 + 5);
  576. if (wmemcmp (wa.buf1, L"aabcEDCBA", 10))
  577. FAIL ();
  578. if (wcpcpy (wa.buf1 + 8, wstr2) != wa.buf1 + 9
  579. || wmemcmp (wa.buf1, L"aabcEDCBF", 10))
  580. FAIL ();
  581. wcsncpy (wa.buf1 + 6, L"X", l0 + 4);
  582. if (wmemcmp (wa.buf1, L"aabcEDX\0\0", 10))
  583. FAIL ();
  584. if (swprintf (wa.buf1 + 7, 3, L"%d", num2) >= 0
  585. || wmemcmp (wa.buf1, L"aabcEDX98", 10))
  586. FAIL ();
  587. wa.buf1[l0 + 8] = L'\0';
  588. wcscat (wa.buf1, L"A");
  589. if (wmemcmp (wa.buf1, L"aabcEDX9A", 10))
  590. FAIL ();
  591. wa.buf1[l0 + 7] = L'\0';
  592. wcsncat (wa.buf1, L"ZYXWV", l0 + 2);
  593. if (wmemcmp (wa.buf1, L"aabcEDXZY", 10))
  594. FAIL ();
  595. #endif
  596. #if __USE_FORTIFY_LEVEL >= 1
  597. /* Now check if all buffer overflows are caught at runtime.
  598. N.B. All tests involving a length parameter need to be done
  599. twice: once with the length a compile-time constant, once without. */
  600. CHK_FAIL_START
  601. wmemcpy (wbuf + 1, L"abcdefghij", 10);
  602. CHK_FAIL_END
  603. CHK_FAIL_START
  604. wmemcpy (wbuf + 1, L"abcdefghij", l0 + 10);
  605. CHK_FAIL_END
  606. CHK_FAIL_START
  607. wmemcpy (wbuf + 9, L"abcdefghij", 10);
  608. CHK_FAIL_END
  609. CHK_FAIL_START
  610. wmemcpy (wbuf + 9, L"abcdefghij", l0 + 10);
  611. CHK_FAIL_END
  612. CHK_FAIL_START
  613. wmemmove (wbuf + 2, wbuf + 1, 9);
  614. CHK_FAIL_END
  615. CHK_FAIL_START
  616. wmemmove (wbuf + 2, wbuf + 1, l0 + 9);
  617. CHK_FAIL_END
  618. #ifdef _GNU_SOURCE
  619. CHK_FAIL_START
  620. wp = wmempcpy (wbuf + 6, L"abcde", 5);
  621. CHK_FAIL_END
  622. CHK_FAIL_START
  623. wp = wmempcpy (wbuf + 6, L"abcde", l0 + 5);
  624. CHK_FAIL_END
  625. #endif
  626. CHK_FAIL_START
  627. wmemset (wbuf + 9, L'j', 2);
  628. CHK_FAIL_END
  629. CHK_FAIL_START
  630. wmemset (wbuf + 9, L'j', l0 + 2);
  631. CHK_FAIL_END
  632. CHK_FAIL_START
  633. wcscpy (wbuf + 5, wstr1 + 5);
  634. CHK_FAIL_END
  635. CHK_FAIL_START
  636. wp = wcpcpy (wbuf + 9, wstr2);
  637. CHK_FAIL_END
  638. CHK_FAIL_START
  639. wcsncpy (wbuf + 7, L"X", 4);
  640. CHK_FAIL_END
  641. CHK_FAIL_START
  642. wcsncpy (wbuf + 7, L"X", l0 + 4);
  643. CHK_FAIL_END
  644. CHK_FAIL_START
  645. wcsncpy (wbuf + 9, L"XABCDEFGH", 8);
  646. CHK_FAIL_END
  647. CHK_FAIL_START
  648. wcslcpy (wbuf + 7, L"X", 4);
  649. CHK_FAIL_END
  650. CHK_FAIL_START
  651. wcslcpy (wbuf + 7, L"X", l0 + 4);
  652. CHK_FAIL_END
  653. CHK_FAIL_START
  654. wcslcpy (wbuf + 9, L"XABCDEFGH", 8);
  655. CHK_FAIL_END
  656. CHK_FAIL_START
  657. wcpncpy (wbuf + 9, L"XABCDEFGH", 8);
  658. CHK_FAIL_END
  659. CHK_FAIL_START
  660. wcpncpy (wbuf + 6, L"cd", 5);
  661. CHK_FAIL_END
  662. CHK_FAIL_START
  663. wcpncpy (wbuf + 6, L"cd", l0 + 5);
  664. CHK_FAIL_END
  665. wmemcpy (wbuf, wstr1 + 2, 9);
  666. CHK_FAIL_START
  667. wcscat (wbuf, L"AB");
  668. CHK_FAIL_END
  669. wmemcpy (wbuf, wstr1 + 3, 8);
  670. CHK_FAIL_START
  671. wcsncat (wbuf, L"ZYXWV", l0 + 3);
  672. CHK_FAIL_END
  673. wmemcpy (wbuf, wstr1 + 4, 7);
  674. CHK_FAIL_START
  675. wcslcat (wbuf, L"ZYXWV", l0 + 11);
  676. CHK_FAIL_END
  677. CHK_FAIL_START
  678. wmemcpy (wa.buf1 + 1, L"abcdefghij", 10);
  679. CHK_FAIL_END
  680. CHK_FAIL_START
  681. wmemcpy (wa.buf1 + 1, L"abcdefghij", l0 + 10);
  682. CHK_FAIL_END
  683. CHK_FAIL_START
  684. wmemmove (wa.buf1 + 2, wa.buf1 + 1, 9);
  685. CHK_FAIL_END
  686. CHK_FAIL_START
  687. wmemmove (wa.buf1 + 2, wa.buf1 + 1, l0 + 9);
  688. CHK_FAIL_END
  689. #ifdef _GNU_SOURCE
  690. CHK_FAIL_START
  691. wp = wmempcpy (wa.buf1 + 6, L"abcde", 5);
  692. CHK_FAIL_END
  693. CHK_FAIL_START
  694. wp = wmempcpy (wa.buf1 + 6, L"abcde", l0 + 5);
  695. CHK_FAIL_END
  696. #endif
  697. CHK_FAIL_START
  698. wmemset (wa.buf1 + 9, L'j', 2);
  699. CHK_FAIL_END
  700. CHK_FAIL_START
  701. wmemset (wa.buf1 + 9, L'j', l0 + 2);
  702. CHK_FAIL_END
  703. #if __USE_FORTIFY_LEVEL >= 2
  704. # define O 0
  705. #else
  706. # define O 1
  707. #endif
  708. CHK_FAIL_START
  709. wcscpy (wa.buf1 + (O + 4), wstr1 + 5);
  710. CHK_FAIL_END
  711. CHK_FAIL_START
  712. wp = wcpcpy (wa.buf1 + (O + 8), wstr2);
  713. CHK_FAIL_END
  714. CHK_FAIL_START
  715. wcsncpy (wa.buf1 + (O + 6), L"X", 4);
  716. CHK_FAIL_END
  717. CHK_FAIL_START
  718. wcsncpy (wa.buf1 + (O + 6), L"X", l0 + 4);
  719. CHK_FAIL_END
  720. wmemcpy (wa.buf1, wstr1 + (3 - O), 8 + O);
  721. CHK_FAIL_START
  722. wcscat (wa.buf1, L"AB");
  723. CHK_FAIL_END
  724. wmemcpy (wa.buf1, wstr1 + (4 - O), 7 + O);
  725. CHK_FAIL_START
  726. wcsncat (wa.buf1, L"ZYXWV", l0 + 3);
  727. CHK_FAIL_END
  728. #endif
  729. /* Now checks for %n protection. */
  730. /* Constant literals passed directly are always ok
  731. (even with warnings about possible bugs from GCC). */
  732. int n1, n2;
  733. if (sprintf (buf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
  734. || n1 != 1 || n2 != 2)
  735. FAIL ();
  736. /* In this case the format string is not known at compile time,
  737. but resides in read-only memory, so is ok. */
  738. if (snprintf (buf, 4, str3, str2, &n1, str2, &n2) != 2
  739. || n1 != 1 || n2 != 2)
  740. FAIL ();
  741. if (dprintf (temp_fd_dprintf, "%s%n%s%n", str2, &n1, str2, &n2) != 2
  742. || n1 != 1 || n2 != 2)
  743. FAIL ();
  744. strcpy (buf2 + 2, "%n%s%n");
  745. /* When the format string is writable and contains %n,
  746. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  747. CHK_FAIL2_START
  748. if (sprintf (buf, buf2, str2, &n1, str2, &n1) != 2)
  749. FAIL ();
  750. CHK_FAIL2_END
  751. CHK_FAIL2_START
  752. if (snprintf (buf, 3, buf2, str2, &n1, str2, &n1) != 2)
  753. FAIL ();
  754. CHK_FAIL2_END
  755. CHK_FAIL2_START
  756. if (dprintf (temp_fd_dprintf, buf2, str2, &n1, str2, &n1) != 2)
  757. FAIL ();
  758. CHK_FAIL2_END
  759. /* But if there is no %n, even writable format string
  760. should work. */
  761. buf2[6] = '\0';
  762. if (sprintf (buf, buf2 + 4, str2) != 1)
  763. FAIL ();
  764. /* Constant literals passed directly are always ok
  765. (even with warnings about possible bugs from GCC). */
  766. if (printf ("%s%n%s%n", str4, &n1, str5, &n2) != 14
  767. || n1 != 7 || n2 != 14)
  768. FAIL ();
  769. /* In this case the format string is not known at compile time,
  770. but resides in read-only memory, so is ok. */
  771. if (printf (str3, str4, &n1, str5, &n2) != 14
  772. || n1 != 7 || n2 != 14)
  773. FAIL ();
  774. strcpy (buf2 + 2, "%n%s%n");
  775. /* When the format string is writable and contains %n,
  776. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  777. CHK_FAIL2_START
  778. if (printf (buf2, str4, &n1, str5, &n1) != 14)
  779. FAIL ();
  780. CHK_FAIL2_END
  781. /* But if there is no %n, even writable format string
  782. should work. */
  783. buf2[6] = '\0';
  784. if (printf (buf2 + 4, str5) != 7)
  785. FAIL ();
  786. FILE *fp = stdout;
  787. /* Constant literals passed directly are always ok
  788. (even with warnings about possible bugs from GCC). */
  789. if (fprintf (fp, "%s%n%s%n", str4, &n1, str5, &n2) != 14
  790. || n1 != 7 || n2 != 14)
  791. FAIL ();
  792. /* In this case the format string is not known at compile time,
  793. but resides in read-only memory, so is ok. */
  794. if (fprintf (fp, str3, str4, &n1, str5, &n2) != 14
  795. || n1 != 7 || n2 != 14)
  796. FAIL ();
  797. strcpy (buf2 + 2, "%n%s%n");
  798. /* When the format string is writable and contains %n,
  799. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  800. CHK_FAIL2_START
  801. if (fprintf (fp, buf2, str4, &n1, str5, &n1) != 14)
  802. FAIL ();
  803. CHK_FAIL2_END
  804. /* But if there is no %n, even writable format string
  805. should work. */
  806. buf2[6] = '\0';
  807. if (fprintf (fp, buf2 + 4, str5) != 7)
  808. FAIL ();
  809. #ifdef _GNU_SOURCE
  810. char *my_ptr = NULL;
  811. strcpy (buf2 + 2, "%n%s%n");
  812. /* When the format string is writable and contains %n,
  813. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  814. CHK_FAIL2_START
  815. if (asprintf (&my_ptr, buf2, str4, &n1, str5, &n1) != 14)
  816. FAIL ();
  817. else
  818. free (my_ptr);
  819. CHK_FAIL2_END
  820. struct obstack obs;
  821. obstack_init (&obs);
  822. CHK_FAIL2_START
  823. if (obstack_printf (&obs, buf2, str4, &n1, str5, &n1) != 14)
  824. FAIL ();
  825. CHK_FAIL2_END
  826. obstack_free (&obs, NULL);
  827. my_ptr = NULL;
  828. if (asprintf (&my_ptr, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
  829. FAIL ();
  830. else
  831. free (my_ptr);
  832. obstack_init (&obs);
  833. if (obstack_printf (&obs, "%s%n%s%n", str4, &n1, str5, &n1) != 14)
  834. FAIL ();
  835. obstack_free (&obs, NULL);
  836. #endif
  837. if (freopen (temp_filename, "r", stdin) == NULL)
  838. {
  839. puts ("could not open temporary file");
  840. exit (1);
  841. }
  842. if (gets (buf) != buf || memcmp (buf, "abcdefgh", 9))
  843. FAIL ();
  844. if (gets (buf) != buf || memcmp (buf, "ABCDEFGHI", 10))
  845. FAIL ();
  846. #if __USE_FORTIFY_LEVEL >= 1
  847. CHK_FAIL_START
  848. if (gets (buf) != buf)
  849. FAIL ();
  850. CHK_FAIL_END
  851. #endif
  852. rewind (stdin);
  853. if (fgets (buf, buf_size, stdin) != buf
  854. || memcmp (buf, "abcdefgh\n", 10))
  855. FAIL ();
  856. if (fgets (buf, buf_size, stdin) != buf || memcmp (buf, "ABCDEFGHI", 10))
  857. FAIL ();
  858. rewind (stdin);
  859. if (fgets (buf, l0 + buf_size, stdin) != buf
  860. || memcmp (buf, "abcdefgh\n", 10))
  861. FAIL ();
  862. #if __USE_FORTIFY_LEVEL >= 1
  863. CHK_FAIL_START
  864. if (fgets (buf, buf_size + 1, stdin) != buf)
  865. FAIL ();
  866. CHK_FAIL_END
  867. CHK_FAIL_START
  868. if (fgets (buf, l0 + buf_size + 1, stdin) != buf)
  869. FAIL ();
  870. CHK_FAIL_END
  871. #endif
  872. rewind (stdin);
  873. #ifdef _GNU_SOURCE
  874. if (fgets_unlocked (buf, buf_size, stdin) != buf
  875. || memcmp (buf, "abcdefgh\n", 10))
  876. FAIL ();
  877. if (fgets_unlocked (buf, buf_size, stdin) != buf
  878. || memcmp (buf, "ABCDEFGHI", 10))
  879. FAIL ();
  880. rewind (stdin);
  881. if (fgets_unlocked (buf, l0 + buf_size, stdin) != buf
  882. || memcmp (buf, "abcdefgh\n", 10))
  883. FAIL ();
  884. #if __USE_FORTIFY_LEVEL >= 1
  885. CHK_FAIL_START
  886. if (fgets_unlocked (buf, buf_size + 1, stdin) != buf)
  887. FAIL ();
  888. CHK_FAIL_END
  889. CHK_FAIL_START
  890. if (fgets_unlocked (buf, l0 + buf_size + 1, stdin) != buf)
  891. FAIL ();
  892. CHK_FAIL_END
  893. #endif
  894. rewind (stdin);
  895. #endif
  896. if (fread (buf, 1, buf_size, stdin) != buf_size
  897. || memcmp (buf, "abcdefgh\nA", 10))
  898. FAIL ();
  899. if (fread (buf, buf_size, 1, stdin) != 1
  900. || memcmp (buf, "BCDEFGHI\na", 10))
  901. FAIL ();
  902. rewind (stdin);
  903. if (fread (buf, l0 + 1, buf_size, stdin) != buf_size
  904. || memcmp (buf, "abcdefgh\nA", 10))
  905. FAIL ();
  906. if (fread (buf, buf_size, l0 + 1, stdin) != 1
  907. || memcmp (buf, "BCDEFGHI\na", 10))
  908. FAIL ();
  909. #if __USE_FORTIFY_LEVEL >= 1
  910. CHK_FAIL_START
  911. if (fread (buf, 1, buf_size + 1, stdin) != buf_size + 1)
  912. FAIL ();
  913. CHK_FAIL_END
  914. CHK_FAIL_START
  915. if (fread (buf, buf_size + 1, l0 + 1, stdin) != 1)
  916. FAIL ();
  917. CHK_FAIL_END
  918. #endif
  919. rewind (stdin);
  920. if (fread_unlocked (buf, 1, buf_size, stdin) != buf_size
  921. || memcmp (buf, "abcdefgh\nA", 10))
  922. FAIL ();
  923. if (fread_unlocked (buf, buf_size, 1, stdin) != 1
  924. || memcmp (buf, "BCDEFGHI\na", 10))
  925. FAIL ();
  926. rewind (stdin);
  927. if (fread_unlocked (buf, 1, 4, stdin) != 4
  928. || memcmp (buf, "abcdFGHI\na", 10))
  929. FAIL ();
  930. if (fread_unlocked (buf, 4, 1, stdin) != 1
  931. || memcmp (buf, "efghFGHI\na", 10))
  932. FAIL ();
  933. rewind (stdin);
  934. if (fread_unlocked (buf, l0 + 1, buf_size, stdin) != buf_size
  935. || memcmp (buf, "abcdefgh\nA", 10))
  936. FAIL ();
  937. if (fread_unlocked (buf, buf_size, l0 + 1, stdin) != 1
  938. || memcmp (buf, "BCDEFGHI\na", 10))
  939. FAIL ();
  940. #if __USE_FORTIFY_LEVEL >= 1
  941. CHK_FAIL_START
  942. if (fread_unlocked (buf, 1, buf_size + 1, stdin) != buf_size + 1)
  943. FAIL ();
  944. CHK_FAIL_END
  945. CHK_FAIL_START
  946. if (fread_unlocked (buf, buf_size + 1, l0 + 1, stdin) != 1)
  947. FAIL ();
  948. CHK_FAIL_END
  949. #endif
  950. lseek (fileno (stdin), 0, SEEK_SET);
  951. if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
  952. || memcmp (buf, "abcdefgh\n", 9))
  953. FAIL ();
  954. if (read (fileno (stdin), buf, buf_size - 1) != buf_size - 1
  955. || memcmp (buf, "ABCDEFGHI", 9))
  956. FAIL ();
  957. lseek (fileno (stdin), 0, SEEK_SET);
  958. if (read (fileno (stdin), buf, l0 + buf_size - 1) != buf_size - 1
  959. || memcmp (buf, "abcdefgh\n", 9))
  960. FAIL ();
  961. #if __USE_FORTIFY_LEVEL >= 1
  962. CHK_FAIL_START
  963. if (read (fileno (stdin), buf, buf_size + 1) != buf_size + 1)
  964. FAIL ();
  965. CHK_FAIL_END
  966. CHK_FAIL_START
  967. if (read (fileno (stdin), buf, l0 + buf_size + 1) != buf_size + 1)
  968. FAIL ();
  969. CHK_FAIL_END
  970. #endif
  971. if (pread (fileno (stdin), buf, buf_size - 1, buf_size - 2)
  972. != buf_size - 1
  973. || memcmp (buf, "\nABCDEFGH", 9))
  974. FAIL ();
  975. if (pread (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
  976. || memcmp (buf, "abcdefgh\n", 9))
  977. FAIL ();
  978. if (pread (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
  979. != buf_size - 1
  980. || memcmp (buf, "h\nABCDEFG", 9))
  981. FAIL ();
  982. #if __USE_FORTIFY_LEVEL >= 1
  983. CHK_FAIL_START
  984. if (pread (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
  985. != buf_size + 1)
  986. FAIL ();
  987. CHK_FAIL_END
  988. CHK_FAIL_START
  989. if (pread (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
  990. != buf_size + 1)
  991. FAIL ();
  992. CHK_FAIL_END
  993. #endif
  994. if (pread64 (fileno (stdin), buf, buf_size - 1, buf_size - 2)
  995. != buf_size - 1
  996. || memcmp (buf, "\nABCDEFGH", 9))
  997. FAIL ();
  998. if (pread64 (fileno (stdin), buf, buf_size - 1, 0) != buf_size - 1
  999. || memcmp (buf, "abcdefgh\n", 9))
  1000. FAIL ();
  1001. if (pread64 (fileno (stdin), buf, l0 + buf_size - 1, buf_size - 3)
  1002. != buf_size - 1
  1003. || memcmp (buf, "h\nABCDEFG", 9))
  1004. FAIL ();
  1005. #if __USE_FORTIFY_LEVEL >= 1
  1006. CHK_FAIL_START
  1007. if (pread64 (fileno (stdin), buf, buf_size + 1, 2 * buf_size)
  1008. != buf_size + 1)
  1009. FAIL ();
  1010. CHK_FAIL_END
  1011. CHK_FAIL_START
  1012. if (pread64 (fileno (stdin), buf, l0 + buf_size + 1, 2 * buf_size)
  1013. != buf_size + 1)
  1014. FAIL ();
  1015. CHK_FAIL_END
  1016. #endif
  1017. if (freopen (temp_filename, "r", stdin) == NULL)
  1018. {
  1019. puts ("could not open temporary file");
  1020. exit (1);
  1021. }
  1022. if (fseek (stdin, 9 + 10 + 11, SEEK_SET))
  1023. {
  1024. puts ("could not seek in test file");
  1025. exit (1);
  1026. }
  1027. #if __USE_FORTIFY_LEVEL >= 1
  1028. CHK_FAIL_START
  1029. if (gets (buf) != buf)
  1030. FAIL ();
  1031. CHK_FAIL_END
  1032. #endif
  1033. /* Check whether missing N$ formats are detected. */
  1034. CHK_FAIL2_START
  1035. printf ("%3$d\n", 1, 2, 3, 4);
  1036. CHK_FAIL2_END
  1037. CHK_FAIL2_START
  1038. fprintf (stdout, "%3$d\n", 1, 2, 3, 4);
  1039. CHK_FAIL2_END
  1040. CHK_FAIL2_START
  1041. sprintf (buf, "%3$d\n", 1, 2, 3, 4);
  1042. CHK_FAIL2_END
  1043. CHK_FAIL2_START
  1044. snprintf (buf, buf_size, "%3$d\n", 1, 2, 3, 4);
  1045. CHK_FAIL2_END
  1046. CHK_FAIL2_START
  1047. dprintf (temp_fd_dprintf, "%3$d\n", 1, 2, 3, 4);
  1048. CHK_FAIL2_END
  1049. int sp[2];
  1050. if (socketpair (PF_UNIX, SOCK_STREAM, 0, sp))
  1051. FAIL ();
  1052. else
  1053. {
  1054. const char *sendstr = "abcdefgh\nABCDEFGH\n0123456789\n";
  1055. if ((size_t) send (sp[0], sendstr, strlen (sendstr), 0)
  1056. != strlen (sendstr))
  1057. FAIL ();
  1058. char recvbuf[12];
  1059. if (recv (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK)
  1060. != sizeof recvbuf
  1061. || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
  1062. FAIL ();
  1063. if (recv (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK)
  1064. != sizeof recvbuf - 7
  1065. || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
  1066. FAIL ();
  1067. #if __USE_FORTIFY_LEVEL >= 1
  1068. CHK_FAIL_START
  1069. if (recv (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK)
  1070. != sizeof recvbuf)
  1071. FAIL ();
  1072. CHK_FAIL_END
  1073. CHK_FAIL_START
  1074. if (recv (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK)
  1075. != sizeof recvbuf - 3)
  1076. FAIL ();
  1077. CHK_FAIL_END
  1078. #endif
  1079. socklen_t sl;
  1080. struct sockaddr_un sa_un;
  1081. sl = sizeof (sa_un);
  1082. if (recvfrom (sp[1], recvbuf, sizeof recvbuf, MSG_PEEK,
  1083. (struct sockaddr *) &sa_un, &sl)
  1084. != sizeof recvbuf
  1085. || memcmp (recvbuf, sendstr, sizeof recvbuf) != 0)
  1086. FAIL ();
  1087. sl = sizeof (sa_un);
  1088. if (recvfrom (sp[1], recvbuf + 6, l0 + sizeof recvbuf - 7, MSG_PEEK,
  1089. (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 7
  1090. || memcmp (recvbuf + 6, sendstr, sizeof recvbuf - 7) != 0)
  1091. FAIL ();
  1092. #if __USE_FORTIFY_LEVEL >= 1
  1093. CHK_FAIL_START
  1094. sl = sizeof (sa_un);
  1095. if (recvfrom (sp[1], recvbuf + 1, sizeof recvbuf, MSG_PEEK,
  1096. (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf)
  1097. FAIL ();
  1098. CHK_FAIL_END
  1099. CHK_FAIL_START
  1100. sl = sizeof (sa_un);
  1101. if (recvfrom (sp[1], recvbuf + 4, l0 + sizeof recvbuf - 3, MSG_PEEK,
  1102. (struct sockaddr *) &sa_un, &sl) != sizeof recvbuf - 3)
  1103. FAIL ();
  1104. CHK_FAIL_END
  1105. #endif
  1106. close (sp[0]);
  1107. close (sp[1]);
  1108. }
  1109. char fname[] = "/tmp/tst-chk1-dir-XXXXXX\0foo";
  1110. char *enddir = strchr (fname, '\0');
  1111. if (mkdtemp (fname) == NULL)
  1112. {
  1113. printf ("mkdtemp failed: %m\n");
  1114. return 1;
  1115. }
  1116. *enddir = '/';
  1117. if (symlink ("bar", fname) != 0)
  1118. FAIL ();
  1119. char readlinkbuf[4];
  1120. if (readlink (fname, readlinkbuf, 4) != 3
  1121. || memcmp (readlinkbuf, "bar", 3) != 0)
  1122. FAIL ();
  1123. if (readlink (fname, readlinkbuf + 1, l0 + 3) != 3
  1124. || memcmp (readlinkbuf, "bbar", 4) != 0)
  1125. FAIL ();
  1126. #if __USE_FORTIFY_LEVEL >= 1
  1127. CHK_FAIL_START
  1128. if (readlink (fname, readlinkbuf + 2, l0 + 3) != 3)
  1129. FAIL ();
  1130. CHK_FAIL_END
  1131. CHK_FAIL_START
  1132. if (readlink (fname, readlinkbuf + 3, 4) != 3)
  1133. FAIL ();
  1134. CHK_FAIL_END
  1135. #endif
  1136. int tmpfd = open ("/tmp", O_RDONLY | O_DIRECTORY);
  1137. if (tmpfd < 0)
  1138. FAIL ();
  1139. if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf, 4) != 3
  1140. || memcmp (readlinkbuf, "bar", 3) != 0)
  1141. FAIL ();
  1142. if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 1,
  1143. l0 + 3) != 3
  1144. || memcmp (readlinkbuf, "bbar", 4) != 0)
  1145. FAIL ();
  1146. #if __USE_FORTIFY_LEVEL >= 1
  1147. CHK_FAIL_START
  1148. if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 2,
  1149. l0 + 3) != 3)
  1150. FAIL ();
  1151. CHK_FAIL_END
  1152. CHK_FAIL_START
  1153. if (readlinkat (tmpfd, fname + sizeof ("/tmp/") - 1, readlinkbuf + 3,
  1154. 4) != 3)
  1155. FAIL ();
  1156. CHK_FAIL_END
  1157. #endif
  1158. close (tmpfd);
  1159. char *cwd1 = getcwd (NULL, 0);
  1160. if (cwd1 == NULL)
  1161. FAIL ();
  1162. char *cwd2 = getcwd (NULL, 250);
  1163. if (cwd2 == NULL)
  1164. FAIL ();
  1165. if (cwd1 && cwd2)
  1166. {
  1167. if (strcmp (cwd1, cwd2) != 0)
  1168. FAIL ();
  1169. *enddir = '\0';
  1170. if (chdir (fname))
  1171. FAIL ();
  1172. char *cwd3 = getcwd (NULL, 0);
  1173. if (cwd3 == NULL)
  1174. FAIL ();
  1175. if (strcmp (fname, cwd3) != 0)
  1176. printf ("getcwd after chdir is '%s' != '%s',"
  1177. "get{c,}wd tests skipped\n", cwd3, fname);
  1178. else
  1179. {
  1180. char getcwdbuf[sizeof fname - 3];
  1181. char *cwd4 = getcwd (getcwdbuf, sizeof getcwdbuf);
  1182. if (cwd4 != getcwdbuf
  1183. || strcmp (getcwdbuf, fname) != 0)
  1184. FAIL ();
  1185. cwd4 = getcwd (getcwdbuf + 1, l0 + sizeof getcwdbuf - 1);
  1186. if (cwd4 != getcwdbuf + 1
  1187. || getcwdbuf[0] != fname[0]
  1188. || strcmp (getcwdbuf + 1, fname) != 0)
  1189. FAIL ();
  1190. #if __USE_FORTIFY_LEVEL >= 1
  1191. CHK_FAIL_START
  1192. if (getcwd (getcwdbuf + 2, l0 + sizeof getcwdbuf)
  1193. != getcwdbuf + 2)
  1194. FAIL ();
  1195. CHK_FAIL_END
  1196. CHK_FAIL_START
  1197. if (getcwd (getcwdbuf + 2, sizeof getcwdbuf)
  1198. != getcwdbuf + 2)
  1199. FAIL ();
  1200. CHK_FAIL_END
  1201. #endif
  1202. if (getwd (getcwdbuf) != getcwdbuf
  1203. || strcmp (getcwdbuf, fname) != 0)
  1204. FAIL ();
  1205. if (getwd (getcwdbuf + 1) != getcwdbuf + 1
  1206. || strcmp (getcwdbuf + 1, fname) != 0)
  1207. FAIL ();
  1208. #if __USE_FORTIFY_LEVEL >= 1
  1209. CHK_FAIL_START
  1210. if (getwd (getcwdbuf + 2) != getcwdbuf + 2)
  1211. FAIL ();
  1212. CHK_FAIL_END
  1213. #endif
  1214. }
  1215. if (chdir (cwd1) != 0)
  1216. FAIL ();
  1217. free (cwd3);
  1218. }
  1219. free (cwd1);
  1220. free (cwd2);
  1221. *enddir = '/';
  1222. if (unlink (fname) != 0)
  1223. FAIL ();
  1224. *enddir = '\0';
  1225. if (rmdir (fname) != 0)
  1226. FAIL ();
  1227. #if PATH_MAX > 0
  1228. char largebuf[PATH_MAX];
  1229. char *realres = realpath (".", largebuf);
  1230. if (realres != largebuf)
  1231. FAIL ();
  1232. # if __USE_FORTIFY_LEVEL >= 1
  1233. CHK_FAIL_START
  1234. char realbuf[1];
  1235. realres = realpath (".", realbuf);
  1236. if (realres != realbuf)
  1237. FAIL ();
  1238. CHK_FAIL_END
  1239. # endif
  1240. #endif
  1241. if (setlocale (LC_ALL, "de_DE.UTF-8") != NULL)
  1242. {
  1243. assert (MB_CUR_MAX <= 10);
  1244. /* First a simple test. */
  1245. char enough[10];
  1246. if (wctomb (enough, L'A') != 1)
  1247. FAIL ();
  1248. #if __USE_FORTIFY_LEVEL >= 1
  1249. /* We know the wchar_t encoding is ISO 10646. So pick a
  1250. character which has a multibyte representation which does not
  1251. fit. */
  1252. CHK_FAIL_START
  1253. char smallbuf[2];
  1254. if (wctomb (smallbuf, L'\x100') != 2)
  1255. FAIL ();
  1256. CHK_FAIL_END
  1257. #endif
  1258. mbstate_t s;
  1259. memset (&s, '\0', sizeof (s));
  1260. if (wcrtomb (enough, L'D', &s) != 1 || enough[0] != 'D')
  1261. FAIL ();
  1262. #if __USE_FORTIFY_LEVEL >= 1
  1263. /* We know the wchar_t encoding is ISO 10646. So pick a
  1264. character which has a multibyte representation which does not
  1265. fit. */
  1266. CHK_FAIL_START
  1267. char smallbuf[1];
  1268. if (wcrtomb (smallbuf, L'\x100', &s) != 2)
  1269. FAIL ();
  1270. CHK_FAIL_END
  1271. /* Same input with a large enough buffer and we're good. */
  1272. char bigenoughbuf[2];
  1273. if (wcrtomb (bigenoughbuf, L'\x100', &s) != 2)
  1274. FAIL ();
  1275. #endif
  1276. wchar_t wenough[10];
  1277. memset (&s, '\0', sizeof (s));
  1278. const char *cp = "A";
  1279. if (mbsrtowcs (wenough, &cp, 10, &s) != 1
  1280. || wcscmp (wenough, L"A") != 0)
  1281. FAIL ();
  1282. cp = "BC";
  1283. if (mbsrtowcs (wenough, &cp, l0 + 10, &s) != 2
  1284. || wcscmp (wenough, L"BC") != 0)
  1285. FAIL ();
  1286. #if __USE_FORTIFY_LEVEL >= 1
  1287. CHK_FAIL_START
  1288. wchar_t wsmallbuf[2];
  1289. cp = "ABC";
  1290. mbsrtowcs (wsmallbuf, &cp, 10, &s);
  1291. CHK_FAIL_END
  1292. #endif
  1293. /* Bug 29030 regression check */
  1294. cp = "HelloWorld";
  1295. if (mbsrtowcs (NULL, &cp, (size_t)-1, &s) != 10)
  1296. FAIL ();
  1297. cp = "A";
  1298. if (mbstowcs (wenough, cp, 10) != 1
  1299. || wcscmp (wenough, L"A") != 0)
  1300. FAIL ();
  1301. cp = "DEF";
  1302. if (mbstowcs (wenough, cp, l0 + 10) != 3
  1303. || wcscmp (wenough, L"DEF") != 0)
  1304. FAIL ();
  1305. #if __USE_FORTIFY_LEVEL >= 1
  1306. CHK_FAIL_START
  1307. wchar_t wsmallbuf[2];
  1308. cp = "ABC";
  1309. mbstowcs (wsmallbuf, cp, 10);
  1310. CHK_FAIL_END
  1311. #endif
  1312. memset (&s, '\0', sizeof (s));
  1313. cp = "ABC";
  1314. wcscpy (wenough, L"DEF");
  1315. if (mbsnrtowcs (wenough, &cp, 1, 10, &s) != 1
  1316. || wcscmp (wenough, L"AEF") != 0)
  1317. FAIL ();
  1318. cp = "IJ";
  1319. if (mbsnrtowcs (wenough, &cp, 1, l0 + 10, &s) != 1
  1320. || wcscmp (wenough, L"IEF") != 0)
  1321. FAIL ();
  1322. #if __USE_FORTIFY_LEVEL >= 1
  1323. CHK_FAIL_START
  1324. wchar_t wsmallbuf[2];
  1325. cp = "ABC";
  1326. mbsnrtowcs (wsmallbuf, &cp, 3, 10, &s);
  1327. CHK_FAIL_END
  1328. #endif
  1329. memset (&s, '\0', sizeof (s));
  1330. const wchar_t *wcp = L"A";
  1331. if (wcsrtombs (enough, &wcp, 10, &s) != 1
  1332. || strcmp (enough, "A") != 0)
  1333. FAIL ();
  1334. wcp = L"BC";
  1335. if (wcsrtombs (enough, &wcp, l0 + 10, &s) != 2
  1336. || strcmp (enough, "BC") != 0)
  1337. FAIL ();
  1338. #if __USE_FORTIFY_LEVEL >= 1
  1339. CHK_FAIL_START
  1340. char smallbuf[2];
  1341. wcp = L"ABC";
  1342. wcsrtombs (smallbuf, &wcp, 10, &s);
  1343. CHK_FAIL_END
  1344. #endif
  1345. memset (enough, 'Z', sizeof (enough));
  1346. wcp = L"EF";
  1347. if (wcstombs (enough, wcp, 10) != 2
  1348. || strcmp (enough, "EF") != 0)
  1349. FAIL ();
  1350. wcp = L"G";
  1351. if (wcstombs (enough, wcp, l0 + 10) != 1
  1352. || strcmp (enough, "G") != 0)
  1353. FAIL ();
  1354. #if __USE_FORTIFY_LEVEL >= 1
  1355. CHK_FAIL_START
  1356. char smallbuf[2];
  1357. wcp = L"ABC";
  1358. wcstombs (smallbuf, wcp, 10);
  1359. CHK_FAIL_END
  1360. #endif
  1361. memset (&s, '\0', sizeof (s));
  1362. wcp = L"AB";
  1363. if (wcsnrtombs (enough, &wcp, 1, 10, &s) != 1
  1364. || strcmp (enough, "A") != 0)
  1365. FAIL ();
  1366. wcp = L"BCD";
  1367. if (wcsnrtombs (enough, &wcp, 1, l0 + 10, &s) != 1
  1368. || strcmp (enough, "B") != 0)
  1369. FAIL ();
  1370. #if __USE_FORTIFY_LEVEL >= 1
  1371. CHK_FAIL_START
  1372. char smallbuf[2];
  1373. wcp = L"ABC";
  1374. wcsnrtombs (smallbuf, &wcp, 3, 10, &s);
  1375. CHK_FAIL_END
  1376. #endif
  1377. }
  1378. else
  1379. {
  1380. puts ("cannot set locale");
  1381. ret = 1;
  1382. }
  1383. int fd;
  1384. #ifdef _GNU_SOURCE
  1385. fd = posix_openpt (O_RDWR);
  1386. if (fd != -1)
  1387. {
  1388. char enough[1000];
  1389. if (ptsname_r (fd, enough, sizeof (enough)) != 0)
  1390. FAIL ();
  1391. #if __USE_FORTIFY_LEVEL >= 1
  1392. CHK_FAIL_START
  1393. char smallbuf[2];
  1394. if (ptsname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
  1395. FAIL ();
  1396. CHK_FAIL_END
  1397. #endif
  1398. close (fd);
  1399. }
  1400. #endif
  1401. #if PATH_MAX > 0
  1402. confstr (_CS_GNU_LIBC_VERSION, largebuf, sizeof (largebuf));
  1403. # if __USE_FORTIFY_LEVEL >= 1
  1404. CHK_FAIL_START
  1405. char smallbuf[1];
  1406. confstr (_CS_GNU_LIBC_VERSION, smallbuf, sizeof (largebuf));
  1407. CHK_FAIL_END
  1408. # endif
  1409. #endif
  1410. gid_t grpslarge[5];
  1411. int ngr = getgroups (5, grpslarge);
  1412. asm volatile ("" : : "r" (ngr));
  1413. #if __USE_FORTIFY_LEVEL >= 1
  1414. CHK_FAIL_START
  1415. char smallbuf[1];
  1416. ngr = getgroups (5, (gid_t *) smallbuf);
  1417. asm volatile ("" : : "r" (ngr));
  1418. CHK_FAIL_END
  1419. #endif
  1420. fd = open (_PATH_TTY, O_RDONLY);
  1421. if (fd != -1)
  1422. {
  1423. char enough[1000];
  1424. if (ttyname_r (fd, enough, sizeof (enough)) != 0)
  1425. FAIL ();
  1426. #if __USE_FORTIFY_LEVEL >= 1
  1427. CHK_FAIL_START
  1428. char smallbuf[2];
  1429. if (ttyname_r (fd, smallbuf, sizeof (smallbuf) + 1) == 0)
  1430. FAIL ();
  1431. CHK_FAIL_END
  1432. #endif
  1433. close (fd);
  1434. }
  1435. char hostnamelarge[1000];
  1436. gethostname (hostnamelarge, sizeof (hostnamelarge));
  1437. #if __USE_FORTIFY_LEVEL >= 1
  1438. CHK_FAIL_START
  1439. char smallbuf[1];
  1440. gethostname (smallbuf, sizeof (hostnamelarge));
  1441. CHK_FAIL_END
  1442. #endif
  1443. char loginlarge[1000];
  1444. getlogin_r (loginlarge, sizeof (hostnamelarge));
  1445. #if __USE_FORTIFY_LEVEL >= 1
  1446. CHK_FAIL_START
  1447. char smallbuf[1];
  1448. getlogin_r (smallbuf, sizeof (loginlarge));
  1449. CHK_FAIL_END
  1450. #endif
  1451. char domainnamelarge[1000];
  1452. int res = getdomainname (domainnamelarge, sizeof (domainnamelarge));
  1453. asm volatile ("" : : "r" (res));
  1454. #if __USE_FORTIFY_LEVEL >= 1
  1455. CHK_FAIL_START
  1456. char smallbuf[1];
  1457. res = getdomainname (smallbuf, sizeof (domainnamelarge));
  1458. asm volatile ("" : : "r" (res));
  1459. CHK_FAIL_END
  1460. #endif
  1461. fd_set s;
  1462. FD_ZERO (&s);
  1463. FD_SET (FD_SETSIZE - 1, &s);
  1464. #if __USE_FORTIFY_LEVEL >= 1
  1465. CHK_FAIL_START
  1466. FD_SET (FD_SETSIZE, &s);
  1467. CHK_FAIL_END
  1468. CHK_FAIL_START
  1469. FD_SET (l0 + FD_SETSIZE, &s);
  1470. CHK_FAIL_END
  1471. #endif
  1472. FD_CLR (FD_SETSIZE - 1, &s);
  1473. #if __USE_FORTIFY_LEVEL >= 1
  1474. CHK_FAIL_START
  1475. FD_CLR (FD_SETSIZE, &s);
  1476. CHK_FAIL_END
  1477. CHK_FAIL_START
  1478. FD_SET (l0 + FD_SETSIZE, &s);
  1479. CHK_FAIL_END
  1480. #endif
  1481. FD_ISSET (FD_SETSIZE - 1, &s);
  1482. #if __USE_FORTIFY_LEVEL >= 1
  1483. CHK_FAIL_START
  1484. FD_ISSET (FD_SETSIZE, &s);
  1485. CHK_FAIL_END
  1486. CHK_FAIL_START
  1487. FD_ISSET (l0 + FD_SETSIZE, &s);
  1488. CHK_FAIL_END
  1489. #endif
  1490. struct pollfd fds[1];
  1491. fds[0].fd = STDOUT_FILENO;
  1492. fds[0].events = POLLOUT;
  1493. poll (fds, 1, 0);
  1494. #if __USE_FORTIFY_LEVEL >= 1
  1495. CHK_FAIL_START
  1496. poll (fds, 2, 0);
  1497. CHK_FAIL_END
  1498. CHK_FAIL_START
  1499. poll (fds, l0 + 2, 0);
  1500. CHK_FAIL_END
  1501. #endif
  1502. #ifdef _GNU_SOURCE
  1503. ppoll (fds, 1, NULL, NULL);
  1504. # if __USE_FORTIFY_LEVEL >= 1
  1505. CHK_FAIL_START
  1506. ppoll (fds, 2, NULL, NULL);
  1507. CHK_FAIL_END
  1508. CHK_FAIL_START
  1509. ppoll (fds, l0 + 2, NULL, NULL);
  1510. CHK_FAIL_END
  1511. # endif
  1512. #endif
  1513. struct in6_addr addr6 = {};
  1514. struct in_addr addr = {};
  1515. char addrstr6[INET6_ADDRSTRLEN];
  1516. char addrstr[INET_ADDRSTRLEN];
  1517. if (inet_ntop (AF_INET6, &addr6, addrstr6, sizeof (addrstr6)) == NULL)
  1518. FAIL ();
  1519. if (inet_ntop (AF_INET, &addr, addrstr, sizeof (addrstr)) == NULL)
  1520. FAIL ();
  1521. #if __USE_FORTIFY_LEVEL >= 1
  1522. CHK_FAIL_START
  1523. inet_ntop (AF_INET6, &addr6, buf, INET6_ADDRSTRLEN);
  1524. CHK_FAIL_END
  1525. CHK_FAIL_START
  1526. inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN);
  1527. CHK_FAIL_END
  1528. #endif
  1529. const char *ipv4str = "127.0.0.1";
  1530. const char *ipv6str = "::1";
  1531. if (inet_pton (AF_INET, ipv4str, (void *) &addr) != 1)
  1532. FAIL ();
  1533. if (inet_pton (AF_INET6, ipv6str, (void *) &addr6) != 1)
  1534. FAIL ();
  1535. #if __USE_FORTIFY_LEVEL >= 1
  1536. char smallbuf[2];
  1537. CHK_FAIL_START
  1538. inet_pton (AF_INET, ipv4str, (void *) smallbuf);
  1539. CHK_FAIL_END
  1540. CHK_FAIL_START
  1541. inet_pton (AF_INET6, ipv6str, (void *) smallbuf);
  1542. CHK_FAIL_END
  1543. CHK_FAIL_START
  1544. inet_pton (AF_INET6, ipv6str, (void *) &addr);
  1545. CHK_FAIL_END
  1546. #endif
  1547. return ret;
  1548. }
  1549. #include <support/test-driver.c>