check-format-test-negatives.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Copyright 2007-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Nokia 2007-2019
  4. * Copyright Siemens AG 2015-2019
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. /*
  12. * A collection of test cases where check-format.pl should not report issues.
  13. * There are some known false positives, though, which are marked below.
  14. */
  15. /*-
  16. * allow extra SPC in format-tagged multi-line comment
  17. */
  18. int f(void) /*
  19. * trailing multi-line comment
  20. */
  21. {
  22. if (ctx == NULL) { /* non-leading end-of-line comment */
  23. if (/* comment after '(' */ pem_name != NULL /* comment before ')' */)
  24. /* entire-line comment indent usually like for the following line */
  25. return NULL; /* hanging indent also for this line after comment */
  26. /* leading comment has same indentation as normal code */ stmt;
  27. /* entire-line comment may have same indent as normal code */
  28. }
  29. for (;;)
  30. ;
  31. for (i = 0;;)
  32. ;
  33. for (i = 0; i < 1;)
  34. ;
  35. #if X
  36. if (1) /* bad style: just part of control structure depends on #if */
  37. #else
  38. if (2) /*@ resulting false positive */
  39. #endif
  40. c; /*@ resulting false positive */
  41. if (1)
  42. if (2)
  43. c;
  44. else
  45. e;
  46. else
  47. f;
  48. do
  49. do
  50. 2;
  51. while (1);
  52. while (2);
  53. if (1)
  54. f(a, b);
  55. do
  56. 1; while (2); /*@ more than one stmt just to construct case */
  57. if (1)
  58. f(a, b);
  59. else
  60. do
  61. 1;
  62. while (2);
  63. if (1)
  64. f(a, b);
  65. else do /*@ (non-brace) code before 'do' just to construct case */
  66. 1;
  67. while (2);
  68. f1234(a,
  69. b); do /*@ (non-brace) code before 'do' just to construct case */
  70. 1;
  71. while (2);
  72. if (1)
  73. f(a,
  74. b); do /*@ (non-brace) code before 'do' just to construct case */
  75. 1;
  76. while (2);
  77. if (1)
  78. f(a, b);
  79. else
  80. do f(c, c); /*@ (non-brace) code after 'do' just to construct case */
  81. while (2);
  82. if (1)
  83. f(a, b);
  84. else
  85. return;
  86. if (1)
  87. f(a,
  88. b); else /*@ (non-brace) code before 'else' just to construct case */
  89. do
  90. 1;
  91. while (2);
  92. if (1)
  93. { /*@ brace after 'if' not on same line just to construct case */
  94. c;
  95. d;
  96. }
  97. /* this comment is correctly indented if it refers to the following line */
  98. d;
  99. if (1) {
  100. 2;
  101. } else /*@ no brace after 'else' just to construct case */
  102. 3;
  103. do {
  104. } while (x);
  105. if (1) {
  106. 2;
  107. } else {
  108. 3;
  109. }
  110. if (4)
  111. 5;
  112. else
  113. 6;
  114. if (1) {
  115. if (2) {
  116. case MAC_TYPE_MAC:
  117. {
  118. EVP_MAC_CTX *new_mac_ctx;
  119. if (ctx->pkey == NULL)
  120. return 0;
  121. }
  122. break;
  123. default:
  124. /* This should be dead code */
  125. return 0;
  126. }
  127. }
  128. if (expr_line1
  129. == expr_line2
  130. && expr_line3) {
  131. c1;
  132. } else {
  133. c;
  134. d;
  135. }
  136. if (expr_line1
  137. == expr_line2
  138. && expr_line3)
  139. hanging_stmt;
  140. }
  141. /* should not trigger: constant on LHS of comparison or assignment operator */
  142. X509 *x509 = NULL;
  143. int y = a + 1 < b;
  144. const OPTIONS passwd_options[] = {
  145. {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
  146. #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  147. {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
  148. #endif
  149. OPT_R_OPTIONS,
  150. {NULL}
  151. };
  152. typedef * d(int)
  153. x;
  154. typedef (int)
  155. x;
  156. typedef (int)*()
  157. x;
  158. typedef *int *
  159. x;
  160. typedef OSSL_CMP_MSG *(*cmp_srv_process_cb_t)
  161. (OSSL_CMP_SRV_CTX *ctx, OSSL_CMP_MSG *msg)
  162. xx;
  163. int f()
  164. {
  165. c;
  166. if (1) {
  167. c;
  168. }
  169. c;
  170. if (1)
  171. if (2)
  172. { /*@ brace after 'if' not on same line just to construct case */
  173. c;
  174. }
  175. e;
  176. const usign = {
  177. 0xDF,
  178. {
  179. dd
  180. },
  181. dd
  182. };
  183. const unsign = {
  184. 0xDF, {
  185. dd
  186. },
  187. dd
  188. };
  189. }
  190. const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
  191. 0xDF,
  192. };
  193. const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] =
  194. {
  195. 0xDF,
  196. };
  197. typedef
  198. int
  199. a;
  200. typedef
  201. struct
  202. {
  203. int a;
  204. } b;
  205. typedef enum {
  206. w = 0
  207. } e_type;
  208. typedef struct {
  209. enum {
  210. w = 0
  211. } e_type;
  212. enum {
  213. w = 0
  214. } e_type;
  215. } e;
  216. struct s_type {
  217. enum e_type {
  218. w = 0
  219. };
  220. };
  221. struct s_type
  222. {
  223. enum e_type {
  224. w = 0
  225. };
  226. enum e2_type {
  227. w = 0
  228. };
  229. };
  230. #define X 1 + 1
  231. #define Y /* .. */ 2 + 2
  232. #define Z 3 + 3 * (*a++)
  233. static varref cmp_vars[] = { /* comment. comment? comment! */
  234. {&opt_config}, {&opt_section},
  235. {&opt_server}, {&opt_proxy}, {&opt_path},
  236. };
  237. #define SWITCH(x) \
  238. switch (x) { \
  239. case 0: \
  240. break; \
  241. default: \
  242. break; \
  243. }
  244. #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
  245. DEFAULT, NEW, FREE) \
  246. static int execute_CTX_##SETN##_##GETN##_##FIELD( \
  247. TEST_FIXTURE *fixture) \
  248. { \
  249. CTX *ctx = fixture->ctx; \
  250. int (*set_fn)(CTX *ctx, TYPE) = \
  251. (int (*)(CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
  252. /* comment */ \
  253. }
  254. union un var; /* struct/union/enum in variable type */
  255. struct provider_store_st *f() /* struct/union/enum in function return type */
  256. {
  257. }
  258. static void f(struct pem_pass_data *data) /* struct/union/enum in arg list */
  259. {
  260. }
  261. static void *fun(void)
  262. {
  263. if (pem_name != NULL)
  264. /* comment */
  265. return NULL;
  266. label0:
  267. label1: /* allow special indent 1 for label at outermost level in body */
  268. do {
  269. label2:
  270. size_t available_len, data_len;
  271. const char *curr = txt, *next = txt;
  272. char *tmp;
  273. {
  274. label3:
  275. }
  276. } while (1);
  277. char *intraline_string_with_comment_delimiters_and_dbl_space = "1 /*1";
  278. char *multiline_string_with_comment_delimiters_and_dbl_space = "1 /*1\
  279. 2222222\'22222222222222222\"222222222" "33333 /*3333333333" "44 /*44444444444\
  280. 55555555555555\
  281. 6666";
  282. }
  283. ASN1_CHOICE(OSSL_CRMF_POPO) = {
  284. ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0),
  285. ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3)
  286. } ASN1_CHOICE_END(OSSL_CRMF_POPO)
  287. IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO)
  288. ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = {
  289. ADB_ENTRY(NID_id_regCtrl_regToken,
  290. ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE,
  291. value.regToken, ASN1_UTF8STRING)),
  292. } ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0,
  293. &attributetypeandvalue_default_tt, NULL);
  294. ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) =
  295. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
  296. OSSL_CRMF_MSGS, OSSL_CRMF_MSG)
  297. ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS)
  298. void f_looong_body_200()
  299. { /* function body length up to 200 lines accepted */
  300. ;
  301. ;
  302. ;
  303. ;
  304. ;
  305. ;
  306. ;
  307. ;
  308. ;
  309. ;
  310. ;
  311. ;
  312. ;
  313. ;
  314. ;
  315. ;
  316. ;
  317. ;
  318. ;
  319. ;
  320. ;
  321. ;
  322. ;
  323. ;
  324. ;
  325. ;
  326. ;
  327. ;
  328. ;
  329. ;
  330. ;
  331. ;
  332. ;
  333. ;
  334. ;
  335. ;
  336. ;
  337. ;
  338. ;
  339. ;
  340. ;
  341. ;
  342. ;
  343. ;
  344. ;
  345. ;
  346. ;
  347. ;
  348. ;
  349. ;
  350. ;
  351. ;
  352. ;
  353. ;
  354. ;
  355. ;
  356. ;
  357. ;
  358. ;
  359. ;
  360. ;
  361. ;
  362. ;
  363. ;
  364. ;
  365. ;
  366. ;
  367. ;
  368. ;
  369. ;
  370. ;
  371. ;
  372. ;
  373. ;
  374. ;
  375. ;
  376. ;
  377. ;
  378. ;
  379. ;
  380. ;
  381. ;
  382. ;
  383. ;
  384. ;
  385. ;
  386. ;
  387. ;
  388. ;
  389. ;
  390. ;
  391. ;
  392. ;
  393. ;
  394. ;
  395. ;
  396. ;
  397. ;
  398. ;
  399. ;
  400. ;
  401. ;
  402. ;
  403. ;
  404. ;
  405. ;
  406. ;
  407. ;
  408. ;
  409. ;
  410. ;
  411. ;
  412. ;
  413. ;
  414. ;
  415. ;
  416. ;
  417. ;
  418. ;
  419. ;
  420. ;
  421. ;
  422. ;
  423. ;
  424. ;
  425. ;
  426. ;
  427. ;
  428. ;
  429. ;
  430. ;
  431. ;
  432. ;
  433. ;
  434. ;
  435. ;
  436. ;
  437. ;
  438. ;
  439. ;
  440. ;
  441. ;
  442. ;
  443. ;
  444. ;
  445. ;
  446. ;
  447. ;
  448. ;
  449. ;
  450. ;
  451. ;
  452. ;
  453. ;
  454. ;
  455. ;
  456. ;
  457. ;
  458. ;
  459. ;
  460. ;
  461. ;
  462. ;
  463. ;
  464. ;
  465. ;
  466. ;
  467. ;
  468. ;
  469. ;
  470. ;
  471. ;
  472. ;
  473. ;
  474. ;
  475. ;
  476. ;
  477. ;
  478. ;
  479. ;
  480. ;
  481. ;
  482. ;
  483. ;
  484. ;
  485. ;
  486. ;
  487. ;
  488. ;
  489. ;
  490. ;
  491. ;
  492. ;
  493. ;
  494. ;
  495. ;
  496. ;
  497. ;
  498. ;
  499. ;
  500. }
  501. void f_looong_body_201()
  502. { /* function body length > 200 lines, but LONG BODY marker present */
  503. ;
  504. ;
  505. ;
  506. ;
  507. ;
  508. ;
  509. ;
  510. ;
  511. ;
  512. ;
  513. ;
  514. ;
  515. ;
  516. ;
  517. ;
  518. ;
  519. ;
  520. ;
  521. ;
  522. ;
  523. ;
  524. ;
  525. ;
  526. ;
  527. ;
  528. ;
  529. ;
  530. ;
  531. ;
  532. ;
  533. ;
  534. ;
  535. ;
  536. ;
  537. ;
  538. ;
  539. ;
  540. ;
  541. ;
  542. ;
  543. ;
  544. ;
  545. ;
  546. ;
  547. ;
  548. ;
  549. ;
  550. ;
  551. ;
  552. ;
  553. ;
  554. ;
  555. ;
  556. ;
  557. ;
  558. ;
  559. ;
  560. ;
  561. ;
  562. ;
  563. ;
  564. ;
  565. ;
  566. ;
  567. ;
  568. ;
  569. ;
  570. ;
  571. ;
  572. ;
  573. ;
  574. ;
  575. ;
  576. ;
  577. ;
  578. ;
  579. ;
  580. ;
  581. ;
  582. ;
  583. ;
  584. ;
  585. ;
  586. ;
  587. ;
  588. ;
  589. ;
  590. ;
  591. ;
  592. ;
  593. ;
  594. ;
  595. ;
  596. ;
  597. ;
  598. ;
  599. ;
  600. ;
  601. ;
  602. ;
  603. ;
  604. ;
  605. ;
  606. ;
  607. ;
  608. ;
  609. ;
  610. ;
  611. ;
  612. ;
  613. ;
  614. ;
  615. ;
  616. ;
  617. ;
  618. ;
  619. ;
  620. ;
  621. ;
  622. ;
  623. ;
  624. ;
  625. ;
  626. ;
  627. ;
  628. ;
  629. ;
  630. ;
  631. ;
  632. ;
  633. ;
  634. ;
  635. ;
  636. ;
  637. ;
  638. ;
  639. ;
  640. ;
  641. ;
  642. ;
  643. ;
  644. ;
  645. ;
  646. ;
  647. ;
  648. ;
  649. ;
  650. ;
  651. ;
  652. ;
  653. ;
  654. ;
  655. ;
  656. ;
  657. ;
  658. ;
  659. ;
  660. ;
  661. ;
  662. ;
  663. ;
  664. ;
  665. ;
  666. ;
  667. ;
  668. ;
  669. ;
  670. ;
  671. ;
  672. ;
  673. ;
  674. ;
  675. ;
  676. ;
  677. ;
  678. ;
  679. ;
  680. ;
  681. ;
  682. ;
  683. ;
  684. ;
  685. ;
  686. ;
  687. ;
  688. ;
  689. ;
  690. ;
  691. ;
  692. ;
  693. ;
  694. ;
  695. ;
  696. ;
  697. ;
  698. ;
  699. ;
  700. ;
  701. ;
  702. ;
  703. ;
  704. ;
  705. }