GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cppcheck/test/testleakautovar.cpp at main · cppcheck-opensource/cppcheck Expand file tree/ Copy pathtestleakautovar.cppMore file actions 3611 lines (3201 loc) · 128 KB / Copy pathtestleakautovar.cppFile metadata and controls3611 lines (3201 loc) · 128 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 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 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 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 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 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 /* * Cppcheck - A tool for static C/C++ code analysis * Copyright (C) 2007-2026 Cppcheck team. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "checkleakautovar.h" #include "fixture.h" #include "helpers.h" #include "settings.h" #include <cstddef> #include <string> class TestLeakAutoVar : public TestFixture { public: TestLeakAutoVar() : TestFixture("TestLeakAutoVar") {} private: const Settings settings = settingsBuilder().library("std.cfg").checkLibrary().build(); void run() override { mNewTemplate = true; // Assign TEST_CASE(assign1); TEST_CASE(assign2); TEST_CASE(assign3); TEST_CASE(assign4); TEST_CASE(assign5); TEST_CASE(assign6); TEST_CASE(assign7); TEST_CASE(assign8); TEST_CASE(assign9); TEST_CASE(assign10); TEST_CASE(assign11); // #3942: x = a(b(p)); TEST_CASE(assign12); // #4236: FP. bar(&x); TEST_CASE(assign13); // #4237: FP. char*&ref=p; p=malloc(10); free(ref); TEST_CASE(assign14); TEST_CASE(assign15); TEST_CASE(assign16); TEST_CASE(assign17); // #9047 TEST_CASE(assign18); TEST_CASE(assign19); TEST_CASE(assign20); // #9187 TEST_CASE(assign21); // #10186 TEST_CASE(assign22); // #9139 TEST_CASE(assign23); TEST_CASE(assign24); // #7440 TEST_CASE(assign25); TEST_CASE(assign26); TEST_CASE(memcpy1); // #11542 TEST_CASE(memcpy2); TEST_CASE(isAutoDealloc); TEST_CASE(realloc1); TEST_CASE(realloc2); TEST_CASE(realloc3); TEST_CASE(realloc4); TEST_CASE(realloc5); // #9292, #9990 TEST_CASE(freopen1); TEST_CASE(freopen2); TEST_CASE(deallocuse1); TEST_CASE(deallocuse3); TEST_CASE(deallocuse4); TEST_CASE(deallocuse5); // #4018: FP. free(p), p = 0; TEST_CASE(deallocuse6); // #4034: FP. x = p = f(); TEST_CASE(deallocuse7); // #6467, #6469, #6473 TEST_CASE(deallocuse8); // #1765 TEST_CASE(deallocuse9); // #9781 TEST_CASE(deallocuse10); TEST_CASE(deallocuse11); // #8302 TEST_CASE(deallocuse12); TEST_CASE(deallocuse13); TEST_CASE(deallocuse14); TEST_CASE(deallocuse15); TEST_CASE(deallocuse16); // #8109: delete with comma operator TEST_CASE(doublefree1); TEST_CASE(doublefree2); TEST_CASE(doublefree3); // #4914 TEST_CASE(doublefree4); // #5451 - FP when exit is called TEST_CASE(doublefree5); // #5522 TEST_CASE(doublefree6); // #7685 TEST_CASE(doublefree7); TEST_CASE(doublefree8); TEST_CASE(doublefree9); TEST_CASE(doublefree10); // #8706 TEST_CASE(doublefree11); TEST_CASE(doublefree12); // #10502 TEST_CASE(doublefree13); // #11008 TEST_CASE(doublefree14); // #9708 TEST_CASE(doublefree15); TEST_CASE(doublefree16); TEST_CASE(doublefree17); // #8109: delete with comma operator TEST_CASE(doublefree18); TEST_CASE(doublefree19); // #13960 // exit TEST_CASE(exit1); TEST_CASE(exit2); TEST_CASE(exit3); TEST_CASE(exit4); // handling function calls TEST_CASE(functioncall1); // goto TEST_CASE(goto1); TEST_CASE(goto2); TEST_CASE(goto3); // #11431 // if/else TEST_CASE(ifelse1); TEST_CASE(ifelse2); TEST_CASE(ifelse3); TEST_CASE(ifelse4); TEST_CASE(ifelse5); TEST_CASE(ifelse6); // #3370 TEST_CASE(ifelse7); // #5576 - if (fd < 0) TEST_CASE(ifelse8); // #5747 - if (fd == -1) TEST_CASE(ifelse9); // #5273 - if (X(p==NULL, 0)) TEST_CASE(ifelse10); // #8794 - if (!(x!=NULL)) TEST_CASE(ifelse11); // #8365 - if (NULL == (p = malloc(4))) TEST_CASE(ifelse12); // #8340 - if ((*p = malloc(4)) == NULL) TEST_CASE(ifelse13); // #8392 TEST_CASE(ifelse14); // #9130 - if (x == (char*)NULL) TEST_CASE(ifelse15); // #9206 - if (global_ptr = malloc(1)) TEST_CASE(ifelse16); // #9635 - if (p = malloc(4), p == NULL) TEST_CASE(ifelse17); // if (!!(!p)) TEST_CASE(ifelse18); TEST_CASE(ifelse19); TEST_CASE(ifelse20); // #10182 TEST_CASE(ifelse21); TEST_CASE(ifelse22); // #10187 TEST_CASE(ifelse23); // #5473 TEST_CASE(ifelse24); // #1733 TEST_CASE(ifelse25); // #9966 TEST_CASE(ifelse26); TEST_CASE(ifelse27); TEST_CASE(ifelse28); // #11038 TEST_CASE(ifelse29); TEST_CASE(ifelse30); // switch TEST_CASE(switch1); // loops TEST_CASE(loop1); TEST_CASE(loop2); // mismatching allocation/deallocation TEST_CASE(mismatchAllocDealloc); TEST_CASE(smartPointerDeleter); TEST_CASE(smartPointerRelease); // Execution reaches a 'return' TEST_CASE(return1); TEST_CASE(return2); TEST_CASE(return3); TEST_CASE(return4); TEST_CASE(return5); TEST_CASE(return6); // #8282 return {p, p} TEST_CASE(return7); // #9343 return (uint8_t*)x TEST_CASE(return8); TEST_CASE(return9); TEST_CASE(return10); TEST_CASE(return11); // #13098 TEST_CASE(return12); // #12238 TEST_CASE(return13); // General tests: variable type, allocation type, etc TEST_CASE(test1); TEST_CASE(test2); TEST_CASE(test3); // #3954 - reference pointer TEST_CASE(test4); // #5923 - static pointer TEST_CASE(test5); // unknown type // Execution reaches a 'throw' TEST_CASE(throw1); TEST_CASE(throw2); // Possible leak => Further configuration is needed for complete analysis TEST_CASE(configuration1); TEST_CASE(configuration2); TEST_CASE(configuration3); TEST_CASE(configuration4); TEST_CASE(configuration5); TEST_CASE(configuration6); TEST_CASE(configurationUse); TEST_CASE(ptrptr); TEST_CASE(nestedAllocation); TEST_CASE(testKeywords); // #6767 TEST_CASE(inlineFunction); // #3989 TEST_CASE(smartPtrInContainer); // #8262 TEST_CASE(unconditionalScope); TEST_CASE(functionCallCastConfig); // #9652 TEST_CASE(functionCallLeakIgnoreConfig); // #7923 } struct CheckOptions { bool cpp = false; const Settings *s = nullptr; }; #define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) template<size_t size> void check_(const char* file, int line, const char (&code)[size], const CheckOptions& options = make_default_obj()) { const Settings& settings1 = options.s ? *options.s : settings; check_(file, line, code, settings1, options.cpp); } template<size_t size> void check_(const char* file, int line, const char (&code)[size], const Settings & s, bool cpp = true) { // Tokenize.. SimpleTokenizer tokenizer(s, *this, cpp); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckLeakAutoVar check; runChecks(check, tokenizer, *this); } void assign1() { check("void f() {\n" " char *p = malloc(10);\n" " p = NULL;\n" " free(p);\n" "}\n"); ASSERT_EQUALS("[test.c:3:5]: (error) Memory leak: p [memleak]\n", errout_str()); } void assign2() { check("void f() {\n" " char *p = malloc(10);\n" " char *q = p;\n" " free(q);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign3() { check("void f() {\n" " char *p = malloc(10);\n" " char *q = p + 1;\n" " free(q - 1);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign4() { check("void f() {\n" " char *a = malloc(10);\n" " a += 10;\n" " free(a - 10);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign5() { check("void foo()\n" "{\n" " char *p = new char[100];\n" " list += p;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign6() { // #2806 - FP when there is redundant assignment check("void foo() {\n" " char *p = malloc(10);\n" " p = strcpy(p,q);\n" " free(p);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign7() { check("void foo(struct str *d) {\n" " struct str *p = malloc(10);\n" " d->p = p;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign8() { // linux list check("void foo(struct str *d) {\n" " struct str *p = malloc(10);\n" " d->p = &p->x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign9() { check("void foo() {\n" " char *p = x();\n" " free(p);\n" " p = NULL;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign10() { check("void foo() {\n" " char *p;\n" " if (x) { p = malloc(10); }\n" " if (!x) { p = NULL; }\n" " free(p);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign11() { // #3942 - FP for x = a(b(p)); check("void f() {\n" " char *p = malloc(10);\n" " x = a(b(p));\n" "}\n"); ASSERT_EQUALS("[test.c:4:1]: (information) --check-library: Function b() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str()); } void assign12() { // #4236: FP. bar(&x) check("void f() {\n" " char *p = malloc(10);\n" " free(p);\n" " bar(&p);\n" " free(p);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign13() { // #4237: FP. char *&ref=p; p=malloc(10); free(ref); check("void f() {\n" " char *p;\n" " char * &ref = p;\n" " p = malloc(10);\n" " free(ref);\n" "}\n", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("", "[test.cpp:6:1]: (error) Memory leak: p [memleak]\n", errout_str()); } void assign14() { check("void f(int x) {\n" " char *p;\n" " if (x && (p = malloc(10))) { }\n" "}\n"); ASSERT_EQUALS("[test.c:4:1]: (error) Memory leak: p [memleak]\n", errout_str()); check("void f(int x) {\n" " char *p;\n" " if (x && (p = new char[10])) { }\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4:1]: (error) Memory leak: p [memleak]\n", errout_str()); } void assign15() { // #8120 check("void f() {\n" " baz *p;\n" " p = malloc(sizeof *p);\n" " free(p);\n" " p = malloc(sizeof *p);\n" " free(p);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign16() { check("void f() {\n" " char *p = malloc(10);\n" " free(p);\n" " if (p=dostuff()) *p = 0;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign17() { // #9047 check("void f() {\n" " char *p = (char*)malloc(10);\n" "}\n"); ASSERT_EQUALS("[test.c:3:1]: (error) Memory leak: p [memleak]\n", errout_str()); check("void f() {\n" " char *p = (char*)(int*)malloc(10);\n" "}\n"); ASSERT_EQUALS("[test.c:3:1]: (error) Memory leak: p [memleak]\n", errout_str()); } void assign18() { check("void f(int x) {\n" " char *p;\n" " if (x && (p = (char*)malloc(10))) { }\n" "}\n"); ASSERT_EQUALS("[test.c:4:1]: (error) Memory leak: p [memleak]\n", errout_str()); check("void f(int x) {\n" " char *p;\n" " if (x && (p = (char*)(int*)malloc(10))) { }\n" "}\n"); ASSERT_EQUALS("[test.c:4:1]: (error) Memory leak: p [memleak]\n", errout_str()); } void assign19() { check("void f() {\n" " char *p = malloc(10);\n" " free((void*)p);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void assign20() { // #9187 check("void f() {\n" " char *p = static_cast<int>(malloc(10));\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3:1]: (error) Memory leak: p [memleak]\n", errout_str()); } void assign21() { check("void f(int **x) {\n" // #10186 " void *p = malloc(10);\n" " *x = (int*)p;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct S { int i; };\n" // #10996 "void f() {\n" " S* s = new S();\n" " (void)s;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:5:1]: (error) Memory leak: s [memleak]\n", errout_str()); } void assign22() { // #9139 const Settings s = settingsBuilder().library("posix.cfg").checkLibrary().build(); check("void f(char tempFileName[256]) {\n" " const int fd = socket(AF_INET, SOCK_PACKET, 0 );\n" "}\n", dinit(CheckOptions, $.cpp = true, $.s = &s)); ASSERT_EQUALS("[test.cpp:3:1]: (error) Resource leak: fd [resourceLeak]\n", errout_str()); check("void f() {\n" " const void * const p = malloc(10);\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3:1]: (error) Memory leak: p [memleak]\n", errout_str()); } void assign23() { const Settings s = settingsBuilder().library("posix.cfg").checkLibrary().build(); check("void f() {\n" " int n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14;\n" " *&n1 = open(\"xx.log\", O_RDONLY);\n" " *&(n2) = open(\"xx.log\", O_RDONLY);\n" " *(&n3) = open(\"xx.log\", O_RDONLY);\n" " *&*&n4 = open(\"xx.log\", O_RDONLY);\n" " *&*&*&(n5) = open(\"xx.log\", O_RDONLY);\n" " *&*&(*&n6) = open(\"xx.log\", O_RDONLY);\n" " *&*(&*&n7) = open(\"xx.log\", O_RDONLY);\n" " *(&*&n8) = open(\"xx.log\", O_RDONLY);\n" " *&(*&*&(*&n9)) = open(\"xx.log\", O_RDONLY);\n" " (n10) = open(\"xx.log\", O_RDONLY);\n" " ((n11)) = open(\"xx.log\", O_RDONLY);\n" " ((*&n12)) = open(\"xx.log\", O_RDONLY);\n" " *(&(*&n13)) = open(\"xx.log\", O_RDONLY);\n" " ((*&(*&n14))) = open(\"xx.log\", O_RDONLY);\n" "}\n", dinit(CheckOptions, $.cpp = true, $.s = &s)); ASSERT_EQUALS("[test.cpp:17:1]: (error) Resource leak: n1 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n2 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n3 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n4 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n5 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n6 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n7 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n8 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n9 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n10 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n11 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n12 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n13 [resourceLeak]\n" "[test.cpp:17:1]: (error) Resource leak: n14 [resourceLeak]\n", errout_str()); } void assign24() { check("void f() {\n" // #7440 " char* data = new char[100];\n" " char** dataPtr = &data;\n" " delete[] *dataPtr;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " char* data = new char[100];\n" " char** dataPtr = &data;\n" " printf(\"test\");\n" " delete[] *dataPtr;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" // #9279 " int* p = new int;\n" " *p = 42;\n" " g();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4:8]: (information) --check-library: Function g() should have <noreturn> configuration [checkLibraryNoReturn]\n", errout_str()); check("void g();\n" "void f() {\n" " int* p = new int;\n" " *p = 42;\n" " g();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6:1]: (error) Memory leak: p [memleak]\n", errout_str()); check("void g() {}\n" "void f() {\n" " int* p = new int;\n" " *p = 42;\n" " g();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6:1]: (error) Memory leak: p [memleak]\n", errout_str()); check("[[noreturn]] void g();\n" "void f() {\n" " int* p = new int;\n" " *p = 42;\n" " g();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void g() { exit(1); }\n" "void f() {\n" " int* p = new int;\n" " *p = 42;\n" " g();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void g() {}\n" // #10517 "void f() {\n" " char* p = malloc(10);\n" " g();\n" "}\n"); ASSERT_EQUALS("[test.c:5:1]: (error) Memory leak: p [memleak]\n", errout_str()); } void assign25() { check("void f() {\n" // #11796 " int* p{ new int };\n" " int* q(new int);\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4:1]: (error) Memory leak: p [memleak]\n" "[test.cpp:4:1]: (error) Memory leak: q [memleak]\n", errout_str()); check("struct S : B {\n" // #12239 " void f();\n" " void g();\n" "};\n" "void S::f() {\n" " FD* fd(new FD(this));\n" " fd->exec();\n" "}\n" "void S::g() {\n" " FD* fd{ new FD(this) };\n" " fd->exec();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct C {\n" // #12327 " char* m_p;\n" " C(char* p) : m_p(p) {}\n" "};\n" "std::list<C> gli;\n" "void f() {\n" " std::list<C> li;\n" " char* p = new char[1];\n" " C c(p);\n" " li.push_back(c);\n" " C c2(li.front());\n" " delete[] c2.m_p;\n" "}\n" "void g() {\n" " char* p = new char[1];\n" " C c(p);\n" " gli.push_back(c);\n" "}\n" "void h() {\n" " std::list<C> li;\n" " char* p = new char[1];\n" " C c(p);\n" " li.push_back(c);\n" " delete[] li.front().m_p;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct S {\n" // #12890 " int** p;\n" " S() {\n" " p = std::malloc(sizeof(int*));\n" " p[0] = new int;\n" " }\n" " ~S() {\n" " delete p[0];\n" " std::free(p);\n" " }\n" "};\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } void assign26() { check("void f(int*& x) {\n" // #8235 " int* p = (int*)malloc(10);\n" " x = p ? p : nullptr;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(int*& x) {\n" " int* p = (int*)malloc(10);\n" " x = p != nullptr ? p : nullptr;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(const char* n) {\n" // #12724 " FILE* fp = fopen(n, \"r\");\n" " bool b = (fp == NULL);\n" " if (b)\n" " return;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6:1]: (error) Resource leak: fp [resourceLeak]\n", errout_str()); } void memcpy1() { // #11542 check("void f(char** old, char* value) {\n" " char *str = strdup(value);\n" " memcpy(old, &str, sizeof(char*));\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void memcpy2() { check("void f(char* old, char* value, size_t len) {\n" " char *str = strdup(value);\n" " memcpy(old, str, len);\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4:1]: (error) Memory leak: str [memleak]\n", errout_str()); } void isAutoDealloc() { check("void f() {\n" " char *p = new char[100];" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2:29]: (error) Memory leak: p [memleak]\n", errout_str()); check("void f() {\n" " Fred *fred = new Fred;" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f() {\n" " std::string *str = new std::string;" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:2:40]: (error) Memory leak: str [memleak]\n", errout_str()); check("class TestType {\n" // #9028 "public:\n" " char ca[12];\n" "};\n" "void f() {\n" " TestType *tt = new TestType();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:7:1]: (error) Memory leak: tt [memleak]\n", errout_str()); check("void f(Bar& b) {\n" // #7622 " char* data = new char[10];\n" " b = Bar(*new Foo(data));\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4:1]: (information) --check-library: Function Foo() should have <use>/<leak-ignore> configuration [checkLibraryUseIgnore]\n", errout_str()); check("class B {};\n" " class D : public B {};\n" " void g() {\n" " auto d = new D();\n" " if (d) {}\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:6:1]: (error) Memory leak: d [memleak]\n", errout_str()); check("struct S {\n" // #12354 " int i{};\n" " void f();\n" "};\n" "void f(S* p, bool b) {\n" " if (b)\n" " p = new S();\n" " p->f();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:9:1]: (error) Memory leak: p [memleak]\n", errout_str()); } void realloc1() { check("void f() {\n" " void *p = malloc(10);\n" " void *q = realloc(p, 20);\n" " free(q)\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void realloc2() { check("void f() {\n" " void *p = malloc(10);\n" " void *q = realloc(p, 20);\n" "}\n"); ASSERT_EQUALS("[test.c:4:1]: (error) Memory leak: q [memleak]\n", errout_str()); } void realloc3() { check("void f() {\n" " char *p = malloc(10);\n" " char *q = (char*) realloc(p, 20);\n" "}\n"); ASSERT_EQUALS("[test.c:4:1]: (error) Memory leak: q [memleak]\n", errout_str()); } void realloc4() { check("void f(void *p) {\n" " void * q = realloc(p, 10);\n" " if (q == NULL)\n" " return;\n" "}\n"); ASSERT_EQUALS("[test.c:5:1]: (error) Memory leak: q [memleak]\n", errout_str()); } void realloc5() { // #9292 check("void * f(void * ptr, size_t size) {\n" " void *datap = realloc(ptr, size);\n" " if (size && !datap)\n" " free(ptr);\n" " return datap;\n" "}\n"); ASSERT_EQUALS("", errout_str()); // #9990 check("void f() {\n" " void * p1 = malloc(10);\n" " if (!p1)\n" " return;\n" " void * p2 = realloc(p1, 42);\n" " if (!p2) {\n" " free(p1);\n" " return;\n" " }\n" " free(p2);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void freopen1() { check("void f() {\n" " void *p = fopen(name,a);\n" " void *q = freopen(name, b, p);\n" " fclose(q)\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void freopen2() { check("void f() {\n" " void *p = fopen(name,a);\n" " void *q = freopen(name, b, p);\n" "}\n"); ASSERT_EQUALS("[test.c:4:1]: (error) Resource leak: q [resourceLeak]\n", errout_str()); } void deallocuse1() { check("void f(char *p) {\n" " free(p);\n" " *p = 0;\n" "}\n"); ASSERT_EQUALS("[test.c:3:6]: (error) Dereferencing 'p' after it is deallocated / released [deallocuse]\n", errout_str()); check("void f(char *p) {\n" " free(p);\n" " char c = *p;\n" "}\n"); ASSERT_EQUALS("[test.c:3:15]: (error) Dereferencing 'p' after it is deallocated / released [deallocuse]\n", errout_str()); } void deallocuse3() { check("void f(struct str *p) {\n" " free(p);\n" " p = p->next;\n" "}\n"); ASSERT_EQUALS("[test.c:3:9]: (error) Dereferencing 'p' after it is deallocated / released [deallocuse]\n", errout_str()); } void deallocuse4() { check("void f(char *p) {\n" " free(p);\n" " return p;\n" "}\n"); ASSERT_EQUALS("[test.c:2:5] -> [test.c:3:5]: (error) Returning/dereferencing 'p' after it is deallocated / released [deallocret]\n", errout_str()); check("void f(char *p) {\n" " if (!p) free(p);\n" " return p;\n" "}\n"); ASSERT_EQUALS("", errout_str()); check("void f(char *p) {\n" " if (!p) delete p;\n" " return p;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(char *p) {\n" " if (!p) delete [] p;\n" " return p;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void f(void* p) {\n" " if (a) {\n" " free(p);\n" " return;\n" " }\n" " g(p);\n" " return;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void deallocuse5() { // #4018 check("void f(char *p) {\n" " free(p), p = 0;\n" " *p = 0;\n" // <- Make sure pointer info is reset. It is NOT a freed pointer dereference "}\n"); ASSERT_EQUALS("", errout_str()); } void deallocuse6() { // #4034 check("void f(char *p) {\n" " free(p);\n" " x = p = foo();\n" // <- p is not dereferenced "}\n"); ASSERT_EQUALS("", errout_str()); } void deallocuse7() { // #6467, #6469, #6473, #6648 check("struct Foo { int* ptr; };\n" "void f(Foo* foo) {\n" " delete foo->ptr;\n" " foo->ptr = new Foo;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct Foo { int* ptr; };\n" "void f(Foo* foo) {\n" " delete foo->ptr;\n" " x = *foo->ptr;\n" "}\n", dinit(CheckOptions, $.cpp = true)); TODO_ASSERT_EQUALS("[test.cpp:4:6]: (error) Dereferencing 'ptr' after it is deallocated / released [deallocuse]\n", "", errout_str()); check("void parse() {\n" " struct Buf {\n" " Buf(uint32_t len) : m_buf(new uint8_t[len]) {}\n" " ~Buf() { delete[]m_buf; }\n" " uint8_t *m_buf;\n" " };\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct Foo {\n" " Foo();\n" " Foo* ptr;\n" " void func();\n" "};\n" "void bar(Foo* foo) {\n" " delete foo->ptr;\n" " foo->ptr = new Foo;\n" " foo->ptr->func();\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("void foo(void (*conv)(char**)) {\n" " char * ptr=(char*)malloc(42);\n" " free(ptr);\n" " (*conv)(&ptr);\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void deallocuse8() { // #1765 check("void f() {\n" " int *ptr = new int;\n" " delete(ptr);\n" " *ptr = 0;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4:6]: (error) Dereferencing 'ptr' after it is deallocated / released [deallocuse]\n", errout_str()); } void deallocuse9() { check("void f(Type* p) {\n" // #9781 " std::shared_ptr<Type> sp(p);\n" " bool b = p->foo();\n" " return b;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("struct A {\n" // #8635 " std::vector<std::unique_ptr<A>> array_;\n" " A* foo() {\n" " A* a = new A();\n" " array_.push_back(std::unique_ptr<A>(a));\n" " return a;\n" " }\n" "};\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); check("int g(int *p) {\n" // #9838 " std::unique_ptr<int> temp(p);\n" " return DoSomething(p);\n" "}\n" "int f() {\n" " return g(new int(3));\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("", errout_str()); } void deallocuse10() { check("void f(char* p) {\n" " free(p);\n" " p[0] = 10;\n" "}\n"); ASSERT_EQUALS("[test.c:3:5]: (error) Dereferencing 'p' after it is deallocated / released [deallocuse]\n", errout_str()); check("int f(int* p) {\n" " free(p);\n" " return p[1];\n" "}\n"); ASSERT_EQUALS("[test.c:2:5] -> [test.c:3:5]: (error) Returning/dereferencing 'p' after it is deallocated / released [deallocret]\n", errout_str()); } void deallocuse11() { // #8302 check("int f() {\n" " int *array = new int[42];\n" " delete [] array;\n" " return array[1];" // << "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:3:3] -> [test.cpp:4:3]: (error) Returning/dereferencing 'array' after it is deallocated / released [deallocret]\n", errout_str()); check("int f() {\n" " int *array = (int*)malloc(40);\n" " free(array);\n" " return array[1];" // << "}\n"); ASSERT_EQUALS("[test.c:3:3] -> [test.c:4:3]: (error) Returning/dereferencing 'array' after it is deallocated / released [deallocret]\n", errout_str()); } void deallocuse12() { check("struct foo { int x; }\n" "void f1(struct foo *f) {\n" " free(f);\n" "}\n" "void f2(struct foo *f, int *out) {\n" " *out = f->x;\n" "}\n"); ASSERT_EQUALS("", errout_str()); } void deallocuse13() { check("void f() {\n" // #9695 " auto* a = new int[2];\n" " delete[] a;\n" " a[1] = 0;\n" " auto* b = static_cast<int*>(malloc(8));\n" " free(b);\n" " b[1] = 0;\n" "}\n", dinit(CheckOptions, $.cpp = true)); ASSERT_EQUALS("[test.cpp:4:5]: (error) Dereferencing 'a' after it is deallocated / released [deallocuse]\n" "[test.cpp:7:5]: (error) Dereferencing 'b' after it is deallocated / released [deallocuse]\n", |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |