check-format-test-negatives.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. #include <errno.h> /* should not report whitespace nits within <...> */
  15. #define F \
  16. void f() \
  17. { \
  18. int i; \
  19. int j; \
  20. \
  21. return; \
  22. }
  23. /* allow extra SPC in single-line comment */
  24. /*
  25. * allow extra SPC in regular multi-line comment
  26. */
  27. /*-
  28. * allow extra SPC in format-tagged multi-line comment
  29. */
  30. /** allow extra '*' in comment opening */
  31. /*! allow extra '!' in comment opening */
  32. /*
  33. ** allow "**" as first non-space chars of a line within multi-line comment
  34. */
  35. int f(void) /*
  36. * trailing multi-line comment
  37. */
  38. {
  39. typedef int INT;
  40. void v;
  41. short b;
  42. char c;
  43. signed s;
  44. unsigned u;
  45. int i;
  46. long l;
  47. float f;
  48. double d;
  49. enum {} enu;
  50. struct {} stru;
  51. union {} un;
  52. auto a;
  53. extern e;
  54. static int stat;
  55. const int con;
  56. volatile int vola;
  57. register int reg;
  58. OSSL_x y, *p = params;
  59. int params[];
  60. OSSL_PARAM * (* params []) [MAX + 1];
  61. XY *(* fn)(int a, char b);
  62. /*
  63. * multi-line comment should not disturb detection of local decls
  64. */
  65. BIO1 ***b;
  66. /* intra-line comment should not disturb detection of local decls */
  67. unsigned k;
  68. /* intra-line comment should not disturb detection of end of local decls */
  69. {
  70. int x; /* just decls in block */
  71. }
  72. if (p != (unsigned char *)
  73. &(ctx->tmp[0])) {
  74. i -= (p - (unsigned char *) /* do not confuse with var decl */
  75. &(ctx->tmp[0]));
  76. }
  77. {
  78. ctx->buf_off = 0; /* do not confuse with var decl */
  79. return 0;
  80. }
  81. {
  82. ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
  83. (unsigned char *)ctx->tmp, /* no decl */
  84. ctx->tmp_len);
  85. }
  86. {
  87. EVP_EncodeFinal(ctx->base64,
  88. (unsigned char *)ctx->buf, &(ctx->len)); /* no decl */
  89. /* push out the bytes */
  90. goto again;
  91. }
  92. {
  93. f(1, (unsigned long)2); /* no decl */
  94. x;
  95. }
  96. {
  97. char *pass_str = get_passwd(opt_srv_secret, "x");
  98. if (pass_str != NULL) {
  99. cleanse(opt_srv_secret);
  100. res = OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)pass_str,
  101. strlen(pass_str));
  102. clear_free(pass_str);
  103. }
  104. }
  105. }
  106. int g(void)
  107. {
  108. if (ctx == NULL) { /* non-leading end-of-line comment */
  109. if (/* comment after '(' */ pem_name != NULL /* comment before ')' */)
  110. /* entire-line comment indent usually like for the following line */
  111. return NULL; /* hanging indent also for this line after comment */
  112. /* leading comment has same indentation as normal code */ stmt;
  113. /* entire-line comment may have same indent as normal code */
  114. }
  115. for (i = 0; i < n; i++)
  116. for (; i < n; i++)
  117. for (i = 0; ; i++)
  118. for (i = 0;; i++)
  119. for (i = 0; i < n; )
  120. for (i = 0; i < n;)
  121. ;
  122. for (i = 0; ; )
  123. for (i = 0; ;)
  124. for (i = 0;; )
  125. for (i = 0;;)
  126. for (; i < n; )
  127. for (; j < n;)
  128. for (; ; i++)
  129. for (;; i++)
  130. ;
  131. for (;;) /* the only variant allowed in case of "empty" for (...) */
  132. ;
  133. for (;;) ; /* should not trigger: space before ';' */
  134. lab: ; /* should not trigger: space before ';' */
  135. #if X
  136. if (1) /* bad style: just part of control structure depends on #if */
  137. #else
  138. if (2) /*@ resulting false positive */
  139. #endif
  140. c; /*@ resulting false positive */
  141. if (1)
  142. if (2)
  143. c;
  144. else
  145. e;
  146. else
  147. f;
  148. do
  149. do
  150. 2;
  151. while (1);
  152. while (2);
  153. if (1)
  154. f(a, b);
  155. do
  156. 1; while (2); /*@ more than one stmt just to construct case */
  157. if (1)
  158. f(a, b);
  159. else
  160. do
  161. 1;
  162. while (2);
  163. if (1)
  164. f(a, b);
  165. else do /*@ (non-brace) code before 'do' just to construct case */
  166. 1;
  167. while (2);
  168. f1234(a,
  169. b); do /*@ (non-brace) code before 'do' just to construct case */
  170. 1;
  171. while (2);
  172. if (1)
  173. f(a,
  174. b); do /*@ (non-brace) code before 'do' just to construct case */
  175. 1;
  176. while (2);
  177. if (1)
  178. f(a, b);
  179. else
  180. do f(c, c); /*@ (non-brace) code after 'do' just to construct case */
  181. while (2);
  182. if (1)
  183. f(a, b);
  184. else
  185. return;
  186. if (1)
  187. f(a,
  188. b); else /*@ (non-brace) code before 'else' just to construct case */
  189. do
  190. 1;
  191. while (2);
  192. if (1)
  193. { /*@ brace after 'if' not on same line just to construct case */
  194. c;
  195. d;
  196. }
  197. /* this comment is correctly indented if it refers to the following line */
  198. d;
  199. if (1) {
  200. 2;
  201. } else /*@ no brace after 'else' just to construct case */
  202. 3;
  203. do {
  204. } while (x);
  205. if (1) {
  206. 2;
  207. } else {
  208. 3;
  209. }
  210. if (4)
  211. 5;
  212. else
  213. 6;
  214. if (1) {
  215. if (2) {
  216. case MAC_TYPE_MAC:
  217. {
  218. EVP_MAC_CTX *new_mac_ctx;
  219. if (ctx->pkey == NULL)
  220. return 0;
  221. }
  222. break;
  223. default:
  224. /* This should be dead code */
  225. return 0;
  226. }
  227. }
  228. if (expr_line1
  229. == expr_line2
  230. && expr_line3) {
  231. c1;
  232. } else {
  233. c;
  234. d;
  235. }
  236. if (expr_line1
  237. == expr_line2
  238. && expr_line3)
  239. hanging_stmt;
  240. }
  241. #define m \
  242. do { /* should not be confused with function header followed by '{' */ \
  243. } while (0)
  244. /* should not trigger: constant on LHS of comparison or assignment operator */
  245. X509 *x509 = NULL;
  246. int y = a + 1 < b;
  247. int ret, was_NULL = *certs == NULL;
  248. /* should not trigger: missing space before ... */
  249. float z = 1e-6 * (-1) * b[+6] * 1e+1 * (a)->f * (long)+1
  250. - (tmstart.tv_sec + tmstart.tv_nsec * 1e-9);
  251. struct st = {-1, 0};
  252. int x = (y <<= 1) + (z <= 5.0);
  253. const OPTIONS passwd_options[] = {
  254. {"aixmd5", OPT_AIXMD5, '-', "AIX MD5-based password algorithm"},
  255. #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  256. {"crypt", OPT_CRYPT, '-', "Standard Unix password algorithm (default)"},
  257. #endif
  258. OPT_R_OPTIONS,
  259. {NULL}
  260. };
  261. typedef * d(int)
  262. x;
  263. typedef (int)
  264. x;
  265. typedef (int)*()
  266. x;
  267. typedef *int *
  268. x;
  269. typedef OSSL_CMP_MSG *(*cmp_srv_process_cb_t)
  270. (OSSL_CMP_SRV_CTX *ctx, OSSL_CMP_MSG *msg)
  271. xx;
  272. #define IF(cond) if (cond)
  273. _Pragma("GCC diagnostic push")
  274. _Pragma("GCC diagnostic pop")
  275. #define CB_ERR_IF(cond, ctx, cert, depth, err) \
  276. if ((cond) && ((depth) < 0 || verify_cb_cert(ctx, cert, depth, err) == 0)) \
  277. return err
  278. static int verify_cb_crl(X509_STORE_CTX *ctx, int err)
  279. {
  280. ctx->error = err;
  281. return ctx->verify_cb(0, ctx);
  282. }
  283. #ifdef CMP_FALLBACK_EST
  284. # define CMP_FALLBACK_CERT_FILE "cert.pem"
  285. #endif
  286. #define X509_OBJECT_get0_X509(obj) \
  287. ((obj) == NULL || (obj)->type != X509_LU_X509 ? NULL : (obj)->data.x509)
  288. #define X509_STORE_CTX_set_current_cert(ctx, x) { (ctx)->current_cert = (x); }
  289. #define X509_STORE_set_ex_data(ctx, idx, data) \
  290. CRYPTO_set_ex_data(&(ctx)->ex_data, (idx), (data))
  291. typedef int (*X509_STORE_CTX_check_revocation_fn)(X509_STORE_CTX *ctx);
  292. #define X509_STORE_CTX_set_error_depth(ctx, depth) \
  293. { (ctx)->error_depth = (depth); }
  294. #define EVP_PKEY_up_ref(x) ((x)->references++)
  295. /* should not report missing blank line: */
  296. DECLARE_STACK_OF(OPENSSL_CSTRING)
  297. bool UTIL_iterate_dir(int (*fn)(const char *file, void *arg), void *arg,
  298. const char *path, bool recursive);
  299. size_t UTIL_url_encode(
  300. size_t *size_needed
  301. );
  302. size_t UTIL_url_encode(const char *source,
  303. char *destination,
  304. size_t destination_len,
  305. size_t *size_needed);
  306. #error well. oops.
  307. int f()
  308. {
  309. c;
  310. if (1) {
  311. c;
  312. }
  313. c;
  314. if (1)
  315. if (2)
  316. { /*@ brace after 'if' not on same line just to construct case */
  317. c;
  318. }
  319. e;
  320. const usign = {
  321. 0xDF,
  322. {
  323. dd
  324. },
  325. dd
  326. };
  327. const unsign = {
  328. 0xDF, {
  329. dd
  330. },
  331. dd
  332. };
  333. }
  334. const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] = {
  335. 0xDF,
  336. };
  337. const unsigned char trans_id[OSSL_CMP_TRANSACTIONID_LENGTH] =
  338. {
  339. 0xDF,
  340. };
  341. typedef
  342. int
  343. a;
  344. typedef
  345. struct
  346. {
  347. int a;
  348. } b;
  349. typedef enum {
  350. w = 0
  351. } e_type;
  352. typedef struct {
  353. enum {
  354. w = 0
  355. } e_type;
  356. enum {
  357. w = 0
  358. } e_type;
  359. } e;
  360. struct s_type {
  361. enum e_type {
  362. w = 0
  363. };
  364. };
  365. struct s_type
  366. {
  367. enum e_type {
  368. w = 0
  369. };
  370. enum e2_type {
  371. w = 0
  372. };
  373. };
  374. #define X 1 + 1
  375. #define Y /* .. */ 2 + 2
  376. #define Z 3 + 3 * (*a++)
  377. static varref cmp_vars[] = { /* comment. comment? comment! */
  378. {&opt_config}, {&opt_section},
  379. {&opt_server}, {&opt_proxy}, {&opt_path},
  380. };
  381. #define SWITCH(x) \
  382. switch (x) { \
  383. case 0: \
  384. break; \
  385. default: \
  386. break; \
  387. }
  388. #define DEFINE_SET_GET_BASE_TEST(PREFIX, SETN, GETN, DUP, FIELD, TYPE, ERR, \
  389. DEFAULT, NEW, FREE) \
  390. static int execute_CTX_##SETN##_##GETN##_##FIELD( \
  391. TEST_FIXTURE *fixture) \
  392. { \
  393. CTX *ctx = fixture->ctx; \
  394. int (*set_fn)(CTX *ctx, TYPE) = \
  395. (int (*)(CTX *ctx, TYPE))PREFIX##_##SETN##_##FIELD; \
  396. /* comment */ \
  397. }
  398. union un var; /* struct/union/enum in variable type */
  399. struct provider_store_st *f() /* struct/union/enum in function return type */
  400. {
  401. }
  402. static void f(struct pem_pass_data *data) /* struct/union/enum in arg list */
  403. {
  404. }
  405. static void *fun(void)
  406. {
  407. if (pem_name != NULL)
  408. /* comment */
  409. return NULL;
  410. label0:
  411. label1: /* allow special indent 1 for label at outermost level in body */
  412. do {
  413. label2:
  414. size_t available_len, data_len;
  415. const char *curr = txt, *next = txt;
  416. char *tmp;
  417. {
  418. label3:
  419. }
  420. } while (1);
  421. char *intraline_string_with_comment_delimiters_and_dbl_space = "1 /*1";
  422. char *multiline_string_with_comment_delimiters_and_dbl_space = "1 /*1\
  423. 2222222\'22222222222222222\"222222222" "33333 /*3333333333" "44 /*44444444444\
  424. 55555555555555\
  425. 6666";
  426. }
  427. ASN1_CHOICE(OSSL_CRMF_POPO) = {
  428. ASN1_IMP(OSSL_CRMF_POPO, value.raVerified, ASN1_NULL, 0),
  429. ASN1_EXP(OSSL_CRMF_POPO, value.keyAgreement, OSSL_CRMF_POPOPRIVKEY, 3)
  430. } ASN1_CHOICE_END(OSSL_CRMF_POPO)
  431. IMPLEMENT_ASN1_FUNCTIONS(OSSL_CRMF_POPO)
  432. ASN1_ADB(OSSL_CRMF_ATTRIBUTETYPEANDVALUE) = {
  433. ADB_ENTRY(NID_id_regCtrl_regToken,
  434. ASN1_SIMPLE(OSSL_CRMF_ATTRIBUTETYPEANDVALUE,
  435. value.regToken, ASN1_UTF8STRING)),
  436. } ASN1_ADB_END(OSSL_CRMF_ATTRIBUTETYPEANDVALUE, 0, type, 0,
  437. &attributetypeandvalue_default_tt, NULL);
  438. ASN1_ITEM_TEMPLATE(OSSL_CRMF_MSGS) =
  439. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0,
  440. OSSL_CRMF_MSGS, OSSL_CRMF_MSG)
  441. ASN1_ITEM_TEMPLATE_END(OSSL_CRMF_MSGS)
  442. void f_looong_body_200()
  443. { /* function body length up to 200 lines accepted */
  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. ;
  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. void f_looong_body_201()
  646. { /* function body length > 200 lines, but LONG BODY marker present */
  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. ;
  793. ;
  794. ;
  795. ;
  796. ;
  797. ;
  798. ;
  799. ;
  800. ;
  801. ;
  802. ;
  803. ;
  804. ;
  805. ;
  806. ;
  807. ;
  808. ;
  809. ;
  810. ;
  811. ;
  812. ;
  813. ;
  814. ;
  815. ;
  816. ;
  817. ;
  818. ;
  819. ;
  820. ;
  821. ;
  822. ;
  823. ;
  824. ;
  825. ;
  826. ;
  827. ;
  828. ;
  829. ;
  830. ;
  831. ;
  832. ;
  833. ;
  834. ;
  835. ;
  836. ;
  837. ;
  838. ;
  839. ;
  840. ;
  841. ;
  842. ;
  843. ;
  844. ;
  845. ;
  846. ;
  847. ;
  848. ;
  849. }