check-format-test-negatives.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Copyright 2007-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright Siemens AG 2015-2022
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. /*
  11. * A collection of test cases where check-format.pl should not report issues.
  12. * There are some known false positives, though, which are marked below.
  13. */
  14. #define F \
  15. void f() \
  16. { \
  17. int i; \
  18. int j; \
  19. \
  20. return; \
  21. }
  22. /*-
  23. * allow extra SPC in format-tagged multi-line comment
  24. */
  25. int f(void) /*
  26. * trailing multi-line comment
  27. */
  28. {
  29. typedef int INT;
  30. void v;
  31. short b;
  32. char c;
  33. signed s;
  34. unsigned u;
  35. int i;
  36. long l;
  37. float f;
  38. double d;
  39. enum {} enu;
  40. struct {} stru;
  41. union {} un;
  42. auto a;
  43. extern e;
  44. static int stat;
  45. const int con;
  46. volatile int vola;
  47. register int reg;
  48. OSSL_x y, *p = params;
  49. int params[];
  50. OSSL_PARAM * (* params []) [MAX + 1];
  51. XY *(* fn)(int a, char b);
  52. /*
  53. * multi-line comment should not disturb detection of local decls
  54. */
  55. BIO1 ***b;
  56. /* intra-line comment should not disturb detection of local decls */
  57. unsigned k;
  58. /* intra-line comment should not disturb detection of end of local decls */
  59. {
  60. int x; /* just decls in block */
  61. }
  62. if (p != (unsigned char *)
  63. &(ctx->tmp[0])) {
  64. i -= (p - (unsigned char *) /* do not confuse with var decl */
  65. &(ctx->tmp[0]));
  66. }
  67. {
  68. ctx->buf_off = 0; /* do not confuse with var decl */
  69. return 0;
  70. }
  71. {
  72. ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
  73. (unsigned char *)ctx->tmp, /* no decl */
  74. ctx->tmp_len);
  75. }
  76. {
  77. EVP_EncodeFinal(ctx->base64,
  78. (unsigned char *)ctx->buf, &(ctx->len)); /* no decl */
  79. /* push out the bytes */
  80. goto again;
  81. }
  82. {
  83. f(1, (unsigned long)2); /* no decl */
  84. x;
  85. }
  86. {
  87. char *pass_str = get_passwd(opt_srv_secret, "x");
  88. if (pass_str != NULL) {
  89. cleanse(opt_srv_secret);
  90. res = OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)pass_str,
  91. strlen(pass_str));
  92. clear_free(pass_str);
  93. }
  94. }
  95. }
  96. int g(void)
  97. {
  98. if (ctx == NULL) { /* non-leading end-of-line comment */
  99. if (/* comment after '(' */ pem_name != NULL /* comment before ')' */)
  100. /* entire-line comment indent usually like for the following line */
  101. return NULL; /* hanging indent also for this line after comment */
  102. /* leading comment has same indentation as normal code */ stmt;
  103. /* entire-line comment may have same indent as normal code */
  104. }
  105. for (;;)
  106. ;
  107. for (i = 0;;)
  108. ;
  109. for (i = 0; i < 1;)
  110. ;
  111. for (;;)
  112. for (; i < n; i++)
  113. for (;; p++)
  114. ;
  115. for (;;) ; /* should not trigger: space before ';' */
  116. lab: ; /* should not trigger: space before ';' */
  117. #if X
  118. if (1) /* bad style: just part of control structure depends on #if */
  119. #else
  120. if (2) /*@ resulting false positive */
  121. #endif
  122. c; /*@ resulting false positive */
  123. if (1)
  124. if (2)
  125. c;
  126. else
  127. e;
  128. else
  129. f;
  130. do
  131. do
  132. 2;
  133. while (1);
  134. while (2);
  135. if (1)
  136. f(a, b);
  137. do
  138. 1; while (2); /*@ more than one stmt just to construct case */
  139. if (1)
  140. f(a, b);
  141. else
  142. do
  143. 1;
  144. while (2);
  145. if (1)
  146. f(a, b);
  147. else do /*@ (non-brace) code before 'do' just to construct case */
  148. 1;
  149. while (2);
  150. f1234(a,
  151. b); do /*@ (non-brace) code before 'do' just to construct case */
  152. 1;
  153. while (2);
  154. if (1)
  155. f(a,
  156. b); do /*@ (non-brace) code before 'do' just to construct case */
  157. 1;
  158. while (2);
  159. if (1)
  160. f(a, b);
  161. else
  162. do f(c, c); /*@ (non-brace) code after 'do' just to construct case */
  163. while (2);
  164. if (1)
  165. f(a, b);
  166. else
  167. return;
  168. if (1)
  169. f(a,
  170. b); else /*@ (non-brace) code before 'else' just to construct case */
  171. do
  172. 1;
  173. while (2);
  174. if (1)
  175. { /*@ brace after 'if' not on same line just to construct case */
  176. c;
  177. d;
  178. }
  179. /* this comment is correctly indented if it refers to the following line */
  180. d;
  181. if (1) {
  182. 2;
  183. } else /*@ no brace after 'else' just to construct case */
  184. 3;
  185. do {
  186. } while (x);
  187. if (1) {
  188. 2;
  189. } else {
  190. 3;
  191. }
  192. if (4)
  193. 5;
  194. else
  195. 6;
  196. if (1) {
  197. if (2) {
  198. case MAC_TYPE_MAC:
  199. {
  200. EVP_MAC_CTX *new_mac_ctx;
  201. if (ctx->pkey == NULL)
  202. return 0;
  203. }
  204. break;
  205. default:
  206. /* This should be dead code */
  207. return 0;
  208. }
  209. }
  210. if (expr_line1
  211. == expr_line2
  212. && expr_line3) {
  213. c1;
  214. } else {
  215. c;
  216. d;
  217. }
  218. if (expr_line1
  219. == expr_line2
  220. && expr_line3)
  221. hanging_stmt;
  222. }
  223. /* should not trigger: constant on LHS of comparison or assignment operator */
  224. X509 *x509 = NULL;
  225. int y = a + 1 < b;
  226. int ret, was_NULL = *certs == NULL;
  227. /* should not trigger: no space before binary ... operator */
  228. float z = 1e-6 * (-1) * b[+6] * 1e+1 * (a)->f * (long)+1
  229. - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9);
  230. struct st = {-1, 0};
  231. const OPTIONS passwd_options[] = {
  232. {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
  233. #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  234. {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
  235. #endif
  236. OPT_R_OPTIONS,
  237. {NULL}
  238. };
  239. typedef * d(int)
  240. x;
  241. typedef (int)
  242. x;
  243. typedef (int)*()
  244. x;
  245. typedef *int *
  246. x;
  247. typedef OSSL_CMP_MSG *(*cmp_srv_process_cb_t)
  248. (OSSL_CMP_SRV_CTX *ctx, OSSL_CMP_MSG *msg)
  249. xx;
  250. int f()
  251. {
  252. c;
  253. if (1) {
  254. c;
  255. }
  256. c;
  257. if (1)
  258. if (2)
  259. { /*@ brace after 'if' not on same line just to construct case */
  260. c;
  261. }
  262. e;
  263. const usign = {
  264. 0xDF,
  265. {
  266. dd
  267. },
  268. dd
  269. };
  270. const unsign = {
  271. 0xDF, {
  272. dd
  273. },
  274. dd
  275. };
  276. }
  277. const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
  278. 0xDF,
  279. };
  280. const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] =
  281. {
  282. 0xDF,
  283. };
  284. typedef
  285. int
  286. a;
  287. typedef
  288. struct
  289. {
  290. int a;
  291. } b;
  292. typedef enum {
  293. w = 0
  294. } e_type;
  295. typedef struct {
  296. enum {
  297. w = 0
  298. } e_type;
  299. enum {
  300. w = 0
  301. } e_type;
  302. } e;
  303. struct s_type {
  304. enum e_type {
  305. w = 0
  306. };
  307. };
  308. struct s_type
  309. {
  310. enum e_type {
  311. w = 0
  312. };
  313. enum e2_type {
  314. w = 0
  315. };
  316. };
  317. #define X 1 + 1
  318. #define Y /* .. */ 2 + 2
  319. #define Z 3 + 3 * (*a++)
  320. static varref cmp_vars[] = { /* comment. comment? comment! */
  321. {&opt_config}, {&opt_section},
  322. {&opt_server}, {&opt_proxy}, {&opt_path},
  323. };
  324. #define SWITCH(x) \
  325. switch (x) { \
  326. case 0: \
  327. break; \
  328. default: \
  329. break; \
  330. }
  331. #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
  332. DEFAULT, NEW, FREE) \
  333. static int execute_CTX_##SETN##_##GETN##_##FIELD( \
  334. TEST_FIXTURE *fixture) \
  335. { \
  336. CTX *ctx = fixture->ctx; \
  337. int (*set_fn)(CTX *ctx, TYPE) = \
  338. (int (*)(CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
  339. /* comment */ \
  340. }
  341. union un var; /* struct/union/enum in variable type */
  342. struct provider_store_st *f() /* struct/union/enum in function return type */
  343. {
  344. }
  345. static void f(struct pem_pass_data *data) /* struct/union/enum in arg list */
  346. {
  347. }
  348. static void *fun(void)
  349. {
  350. if (pem_name != NULL)
  351. /* comment */
  352. return NULL;
  353. label0:
  354. label1: /* allow special indent 1 for label at outermost level in body */
  355. do {
  356. label2:
  357. size_t available_len, data_len;
  358. const char *curr = txt, *next = txt;
  359. char *tmp;
  360. {
  361. label3:
  362. }
  363. } while (1);
  364. char *intraline_string_with_comment_delimiters_and_dbl_space = "1 /*1";
  365. char *multiline_string_with_comment_delimiters_and_dbl_space = "1 /*1\
  366. 2222222\'22222222222222222\"222222222" "33333 /*3333333333" "44 /*44444444444\
  367. 55555555555555\
  368. 6666";
  369. }
  370. ASN1_CHOICE(OSSL_CRMF_POPO) = {
  371. ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0),
  372. ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3)
  373. } ASN1_CHOICE_END(OSSL_CRMF_POPO)
  374. IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO)
  375. ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = {
  376. ADB_ENTRY(NID_id_regCtrl_regToken,
  377. ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE,
  378. value.regToken, ASN1_UTF8STRING)),
  379. } ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0,
  380. &attributetypeandvalue_default_tt, NULL);
  381. ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) =
  382. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
  383. OSSL_CRMF_MSGS, OSSL_CRMF_MSG)
  384. ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS)
  385. void f_looong_body_200()
  386. { /* function body length up to 200 lines accepted */
  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. ;
  502. ;
  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. void f_looong_body_201()
  589. { /* function body length > 200 lines, but LONG BODY marker present */
  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. ;
  706. ;
  707. ;
  708. ;
  709. ;
  710. ;
  711. ;
  712. ;
  713. ;
  714. ;
  715. ;
  716. ;
  717. ;
  718. ;
  719. ;
  720. ;
  721. ;
  722. ;
  723. ;
  724. ;
  725. ;
  726. ;
  727. ;
  728. ;
  729. ;
  730. ;
  731. ;
  732. ;
  733. ;
  734. ;
  735. ;
  736. ;
  737. ;
  738. ;
  739. ;
  740. ;
  741. ;
  742. ;
  743. ;
  744. ;
  745. ;
  746. ;
  747. ;
  748. ;
  749. ;
  750. ;
  751. ;
  752. ;
  753. ;
  754. ;
  755. ;
  756. ;
  757. ;
  758. ;
  759. ;
  760. ;
  761. ;
  762. ;
  763. ;
  764. ;
  765. ;
  766. ;
  767. ;
  768. ;
  769. ;
  770. ;
  771. ;
  772. ;
  773. ;
  774. ;
  775. ;
  776. ;
  777. ;
  778. ;
  779. ;
  780. ;
  781. ;
  782. ;
  783. ;
  784. ;
  785. ;
  786. ;
  787. ;
  788. ;
  789. ;
  790. ;
  791. ;
  792. }