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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
|
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 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 "checkfunctions.h"
#include "errortypes.h"
#include "fixture.h"
#include "helpers.h"
#include "platform.h"
#include "settings.h"
#include "standards.h"
#include <cstddef>
#include <string>
class TestFunctions : public TestFixture {
public:
TestFunctions() : TestFixture("TestFunctions") {}
private:
const Settings settings = settingsBuilder().severity(Severity::style).severity(Severity::warning).severity(Severity::performance).severity(Severity::portability).
certainty(Certainty::inconclusive).c(Standards::C11).cpp(Standards::CPP11).library("std.cfg").library("posix.cfg").build();
void run() override {
// Prohibited functions
TEST_CASE(prohibitedFunctions_posix);
TEST_CASE(prohibitedFunctions_index);
TEST_CASE(prohibitedFunctions_qt_index); // FP when using the Qt function 'index'?
TEST_CASE(prohibitedFunctions_rindex);
TEST_CASE(prohibitedFunctions_var); // no false positives for variables
TEST_CASE(prohibitedFunctions_gets); // dangerous function
TEST_CASE(prohibitedFunctions_alloca);
TEST_CASE(prohibitedFunctions_declaredFunction); // declared function ticket #3121
TEST_CASE(prohibitedFunctions_std_gets); // test std::gets
TEST_CASE(prohibitedFunctions_multiple); // multiple use of obsolete functions
TEST_CASE(prohibitedFunctions_c_declaration); // c declared function
TEST_CASE(prohibitedFunctions_functionWithBody); // function with body
TEST_CASE(prohibitedFunctions_crypt); // Non-reentrant function
TEST_CASE(prohibitedFunctions_namespaceHandling);
// Invalid function usage
TEST_CASE(invalidFunctionUsage1);
TEST_CASE(invalidFunctionUsageStrings);
// Invalid function argument
TEST_CASE(invalidFunctionArg1);
// Math function usage
TEST_CASE(mathfunctionCall_fmod);
TEST_CASE(mathfunctionCall_sqrt);
TEST_CASE(mathfunctionCall_log);
TEST_CASE(mathfunctionCall_acos);
TEST_CASE(mathfunctionCall_asin);
TEST_CASE(mathfunctionCall_pow);
TEST_CASE(mathfunctionCall_atan2);
TEST_CASE(mathfunctionCall_precision);
// Ignored return value
TEST_CASE(checkIgnoredReturnValue);
TEST_CASE(checkIgnoredErrorCode);
// memset..
TEST_CASE(memsetZeroBytes);
TEST_CASE(memsetInvalid2ndParam);
// missing "return"
TEST_CASE(checkMissingReturn1);
TEST_CASE(checkMissingReturn2); // #11798
TEST_CASE(checkMissingReturn3);
TEST_CASE(checkMissingReturn4);
TEST_CASE(checkMissingReturn5);
TEST_CASE(checkMissingReturn6); // #13180
// std::move for locar variable
TEST_CASE(returnLocalStdMove1);
TEST_CASE(returnLocalStdMove2);
TEST_CASE(returnLocalStdMove3);
TEST_CASE(returnLocalStdMove4);
TEST_CASE(returnLocalStdMove5);
TEST_CASE(negativeMemoryAllocationSizeError); // #389
TEST_CASE(checkLibraryMatchFunctions);
TEST_CASE(checkUseStandardLibrary1);
TEST_CASE(checkUseStandardLibrary2);
TEST_CASE(checkUseStandardLibrary3);
TEST_CASE(checkUseStandardLibrary4);
TEST_CASE(checkUseStandardLibrary5);
TEST_CASE(checkUseStandardLibrary6);
TEST_CASE(checkUseStandardLibrary7);
TEST_CASE(checkUseStandardLibrary8);
TEST_CASE(checkUseStandardLibrary9);
TEST_CASE(checkUseStandardLibrary10);
TEST_CASE(checkUseStandardLibrary11);
TEST_CASE(checkUseStandardLibrary12);
TEST_CASE(checkUseStandardLibrary13);
TEST_CASE(checkUseStandardLibrary14);
}
struct CheckOptions
{
CheckOptions() = default;
bool cpp = true;
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& s = options.s ? *options.s : settings;
// Tokenize..
SimpleTokenizer tokenizer(s, *this);
ASSERT_LOC(tokenizer.tokenize(code, options.cpp), file, line);
runChecks<CheckFunctions>(tokenizer, this);
}
void prohibitedFunctions_posix() {
check("void f()\n"
"{\n"
" bsd_signal(SIGABRT, SIG_IGN);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolescent function 'bsd_signal' called. It is recommended to use 'sigaction' instead.\n", errout_str());
check("int f()\n"
"{\n"
" int bsd_signal(0);\n"
" return bsd_signal;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f()\n"
"{\n"
" struct hostent *hp;\n"
" if(!hp = gethostbyname(\"127.0.0.1\")) {\n"
" exit(1);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Obsolescent function 'gethostbyname' called. It is recommended to use 'getaddrinfo' instead.\n", errout_str());
check("void f()\n"
"{\n"
" long addr;\n"
" addr = inet_addr(\"127.0.0.1\");\n"
" if(!hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET)) {\n"
" exit(1);\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (style) Obsolescent function 'gethostbyaddr' called. It is recommended to use 'getnameinfo' instead.\n", errout_str());
check("void f()\n"
"{\n"
" usleep( 1000 );\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Obsolescent function 'usleep' called. It is recommended to use 'nanosleep' or 'setitimer' instead.\n", errout_str());
}
void prohibitedFunctions_index() {
check("namespace n1 {\n"
" int index(){ return 1; };\n"
"}\n"
"int main()\n"
"{\n"
" n1::index();\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("std::size_t f()\n"
"{\n"
" std::size_t index(0);\n"
" index++;\n"
" return index;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int f()\n"
"{\n"
" return this->index();\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f()\n"
"{\n"
" int index( 0 );\n"
"}");
ASSERT_EQUALS("", errout_str());
check("const char f()\n"
"{\n"
" const char var[6] = \"index\";\n"
" const char i = index(var, 0);\n"
" return i;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Obsolescent function 'index' called. It is recommended to use 'strchr' instead.\n",
errout_str());
}
void prohibitedFunctions_qt_index() {
check("void TDataModel::forceRowRefresh(int row) {\n"
" emit dataChanged(index(row, 0), index(row, columnCount() - 1));\n"
"}");
ASSERT_EQUALS(
"[test.cpp:2]: (style) Obsolescent function 'index' called. It is recommended to use 'strchr' instead.\n"
"[test.cpp:2]: (style) Obsolescent function 'index' called. It is recommended to use 'strchr' instead.\n", // duplicate
errout_str());
}
void prohibitedFunctions_rindex() {
check("void f()\n"
"{\n"
" int rindex( 0 );\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f()\n"
"{\n"
" const char var[7] = \"rindex\";\n"
" print(rindex(var, 0));\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (style) Obsolescent function 'rindex' called. It is recommended to use 'strrchr' instead.\n", errout_str());
}
void prohibitedFunctions_var() {
check("class Fred {\n"
"public:\n"
" Fred() : index(0) { }\n"
" int index;\n"
"};");
ASSERT_EQUALS("", errout_str());
}
void prohibitedFunctions_gets() {
check("void f()\n"
"{\n"
" char *x = gets(a);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Obsolete function 'gets' called. It is recommended to use 'fgets' or 'gets_s' instead.\n", errout_str());
check("void f()\n"
"{\n"
" foo(x, gets(a));\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Obsolete function 'gets' called. It is recommended to use 'fgets' or 'gets_s' instead.\n", errout_str());
}
void prohibitedFunctions_alloca() {
check("void f()\n"
"{\n"
" char *x = alloca(10);\n"
"}"); // #4382 - there are no VLAs in C++
ASSERT_EQUALS("[test.cpp:3]: (warning) Obsolete function 'alloca' called.\n", errout_str());
check("void f()\n"
"{\n"
" char *x = alloca(10);\n"
"}", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("[test.c:3]: (warning) Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n", errout_str());
const Settings s = settingsBuilder(settings).c(Standards::C89).cpp(Standards::CPP03).build();
check("void f()\n"
"{\n"
" char *x = alloca(10);\n"
"}", dinit(CheckOptions, $.s = &s)); // #4382 - there are no VLAs in C++
ASSERT_EQUALS("", errout_str());
check("void f()\n"
"{\n"
" char *x = alloca(10);\n"
"}", dinit(CheckOptions, $.cpp = false, $.s = &s)); // #7558 - no alternative to alloca in C89
ASSERT_EQUALS("", errout_str());
check("void f()\n"
"{\n"
" char *x = alloca(10);\n"
"}", dinit(CheckOptions, $.cpp = false, $.s = &s));
ASSERT_EQUALS("", errout_str());
}
// ticket #3121
void prohibitedFunctions_declaredFunction() {
check("int ftime ( int a )\n"
"{\n"
" return a;\n"
"}\n"
"int main ()\n"
"{\n"
" int b ; b = ftime ( 1 ) ;\n"
" return 0 ;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
// test std::gets
void prohibitedFunctions_std_gets() {
check("void f(char * str)\n"
"{\n"
" char *x = std::gets(str);\n"
" char *y = gets(str);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Obsolete function 'gets' called. It is recommended to use 'fgets' or 'gets_s' instead.\n"
"[test.cpp:4]: (warning) Obsolete function 'gets' called. It is recommended to use 'fgets' or 'gets_s' instead.\n", errout_str());
}
// multiple use
void prohibitedFunctions_multiple() {
check("void f(char * str)\n"
"{\n"
" char *x = std::gets(str);\n"
" usleep( 1000 );\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Obsolete function 'gets' called. It is recommended to use 'fgets' or 'gets_s' instead.\n"
"[test.cpp:4]: (style) Obsolescent function 'usleep' called. It is recommended to use 'nanosleep' or 'setitimer' instead.\n", errout_str());
}
void prohibitedFunctions_c_declaration() {
check("char * gets ( char * c ) ;\n"
"int main ()\n"
"{\n"
" char s [ 10 ] ;\n"
" gets ( s ) ;\n"
" return 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (warning) Obsolete function 'gets' called. It is recommended to use 'fgets' or 'gets_s' instead.\n", errout_str());
check("int getcontext(ucontext_t *ucp);\n"
"void f (ucontext_t *ucp)\n"
"{\n"
" getcontext ( ucp ) ;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (portability) Obsolescent function 'getcontext' called. Applications are recommended to be rewritten to use POSIX threads.\n", errout_str());
}
void prohibitedFunctions_functionWithBody() {
check("char * gets ( char * c ) { return c; }\n"
"int main ()\n"
"{\n"
" char s [ 10 ] ;\n"
" gets ( s ) ;\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void prohibitedFunctions_crypt() {
check("void f(char *pwd)\n"
"{\n"
" char *cpwd;"
" crypt(pwd, cpwd);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function crypt() is not used.\n"
"[test.cpp:3]: (portability) Non reentrant function 'crypt' called. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'.\n", errout_str());
check("void f()\n"
"{\n"
" char *pwd = getpass(\"Password:\");"
" char *cpwd;"
" crypt(pwd, cpwd);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function crypt() is not used.\n"
"[test.cpp:3]: (portability) Non reentrant function 'crypt' called. For threadsafe applications it is recommended to use the reentrant replacement function 'crypt_r'.\n", errout_str());
check("int f()\n"
"{\n"
" int crypt = 0;"
" return crypt;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void prohibitedFunctions_namespaceHandling() {
check("void f()\n"
"{\n"
" time_t t = 0;"
" auto lt = std::localtime(&t);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'localtime' called. For threadsafe applications it is recommended to use the reentrant replacement function 'localtime_r'.\n", errout_str());
// Passed as function argument
check("void f()\n"
"{\n"
" printf(\"Magic guess: %d\", getpwent());\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'getpwent' called. For threadsafe applications it is recommended to use the reentrant replacement function 'getpwent_r'.\n", errout_str());
// Pass return value
check("void f()\n"
"{\n"
" time_t t = 0;"
" struct tm *foo = localtime(&t);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Non reentrant function 'localtime' called. For threadsafe applications it is recommended to use the reentrant replacement function 'localtime_r'.\n", errout_str());
// Access via global namespace
check("void f()\n"
"{\n"
" ::getpwent();\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function ::getpwent() is not used.\n"
"[test.cpp:3]: (portability) Non reentrant function 'getpwent' called. For threadsafe applications it is recommended to use the reentrant replacement function 'getpwent_r'.\n", errout_str());
// Be quiet on function definitions
check("int getpwent()\n"
"{\n"
" return 123;\n"
"}");
ASSERT_EQUALS("", errout_str());
// Be quiet on other namespaces
check("void f()\n"
"{\n"
" foobar::getpwent();\n"
"}");
ASSERT_EQUALS("", errout_str());
// Be quiet on class member functions
check("void f()\n"
"{\n"
" foobar.getpwent();\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void invalidFunctionUsage1() {
check("void f() { memset(a,b,sizeof(a)!=12); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout_str());
check("void f() { memset(a,b,sizeof(a)!=0); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout_str());
check("void f() { memset(a,b,!c); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout_str());
// Ticket #6990
check("void f(bool c) { memset(a,b,c); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout_str());
check("void f() { memset(a,b,true); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout_str());
// Ticket #6588 (c mode)
check("void record(char* buf, int n) {\n"
" memset(buf, 0, n < 255);\n" /* KO */
" memset(buf, 0, n < 255 ? n : 255);\n" /* OK */
"}", dinit(CheckOptions, $.cpp = false));
ASSERT_EQUALS("[test.c:2]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout_str());
// Ticket #6588 (c++ mode)
check("void record(char* buf, int n) {\n"
" memset(buf, 0, n < 255);\n" /* KO */
" memset(buf, 0, n < 255 ? n : 255);\n" /* OK */
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Invalid memset() argument nr 3. A non-boolean value is required.\n", errout_str());
check("int boolArgZeroIsInvalidButOneIsValid(int a, int param) {\n"
" return div(a, param > 0);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Invalid div() argument nr 2. The value is 0 or 1 (boolean) but the valid values are ':-1,1:'.\n", errout_str());
check("void boolArgZeroIsValidButOneIsInvalid(int param) {\n"
" strtol(a, b, param > 0);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Invalid strtol() argument nr 3. The value is 0 or 1 (boolean) but the valid values are '0,2:36'.\n", errout_str());
check("void f() { strtol(a,b,1); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strtol() argument nr 3. The value is 1 but the valid values are '0,2:36'.\n", errout_str());
check("void f() { strtol(a,b,10); }");
ASSERT_EQUALS("", errout_str());
check("void f(std::vector<int>& v) {\n" // #10754
" int N = -1;\n"
" for (long i = 0; i < g(); i++)\n"
" N = h(N);\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (warning) Invalid v.resize() argument nr 1. The value is -1 but the valid values are '0:'.\n", errout_str());
check("void f(std::vector<int>& v, int N) {\n"
" if (N < -1)\n"
" return;\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Either the condition 'N<-1' is redundant or v.resize() argument nr 1 can have invalid value. The value is -1 but the valid values are '0:'.\n",
errout_str());
check("void f(std::vector<int>& v, int N) {\n"
" if (N == -1) {}\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'N==-1' is redundant or v.resize() argument nr 1 can have invalid value. The value is -1 but the valid values are '0:'.\n",
errout_str());
check("void f(std::vector<int>& v, int N, bool b) {\n"
" if (b)\n"
" N = -1;\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Invalid v.resize() argument nr 1. The value is -1 but the valid values are '0:'.\n",
errout_str());
check("void f(std::vector<int>& v) {\n"
" int N = -1;\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid v.resize() argument nr 1. The value is -1 but the valid values are '0:'.\n",
errout_str());
}
void invalidFunctionUsageStrings() {
check("size_t f() { char x = 'x'; return strlen(&x); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("size_t f() { return strlen(&x); }");
ASSERT_EQUALS("", errout_str());
check("size_t f(char x) { return strlen(&x); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("size_t f() { char x = '\\0'; return strlen(&x); }");
ASSERT_EQUALS("", errout_str());
check("size_t f() {\n"
" char x;\n"
" if (y)\n"
" x = '\\0';\n"
" else\n"
" x = 'a';\n"
" return strlen(&x);\n"
"}");
ASSERT_EQUALS("[test.cpp:7]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("int f() { char x = '\\0'; return strcmp(\"Hello world\", &x); }");
ASSERT_EQUALS("", errout_str());
check("int f() { char x = 'x'; return strcmp(\"Hello world\", &x); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strcmp() argument nr 2. A nul-terminated string is required.\n", errout_str());
check("size_t f(char x) { char * y = &x; return strlen(y); }");
TODO_ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", "", errout_str());
check("size_t f(char x) { char * y = &x; char *z = y; return strlen(z); }");
TODO_ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", "", errout_str());
check("size_t f() { char x = 'x'; char * y = &x; char *z = y; return strlen(z); }");
TODO_ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", "", errout_str());
check("size_t f() { char x = '\\0'; char * y = &x; char *z = y; return strlen(z); }");
ASSERT_EQUALS("", errout_str());
check("size_t f() { char x[] = \"Hello world\"; return strlen(x); }");
ASSERT_EQUALS("", errout_str());
check("size_t f(char x[]) { return strlen(x); }");
ASSERT_EQUALS("", errout_str());
check("int f(char x, char y) { return strcmp(&x, &y); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strcmp() argument nr 1. A nul-terminated string is required.\n"
"[test.cpp:1]: (error) Invalid strcmp() argument nr 2. A nul-terminated string is required.\n", errout_str());
check("size_t f() { char x[] = \"Hello world\"; return strlen(&x[0]); }");
ASSERT_EQUALS("", errout_str());
check("size_t f() { char* x = \"Hello world\"; return strlen(&x[0]); }");
ASSERT_EQUALS("", errout_str());
check("struct S {\n"
" char x;\n"
"};\n"
"size_t f() {\n"
" S s1 = {0};\n"
" S s2;\n;"
" s2.x = 'x';\n"
" size_t l1 = strlen(&s1.x);\n"
" size_t l2 = strlen(&s2.x);\n"
" return l1 + l2;\n"
"}");
ASSERT_EQUALS("[test.cpp:8]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n"
"[test.cpp:9]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("const char x = 'x'; size_t f() { return strlen(&x); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("struct someStruct {\n"
" union {\n"
" struct {\n"
" uint16_t nr;\n"
" uint8_t d[40];\n"
" } data;\n"
" char buf[42];\n"
" } x;\n"
"};\n"
"int f(struct someStruct * const tp, const int k)\n"
"{\n"
" return strcmp(&tp->x.buf[k], \"needle\");\n"
"}");
ASSERT_EQUALS("", errout_str());
check("struct someStruct {\n"
" char buf[42];\n"
"};\n"
"int f(struct someStruct * const tp, const int k)\n"
"{\n"
" return strcmp(&tp->buf[k], \"needle\");\n"
"}");
ASSERT_EQUALS("", errout_str());
check("const char x = 'x'; size_t f() { char y = x; return strlen(&y); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("const char x = '\\0'; size_t f() { return strlen(&x); }");
ASSERT_EQUALS("", errout_str());
check("const char x = '\\0'; size_t f() { char y = x; return strlen(&y); }");
ASSERT_EQUALS("", errout_str());
check("size_t f() {\n"
" char * a = \"Hello world\";\n"
" char ** b = &a;\n"
" return strlen(&b[0][0]);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("size_t f() {\n"
" char ca[] = \"asdf\";\n"
" return strlen((char*) &ca);\n"
"}");
ASSERT_EQUALS("", errout_str());
// #5225
check("int main(void)\n"
"{\n"
" char str[80] = \"hello worl\";\n"
" char d = 'd';\n"
" strcat(str, &d);\n"
" puts(str);\n"
" return 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Invalid strcat() argument nr 2. A nul-terminated string is required.\n", errout_str());
check("FILE* f(void) {\n"
" const char fileName[1] = { \'x\' };\n"
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid fopen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("FILE* f(void) {\n"
" const char fileName[2] = { \'x\', \'y\' };\n"
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid fopen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("FILE* f(void) {\n"
" const char fileName[3] = { \'x\', \'y\' ,\'\\0\' };\n"
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("", errout_str());
check("FILE* f(void) {\n"
" const char fileName[3] = { \'x\', \'\\0\' ,\'y\' };\n"
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("", errout_str());
check("FILE* f(void) {\n"
" const char fileName[3] = { \'x\', \'y\' };\n" // implicit '\0' added at the end
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("", errout_str());
check("FILE* f(void) {\n"
" const char fileName[] = { \'\\0\' };\n"
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("", errout_str());
check("FILE* f(void) {\n"
" const char fileName[] = { \'0\' + 42 };\n" // no size is explicitly defined, no implicit '\0' is added
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid fopen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("FILE* f(void) {\n"
" const char fileName[] = { \'0\' + 42, \'x\' };\n" // no size is explicitly defined, no implicit '\0' is added
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid fopen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("FILE* f(void) {\n"
" const char fileName[2] = { \'0\' + 42 };\n" // implicitly '\0' added at the end because size is set to 2
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("", errout_str());
check("FILE* f(void) {\n"
" const char fileName[] = { };\n"
" return fopen(fileName, \"r\"); \n"
"}");
ASSERT_EQUALS("", errout_str());
check("void scanMetaTypes()\n" // don't crash
"{\n"
" QVector<int> metaTypes;\n"
" for (int mtId = 0; mtId <= QMetaType::User; ++mtId) {\n"
" const auto name = QMetaType::typeName(mtId);\n"
" if (strstr(name, \"GammaRay::\") != name)\n"
" metaTypes.push_back(mtId);\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int f() {\n"
" const char c[3] = \"abc\";\n"
" return strlen(c);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid strlen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("int f() {\n"
" const wchar_t c[3] = L\"abc\";\n"
" return wcslen(c);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid wcslen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("void f(char* dest) {\n"
" char if_name[(IF_NAMESIZE > 21 ? IF_NAMESIZE : 21) + 1] = \"%\";\n"
" strcat(dest, if_name);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("int f() {\n"
" const char c[3] = \"ab\\0\";\n"
" return strlen(c);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("void f(int n) {\n" // #11179
" char s[8] = \" \";\n"
" n = (n + 1) % 100;\n"
" sprintf(s, \"lwip%02d\", n);\n"
" s[strlen(s)] = ' ';\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("size_t f() { wchar_t x = L'x'; return wcslen(&x); }");
ASSERT_EQUALS("[test.cpp:1]: (error) Invalid wcslen() argument nr 1. A nul-terminated string is required.\n", errout_str());
check("void f() { char a[10] = \"1234567890\"; puts(a); }", dinit(CheckOptions, $.cpp = false)); // #1770
ASSERT_EQUALS("[test.c:1]: (error) Invalid puts() argument nr 1. A nul-terminated string is required.\n", errout_str());
}
void invalidFunctionArg1() {
const Settings settingsUnix32 = settingsBuilder(settings).platform(Platform::Unix32).build();
check("int main() {\n"
" char tgt[7];\n"
" char src[7+1] = \"7777777\";\n"
" if (sizeof tgt <= sizeof src) {\n"
" memmove(&tgt, &src, sizeof tgt);\n"
" } else {\n"
" memmove(&tgt, &src, sizeof src);\n"
" memset(&tgt + sizeof src, ' ', sizeof tgt - sizeof src);\n"
" }\n"
"}\n", dinit(CheckOptions, $.cpp = false, $.s = &settingsUnix32));
ASSERT_EQUALS("", errout_str());
}
void mathfunctionCall_sqrt() {
// sqrt, sqrtf, sqrtl
check("void foo()\n"
"{\n"
" std::cout << sqrt(-1) << std::endl;\n"
" std::cout << sqrtf(-1) << std::endl;\n"
" std::cout << sqrtl(-1) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid sqrt() argument nr 1. The value is -1 but the valid values are '0.0:'.\n"
"[test.cpp:4]: (error) Invalid sqrtf() argument nr 1. The value is -1 but the valid values are '0.0:'.\n"
"[test.cpp:5]: (error) Invalid sqrtl() argument nr 1. The value is -1 but the valid values are '0.0:'.\n", errout_str());
// implementation-defined behaviour for "finite values of x<0" only:
check("void foo()\n"
"{\n"
" std::cout << sqrt(-0.) << std::endl;\n"
" std::cout << sqrtf(-0.) << std::endl;\n"
" std::cout << sqrtl(-0.) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void foo()\n"
"{\n"
" std::cout << sqrt(1) << std::endl;\n"
" std::cout << sqrtf(1) << std::endl;\n"
" std::cout << sqrtl(1) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void mathfunctionCall_log() {
// log,log10,logf,logl,log10f,log10l,log2,log2f,log2l,log1p,log1pf,log1pl
check("void foo()\n"
"{\n"
" std::cout << log(-2) << std::endl;\n"
" std::cout << logf(-2) << std::endl;\n"
" std::cout << logl(-2) << std::endl;\n"
" std::cout << log10(-2) << std::endl;\n"
" std::cout << log10f(-2) << std::endl;\n"
" std::cout << log10l(-2) << std::endl;\n"
" std::cout << log2(-2) << std::endl;\n"
" std::cout << log2f(-2) << std::endl;\n"
" std::cout << log2l(-2) << std::endl;\n"
" std::cout << log1p(-3) << std::endl;\n"
" std::cout << log1pf(-3) << std::endl;\n"
" std::cout << log1pl(-3) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid log() argument nr 1. The value is -2 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:4]: (error) Invalid logf() argument nr 1. The value is -2 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:5]: (error) Invalid logl() argument nr 1. The value is -2 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:6]: (error) Invalid log10() argument nr 1. The value is -2 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:7]: (error) Invalid log10f() argument nr 1. The value is -2 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:8]: (error) Invalid log10l() argument nr 1. The value is -2 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:9]: (error) Invalid log2() argument nr 1. The value is -2 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:10]: (error) Invalid log2f() argument nr 1. The value is -2 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:11]: (error) Invalid log2l() argument nr 1. The value is -2 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:3]: (warning) Passing value -2 to log() leads to implementation-defined result.\n"
"[test.cpp:4]: (warning) Passing value -2 to logf() leads to implementation-defined result.\n"
"[test.cpp:5]: (warning) Passing value -2 to logl() leads to implementation-defined result.\n"
"[test.cpp:6]: (warning) Passing value -2 to log10() leads to implementation-defined result.\n"
"[test.cpp:7]: (warning) Passing value -2 to log10f() leads to implementation-defined result.\n"
"[test.cpp:8]: (warning) Passing value -2 to log10l() leads to implementation-defined result.\n"
"[test.cpp:9]: (warning) Passing value -2 to log2() leads to implementation-defined result.\n"
"[test.cpp:10]: (warning) Passing value -2 to log2f() leads to implementation-defined result.\n"
"[test.cpp:11]: (warning) Passing value -2 to log2l() leads to implementation-defined result.\n"
"[test.cpp:12]: (warning) Passing value -3 to log1p() leads to implementation-defined result.\n"
"[test.cpp:13]: (warning) Passing value -3 to log1pf() leads to implementation-defined result.\n"
"[test.cpp:14]: (warning) Passing value -3 to log1pl() leads to implementation-defined result.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << log(-1) << std::endl;\n"
" std::cout << logf(-1) << std::endl;\n"
" std::cout << logl(-1) << std::endl;\n"
" std::cout << log10(-1) << std::endl;\n"
" std::cout << log10f(-1) << std::endl;\n"
" std::cout << log10l(-1) << std::endl;\n"
" std::cout << log2(-1) << std::endl;\n"
" std::cout << log2f(-1) << std::endl;\n"
" std::cout << log2l(-1) << std::endl;\n"
" std::cout << log1p(-2) << std::endl;\n"
" std::cout << log1pf(-2) << std::endl;\n"
" std::cout << log1pl(-2) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid log() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:4]: (error) Invalid logf() argument nr 1. The value is -1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:5]: (error) Invalid logl() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:6]: (error) Invalid log10() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:7]: (error) Invalid log10f() argument nr 1. The value is -1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:8]: (error) Invalid log10l() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:9]: (error) Invalid log2() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:10]: (error) Invalid log2f() argument nr 1. The value is -1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:11]: (error) Invalid log2l() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:3]: (warning) Passing value -1 to log() leads to implementation-defined result.\n"
"[test.cpp:4]: (warning) Passing value -1 to logf() leads to implementation-defined result.\n"
"[test.cpp:5]: (warning) Passing value -1 to logl() leads to implementation-defined result.\n"
"[test.cpp:6]: (warning) Passing value -1 to log10() leads to implementation-defined result.\n"
"[test.cpp:7]: (warning) Passing value -1 to log10f() leads to implementation-defined result.\n"
"[test.cpp:8]: (warning) Passing value -1 to log10l() leads to implementation-defined result.\n"
"[test.cpp:9]: (warning) Passing value -1 to log2() leads to implementation-defined result.\n"
"[test.cpp:10]: (warning) Passing value -1 to log2f() leads to implementation-defined result.\n"
"[test.cpp:11]: (warning) Passing value -1 to log2l() leads to implementation-defined result.\n"
"[test.cpp:12]: (warning) Passing value -2 to log1p() leads to implementation-defined result.\n"
"[test.cpp:13]: (warning) Passing value -2 to log1pf() leads to implementation-defined result.\n"
"[test.cpp:14]: (warning) Passing value -2 to log1pl() leads to implementation-defined result.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << log(-1.0) << std::endl;\n"
" std::cout << logf(-1.0) << std::endl;\n"
" std::cout << logl(-1.0) << std::endl;\n"
" std::cout << log10(-1.0) << std::endl;\n"
" std::cout << log10f(-1.0) << std::endl;\n"
" std::cout << log10l(-1.0) << std::endl;\n"
" std::cout << log2(-1.0) << std::endl;\n"
" std::cout << log2f(-1.0) << std::endl;\n"
" std::cout << log2l(-1.0) << std::endl;\n"
" std::cout << log1p(-2.0) << std::endl;\n"
" std::cout << log1pf(-2.0) << std::endl;\n"
" std::cout << log1pl(-2.0) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid log() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:4]: (error) Invalid logf() argument nr 1. The value is -1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:5]: (error) Invalid logl() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:6]: (error) Invalid log10() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:7]: (error) Invalid log10f() argument nr 1. The value is -1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:8]: (error) Invalid log10l() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:9]: (error) Invalid log2() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:10]: (error) Invalid log2f() argument nr 1. The value is -1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:11]: (error) Invalid log2l() argument nr 1. The value is -1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:3]: (warning) Passing value -1.0 to log() leads to implementation-defined result.\n"
"[test.cpp:4]: (warning) Passing value -1.0 to logf() leads to implementation-defined result.\n"
"[test.cpp:5]: (warning) Passing value -1.0 to logl() leads to implementation-defined result.\n"
"[test.cpp:6]: (warning) Passing value -1.0 to log10() leads to implementation-defined result.\n"
"[test.cpp:7]: (warning) Passing value -1.0 to log10f() leads to implementation-defined result.\n"
"[test.cpp:8]: (warning) Passing value -1.0 to log10l() leads to implementation-defined result.\n"
"[test.cpp:9]: (warning) Passing value -1.0 to log2() leads to implementation-defined result.\n"
"[test.cpp:10]: (warning) Passing value -1.0 to log2f() leads to implementation-defined result.\n"
"[test.cpp:11]: (warning) Passing value -1.0 to log2l() leads to implementation-defined result.\n"
"[test.cpp:12]: (warning) Passing value -2.0 to log1p() leads to implementation-defined result.\n"
"[test.cpp:13]: (warning) Passing value -2.0 to log1pf() leads to implementation-defined result.\n"
"[test.cpp:14]: (warning) Passing value -2.0 to log1pl() leads to implementation-defined result.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << log(-0.1) << std::endl;\n"
" std::cout << logf(-0.1) << std::endl;\n"
" std::cout << logl(-0.1) << std::endl;\n"
" std::cout << log10(-0.1) << std::endl;\n"
" std::cout << log10f(-0.1) << std::endl;\n"
" std::cout << log10l(-0.1) << std::endl;\n"
" std::cout << log2(-0.1) << std::endl;\n"
" std::cout << log2f(-0.1) << std::endl;\n"
" std::cout << log2l(-0.1) << std::endl;\n"
" std::cout << log1p(-1.1) << std::endl;\n"
" std::cout << log1pf(-1.1) << std::endl;\n"
" std::cout << log1pl(-1.1) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid log() argument nr 1. The value is -0.1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:4]: (error) Invalid logf() argument nr 1. The value is -0.1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:5]: (error) Invalid logl() argument nr 1. The value is -0.1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:6]: (error) Invalid log10() argument nr 1. The value is -0.1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:7]: (error) Invalid log10f() argument nr 1. The value is -0.1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:8]: (error) Invalid log10l() argument nr 1. The value is -0.1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:9]: (error) Invalid log2() argument nr 1. The value is -0.1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:10]: (error) Invalid log2f() argument nr 1. The value is -0.1 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:11]: (error) Invalid log2l() argument nr 1. The value is -0.1 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:3]: (warning) Passing value -0.1 to log() leads to implementation-defined result.\n"
"[test.cpp:4]: (warning) Passing value -0.1 to logf() leads to implementation-defined result.\n"
"[test.cpp:5]: (warning) Passing value -0.1 to logl() leads to implementation-defined result.\n"
"[test.cpp:6]: (warning) Passing value -0.1 to log10() leads to implementation-defined result.\n"
"[test.cpp:7]: (warning) Passing value -0.1 to log10f() leads to implementation-defined result.\n"
"[test.cpp:8]: (warning) Passing value -0.1 to log10l() leads to implementation-defined result.\n"
"[test.cpp:9]: (warning) Passing value -0.1 to log2() leads to implementation-defined result.\n"
"[test.cpp:10]: (warning) Passing value -0.1 to log2f() leads to implementation-defined result.\n"
"[test.cpp:11]: (warning) Passing value -0.1 to log2l() leads to implementation-defined result.\n"
"[test.cpp:12]: (warning) Passing value -1.1 to log1p() leads to implementation-defined result.\n"
"[test.cpp:13]: (warning) Passing value -1.1 to log1pf() leads to implementation-defined result.\n"
"[test.cpp:14]: (warning) Passing value -1.1 to log1pl() leads to implementation-defined result.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << log(0) << std::endl;\n"
" std::cout << logf(0.) << std::endl;\n"
" std::cout << logl(0.0) << std::endl;\n"
" std::cout << log10(0.0) << std::endl;\n"
" std::cout << log10f(0) << std::endl;\n"
" std::cout << log10l(0.) << std::endl;\n"
" std::cout << log2(0.) << std::endl;\n"
" std::cout << log2f(0.0) << std::endl;\n"
" std::cout << log2l(0) << std::endl;\n"
" std::cout << log1p(-1.) << std::endl;\n"
" std::cout << log1pf(-1.0) << std::endl;\n"
" std::cout << log1pl(-1) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid log() argument nr 1. The value is 0 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:4]: (error) Invalid logf() argument nr 1. The value is 0 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:5]: (error) Invalid logl() argument nr 1. The value is 0 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:6]: (error) Invalid log10() argument nr 1. The value is 0 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:7]: (error) Invalid log10f() argument nr 1. The value is 0 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:8]: (error) Invalid log10l() argument nr 1. The value is 0 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:9]: (error) Invalid log2() argument nr 1. The value is 0 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:10]: (error) Invalid log2f() argument nr 1. The value is 0 but the valid values are '1.4013e-45:'.\n"
"[test.cpp:11]: (error) Invalid log2l() argument nr 1. The value is 0 but the valid values are '4.94066e-324:'.\n"
"[test.cpp:3]: (warning) Passing value 0 to log() leads to implementation-defined result.\n"
"[test.cpp:4]: (warning) Passing value 0. to logf() leads to implementation-defined result.\n"
"[test.cpp:5]: (warning) Passing value 0.0 to logl() leads to implementation-defined result.\n"
"[test.cpp:6]: (warning) Passing value 0.0 to log10() leads to implementation-defined result.\n"
"[test.cpp:7]: (warning) Passing value 0 to log10f() leads to implementation-defined result.\n"
"[test.cpp:8]: (warning) Passing value 0. to log10l() leads to implementation-defined result.\n"
"[test.cpp:9]: (warning) Passing value 0. to log2() leads to implementation-defined result.\n"
"[test.cpp:10]: (warning) Passing value 0.0 to log2f() leads to implementation-defined result.\n"
"[test.cpp:11]: (warning) Passing value 0 to log2l() leads to implementation-defined result.\n"
"[test.cpp:12]: (warning) Passing value -1. to log1p() leads to implementation-defined result.\n"
"[test.cpp:13]: (warning) Passing value -1.0 to log1pf() leads to implementation-defined result.\n"
"[test.cpp:14]: (warning) Passing value -1 to log1pl() leads to implementation-defined result.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << log(1E-3) << std::endl;\n"
" std::cout << logf(1E-3) << std::endl;\n"
" std::cout << logl(1E-3) << std::endl;\n"
" std::cout << log10(1E-3) << std::endl;\n"
" std::cout << log10f(1E-3) << std::endl;\n"
" std::cout << log10l(1E-3) << std::endl;\n"
" std::cout << log2(1E-3) << std::endl;\n"
" std::cout << log2f(1E-3) << std::endl;\n"
" std::cout << log2l(1E-3) << std::endl;\n"
" std::cout << log1p(-1+1E-3) << std::endl;\n"
" std::cout << log1pf(-1+1E-3) << std::endl;\n"
" std::cout << log1pl(-1+1E-3) << std::endl;\n"
" std::cout << log(1.0E-3) << std::endl;\n"
" std::cout << logf(1.0E-3) << std::endl;\n"
" std::cout << logl(1.0E-3) << std::endl;\n"
" std::cout << log10(1.0E-3) << std::endl;\n"
" std::cout << log10f(1.0E-3) << std::endl;\n"
" std::cout << log10l(1.0E-3) << std::endl;\n"
" std::cout << log2(1.0E-3) << std::endl;\n"
" std::cout << log2f(1.0E-3) << std::endl;\n"
" std::cout << log2l(1.0E-3) << std::endl;\n"
" std::cout << log1p(-1+1.0E-3) << std::endl;\n"
" std::cout << log1pf(-1+1.0E-3) << std::endl;\n"
" std::cout << log1pl(-1+1.0E-3) << std::endl;\n"
" std::cout << log(1.0E+3) << std::endl;\n"
" std::cout << logf(1.0E+3) << std::endl;\n"
" std::cout << logl(1.0E+3) << std::endl;\n"
" std::cout << log10(1.0E+3) << std::endl;\n"
" std::cout << log10f(1.0E+3) << std::endl;\n"
" std::cout << log10l(1.0E+3) << std::endl;\n"
" std::cout << log2(1.0E+3) << std::endl;\n"
" std::cout << log2f(1.0E+3) << std::endl;\n"
" std::cout << log2l(1.0E+3) << std::endl;\n"
" std::cout << log1p(1.0E+3) << std::endl;\n"
" std::cout << log1pf(1.0E+3) << std::endl;\n"
" std::cout << log1pl(1.0E+3) << std::endl;\n"
" std::cout << log(2.0) << std::endl;\n"
" std::cout << logf(2.0) << std::endl;\n"
" std::cout << logf(2.0f) << std::endl;\n"
" std::cout << log10(2.0) << std::endl;\n"
" std::cout << log10f(2.0) << std::endl;\n"
" std::cout << log10f(2.0f) << std::endl;\n"
" std::cout << log2(2.0) << std::endl;\n"
" std::cout << log2f(2.0) << std::endl;\n"
" std::cout << log2f(2.0f) << std::endl;\n"
" std::cout << log1p(2.0) << std::endl;\n"
" std::cout << log1pf(2.0) << std::endl;\n"
" std::cout << log1pf(2.0f) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void foo()\n"
"{\n"
" std::string *log(0);\n"
"}");
ASSERT_EQUALS("", errout_str());
// #3473 - no warning if "log" is a variable
check("Fred::Fred() : log(0) { }");
ASSERT_EQUALS("", errout_str());
// #5748
check("void f() { foo.log(0); }");
ASSERT_EQUALS("", errout_str());
}
void mathfunctionCall_acos() {
// acos, acosf, acosl
check("void foo()\n"
"{\n"
" return acos(-1) \n"
" + acos(0.1) \n"
" + acos(0.0001) \n"
" + acos(0.01) \n"
" + acos(1.0E-1) \n"
" + acos(-1.0E-1) \n"
" + acos(+1.0E-1) \n"
" + acos(0.1E-1) \n"
" + acos(+0.1E-1) \n"
" + acos(-0.1E-1) \n"
" + acosf(-1) \n"
" + acosf(0.1) \n"
" + acosf(0.0001) \n"
" + acosf(0.01) \n"
" + acosf(1.0E-1) \n"
" + acosf(-1.0E-1) \n"
" + acosf(+1.0E-1) \n"
" + acosf(0.1E-1) \n"
" + acosf(+0.1E-1) \n"
" + acosf(-0.1E-1) \n"
" + acosl(-1) \n"
" + acosl(0.1) \n"
" + acosl(0.0001) \n"
" + acosl(0.01) \n"
" + acosl(1.0E-1) \n"
" + acosl(-1.0E-1) \n"
" + acosl(+1.0E-1) \n"
" + acosl(0.1E-1) \n"
" + acosl(+0.1E-1) \n"
" + acosl(-0.1E-1);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void foo()\n"
"{\n"
" std::cout << acos(1.1) << std::endl;\n"
" std::cout << acosf(1.1) << std::endl;\n"
" std::cout << acosl(1.1) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid acos() argument nr 1. The value is 1.1 but the valid values are '-1.0:1.0'.\n"
"[test.cpp:4]: (error) Invalid acosf() argument nr 1. The value is 1.1 but the valid values are '-1.0:1.0'.\n"
"[test.cpp:5]: (error) Invalid acosl() argument nr 1. The value is 1.1 but the valid values are '-1.0:1.0'.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << acos(-1.1) << std::endl;\n"
" std::cout << acosf(-1.1) << std::endl;\n"
" std::cout << acosl(-1.1) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid acos() argument nr 1. The value is -1.1 but the valid values are '-1.0:1.0'.\n"
"[test.cpp:4]: (error) Invalid acosf() argument nr 1. The value is -1.1 but the valid values are '-1.0:1.0'.\n"
"[test.cpp:5]: (error) Invalid acosl() argument nr 1. The value is -1.1 but the valid values are '-1.0:1.0'.\n", errout_str());
}
void mathfunctionCall_asin() {
// asin, asinf, asinl
check("void foo()\n"
"{\n"
" return asin(1) \n"
" + asin(-1) \n"
" + asin(0.1) \n"
" + asin(0.0001) \n"
" + asin(0.01) \n"
" + asin(1.0E-1) \n"
" + asin(-1.0E-1) \n"
" + asin(+1.0E-1) \n"
" + asin(0.1E-1) \n"
" + asin(+0.1E-1) \n"
" + asin(-0.1E-1) \n"
" + asinf(1) \n"
" + asinf(-1) \n"
" + asinf(0.1) \n"
" + asinf(0.0001) \n"
" + asinf(0.01) \n"
" + asinf(1.0E-1) \n"
" + asinf(-1.0E-1) \n"
" + asinf(+1.0E-1) \n"
" + asinf(0.1E-1) \n"
" + asinf(+0.1E-1) \n"
" + asinf(-0.1E-1) \n"
" + asinl(1) \n"
" + asinl(-1) \n"
" + asinl(0.1) \n"
" + asinl(0.0001) \n"
" + asinl(0.01) \n"
" + asinl(1.0E-1) \n"
" + asinl(-1.0E-1) \n"
" + asinl(+1.0E-1) \n"
" + asinl(0.1E-1) \n"
" + asinl(+0.1E-1) \n"
" + asinl(-0.1E-1);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void foo()\n"
"{\n"
" std::cout << asin(1.1) << std::endl;\n"
" std::cout << asinf(1.1) << std::endl;\n"
" std::cout << asinl(1.1) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid asin() argument nr 1. The value is 1.1 but the valid values are '-1.0:1.0'.\n"
"[test.cpp:4]: (error) Invalid asinf() argument nr 1. The value is 1.1 but the valid values are '-1.0:1.0'.\n"
"[test.cpp:5]: (error) Invalid asinl() argument nr 1. The value is 1.1 but the valid values are '-1.0:1.0'.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << asin(-1.1) << std::endl;\n"
" std::cout << asinf(-1.1) << std::endl;\n"
" std::cout << asinl(-1.1) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid asin() argument nr 1. The value is -1.1 but the valid values are '-1.0:1.0'.\n"
"[test.cpp:4]: (error) Invalid asinf() argument nr 1. The value is -1.1 but the valid values are '-1.0:1.0'.\n"
"[test.cpp:5]: (error) Invalid asinl() argument nr 1. The value is -1.1 but the valid values are '-1.0:1.0'.\n", errout_str());
}
void mathfunctionCall_pow() {
// pow, powf, powl
check("void foo()\n"
"{\n"
" std::cout << pow(0,-10) << std::endl;\n"
" std::cout << powf(0,-10) << std::endl;\n"
" std::cout << powl(0,-10) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Passing values 0 and -10 to pow() leads to implementation-defined result.\n"
"[test.cpp:4]: (warning) Passing values 0 and -10 to powf() leads to implementation-defined result.\n"
"[test.cpp:5]: (warning) Passing values 0 and -10 to powl() leads to implementation-defined result.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << pow(0,10) << std::endl;\n"
" std::cout << powf(0,10) << std::endl;\n"
" std::cout << powl(0,10) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void mathfunctionCall_atan2() {
// atan2
check("void foo()\n"
"{\n"
" std::cout << atan2(1,1) ;\n"
" std::cout << atan2(-1,-1) ;\n"
" std::cout << atan2(0.1,1) ;\n"
" std::cout << atan2(0.0001,100) ;\n"
" std::cout << atan2(0.0,1e-1) ;\n"
" std::cout << atan2(1.0E-1,-3) ;\n"
" std::cout << atan2(-1.0E-1,+2) ;\n"
" std::cout << atan2(+1.0E-1,0) ;\n"
" std::cout << atan2(0.1E-1,3) ;\n"
" std::cout << atan2(+0.1E-1,1) ;\n"
" std::cout << atan2(-0.1E-1,8) ;\n"
" std::cout << atan2f(1,1) ;\n"
" std::cout << atan2f(-1,-1) ;\n"
" std::cout << atan2f(0.1,1) ;\n"
" std::cout << atan2f(0.0001,100) ;\n"
" std::cout << atan2f(0.0,1e-1) ;\n"
" std::cout << atan2f(1.0E-1,-3) ;\n"
" std::cout << atan2f(-1.0E-1,+2) ;\n"
" std::cout << atan2f(+1.0E-1,0) ;\n"
" std::cout << atan2f(0.1E-1,3) ;\n"
" std::cout << atan2f(+0.1E-1,1) ;\n"
" std::cout << atan2f(-0.1E-1,8) ;\n"
" std::cout << atan2l(1,1) ;\n"
" std::cout << atan2l(-1,-1) ;\n"
" std::cout << atan2l(0.1,1) ;\n"
" std::cout << atan2l(0.0001,100) ;\n"
" std::cout << atan2l(0.0,1e-1) ;\n"
" std::cout << atan2l(1.0E-1,-3) ;\n"
" std::cout << atan2l(-1.0E-1,+2) ;\n"
" std::cout << atan2l(+1.0E-1,0) ;\n"
" std::cout << atan2l(0.1E-1,3) ;\n"
" std::cout << atan2l(+0.1E-1,1) ;\n"
" std::cout << atan2l(-0.1E-1,8) ;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void foo()\n"
"{\n"
" std::cout << atan2(0,0) << std::endl;\n"
" std::cout << atan2f(0,0) << std::endl;\n"
" std::cout << atan2l(0,0) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Passing values 0 and 0 to atan2() leads to implementation-defined result.\n"
"[test.cpp:4]: (warning) Passing values 0 and 0 to atan2f() leads to implementation-defined result.\n"
"[test.cpp:5]: (warning) Passing values 0 and 0 to atan2l() leads to implementation-defined result.\n", errout_str());
}
void mathfunctionCall_fmod() {
// fmod, fmodl, fmodf
check("void foo()\n"
"{\n"
" std::cout << fmod(1.0,0) << std::endl;\n"
" std::cout << fmodf(1.0,0) << std::endl;\n"
" std::cout << fmodl(1.0,0) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid fmod() argument nr 2. The value is 0 but the valid values are '!0.0'.\n"
"[test.cpp:4]: (error) Invalid fmodf() argument nr 2. The value is 0 but the valid values are '!0.0'.\n"
"[test.cpp:5]: (error) Invalid fmodl() argument nr 2. The value is 0 but the valid values are '!0.0'.\n"
"[test.cpp:3]: (warning) Passing values 1.0 and 0 to fmod() leads to implementation-defined result.\n"
"[test.cpp:4]: (warning) Passing values 1.0 and 0 to fmodf() leads to implementation-defined result.\n"
"[test.cpp:5]: (warning) Passing values 1.0 and 0 to fmodl() leads to implementation-defined result.\n", errout_str());
check("void foo()\n"
"{\n"
" std::cout << fmod(1.0,1) << std::endl;\n"
" std::cout << fmodf(1.0,1) << std::endl;\n"
" std::cout << fmodl(1.0,1) << std::endl;\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void mathfunctionCall_precision() {
check("void foo() {\n"
" print(exp(x) - 1);\n"
" print(log(1 + x));\n"
" print(1 - erf(x));\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression 'exp(x) - 1' can be replaced by 'expm1(x)' to avoid loss of precision.\n"
"[test.cpp:3]: (style) Expression 'log(1 + x)' can be replaced by 'log1p(x)' to avoid loss of precision.\n"
"[test.cpp:4]: (style) Expression '1 - erf(x)' can be replaced by 'erfc(x)' to avoid loss of precision.\n", errout_str());
check("void foo() {\n"
" print(exp(x) - 1.0);\n"
" print(log(1.0 + x));\n"
" print(1.0 - erf(x));\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression 'exp(x) - 1' can be replaced by 'expm1(x)' to avoid loss of precision.\n"
"[test.cpp:3]: (style) Expression 'log(1 + x)' can be replaced by 'log1p(x)' to avoid loss of precision.\n"
"[test.cpp:4]: (style) Expression '1 - erf(x)' can be replaced by 'erfc(x)' to avoid loss of precision.\n", errout_str());
check("void foo() {\n"
" print(exp(3 + x*f(a)) - 1);\n"
" print(log(x*4 + 1));\n"
" print(1 - erf(34*x + f(x) - c));\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Expression 'exp(x) - 1' can be replaced by 'expm1(x)' to avoid loss of precision.\n"
"[test.cpp:3]: (style) Expression 'log(1 + x)' can be replaced by 'log1p(x)' to avoid loss of precision.\n"
"[test.cpp:4]: (style) Expression '1 - erf(x)' can be replaced by 'erfc(x)' to avoid loss of precision.\n", errout_str());
check("void foo() {\n"
" print(2*exp(x) - 1);\n"
" print(1 - erf(x)/2.0);\n"
"}");
ASSERT_EQUALS("", errout_str());
}
void checkIgnoredReturnValue() {
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def version=\"2\">\n"
" <function name=\"mystrcmp,foo::mystrcmp\">\n"
" <use-retval/>\n"
" <arg nr=\"1\"/>\n"
" <arg nr=\"2\"/>\n"
" </function>\n"
"</def>";
const Settings settings2 = settingsBuilder().severity(Severity::warning).libraryxml(xmldata).build();
check("void foo() {\n"
" mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function mystrcmp() is not used.\n", errout_str());
check("void foo() {\n"
" foo::mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function foo::mystrcmp() is not used.\n", errout_str());
check("void f() {\n"
" foo x;\n"
" x.mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function x.mystrcmp() is not used.\n", errout_str());
check("bool mystrcmp(char* a, char* b);\n" // cppcheck sees a custom strcmp definition, but it returns a value. Assume it is the one specified in the library.
"void foo() {\n"
" mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function mystrcmp() is not used.\n", errout_str());
check("void mystrcmp(char* a, char* b);\n" // cppcheck sees a custom strcmp definition which returns void!
"void foo() {\n"
" mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
check("void foo() {\n"
" class mystrcmp { mystrcmp() {} };\n" // strcmp is a constructor definition here
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
check("void foo() {\n"
" return mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
check("void foo() {\n"
" return foo::mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
check("void foo() {\n"
" if(mystrcmp(a, b));\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
check("void foo() {\n"
" bool b = mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
// #6194
check("void foo() {\n"
" MyStrCmp mystrcmp(x, y);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
// #6197
check("void foo() {\n"
" abc::def.mystrcmp(a,b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
// #6233
check("int main() {\n"
" auto lambda = [](double value) {\n"
" double rounded = floor(value + 0.5);\n"
" printf(\"Rounded value = %f\\n\", rounded);\n"
" };\n"
" lambda(13.3);\n"
" return 0;\n"
"}");
ASSERT_EQUALS("", errout_str());
// #6669
check("void foo(size_t size) {\n"
" void * res{malloc(size)};\n"
"}");
ASSERT_EQUALS("", errout_str());
// #7447
check("void foo() {\n"
" int x{mystrcmp(a,b)};\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
// #7905
check("void foo() {\n"
" int x({mystrcmp(a,b)});\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("", errout_str());
check("void foo() {\n" // don't crash
" DEBUG(123)(mystrcmp(a,b))(fd);\n"
"}", dinit(CheckOptions, $.s = &settings2));
check("struct teststruct {\n"
" int testfunc1() __attribute__ ((warn_unused_result)) { return 1; }\n"
" [[nodiscard]] int testfunc2() { return 1; }\n"
" void foo() { testfunc1(); testfunc2(); }\n"
"};\n"
"int main() {\n"
" teststruct TestStruct1;\n"
" TestStruct1.testfunc1();\n"
" TestStruct1.testfunc2();\n"
" return 0;\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("[test.cpp:4]: (warning) Return value of function testfunc1() is not used.\n"
"[test.cpp:4]: (warning) Return value of function testfunc2() is not used.\n"
"[test.cpp:8]: (warning) Return value of function TestStruct1.testfunc1() is not used.\n"
"[test.cpp:9]: (warning) Return value of function TestStruct1.testfunc2() is not used.\n", errout_str());
// #9006
check("template <typename... a> uint8_t b(std::tuple<uint8_t> d) {\n"
" std::tuple<a...> c{std::move(d)};\n"
" return std::get<0>(c);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("struct A { int x; };\n"
"template <class... Ts>\n"
"A f(int x, Ts... xs) {\n"
" return {std::move(x), static_cast<int>(xs)...};\n"
"}\n"
"A g() { return f(1); }");
ASSERT_EQUALS("", errout_str());
// #8412 - unused operator result
check("void foo() {\n"
" !mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("[test.cpp:2]: (warning) Return value of function mystrcmp() is not used.\n", errout_str());
check("void f(std::vector<int*> v) {\n"
" delete *v.begin();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("int __attribute__((pure)) p_foo(int);\n" // #12697
"int __attribute__((const)) c_foo(int);\n"
"void f() {\n"
" p_foo(0);\n"
" c_foo(0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Return value of function p_foo() is not used.\n"
"[test.cpp:5]: (warning) Return value of function c_foo() is not used.\n",
errout_str());
}
void checkIgnoredErrorCode() {
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
"<def version=\"2\">\n"
" <function name=\"mystrcmp\">\n"
" <use-retval type=\"error-code\"/>\n"
" <arg nr=\"1\"/>\n"
" <arg nr=\"2\"/>\n"
" </function>\n"
"</def>";
const Settings settings2 = settingsBuilder().severity(Severity::style).libraryxml(xmldata).build();
check("void foo() {\n"
" mystrcmp(a, b);\n"
"}", dinit(CheckOptions, $.s = &settings2));
ASSERT_EQUALS("[test.cpp:2]: (style) Error code from the return value of function mystrcmp() is not used.\n", errout_str());
}
void memsetZeroBytes() {
check("void f() {\n"
" memset(p, 10, 0x0);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0 bytes.\n", errout_str());
check("void f() {\n"
" memset(p, sizeof(p), 0);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0 bytes.\n", errout_str());
check("void f() {\n"
" memset(p, sizeof(p), i);\n"
"}");
ASSERT_EQUALS("", errout_str());
// #6269 false positives in case of overloaded standard library functions
check("class c {\n"
" void memset( int i );\n"
" void f( void ) {\n"
" memset( 0 );\n"
" }\n"
"};");
ASSERT_EQUALS("", errout_str());
// #7285
check("void f() {\n"
" memset(&tm, sizeof(tm), 0);\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (warning) memset() called to fill 0 bytes.\n", errout_str());
}
void memsetInvalid2ndParam() {
check("void f() {\n"
" int* is = new int[10];\n"
" memset(is, 1.0f, 40);\n"
" int* is2 = new int[10];\n"
" memset(is2, 0.1f, 40);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) The 2nd memset() argument '1.0f' is a float, its representation is implementation defined.\n"
"[test.cpp:5]: (portability) The 2nd memset() argument '0.1f' is a float, its representation is implementation defined.\n", errout_str());
check("void f() {\n"
" int* is = new int[10];\n"
" float g = computeG();\n"
" memset(is, g, 40);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (portability) The 2nd memset() argument 'g' is a float, its representation is implementation defined.\n", errout_str());
check("void f() {\n"
" int* is = new int[10];\n"
" memset(is, 0.0f, 40);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f() {\n" // FP
" float x = 2.3f;\n"
" memset(a, (x?64:0), 40);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" short ss[] = {1, 2};\n"
" memset(ss, 256, 4);\n"
" short ss2[2];\n"
" memset(ss2, -129, 4);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) The 2nd memset() argument '256' doesn't fit into an 'unsigned char'.\n"
"[test.cpp:5]: (warning) The 2nd memset() argument '-129' doesn't fit into an 'unsigned char'.\n", errout_str());
check("void f() {\n"
" int is[10];\n"
" memset(is, 0xEE, 40);\n"
" unsigned char* cs = malloc(256);\n"
" memset(cs, -1, 256);\n"
" short* ss[30];\n"
" memset(ss, -128, 60);\n"
" char cs2[30];\n"
" memset(cs2, 255, 30);\n"
" char cs3[30];\n"
" memset(cs3, 0, 30);\n"
"}");
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" int is[10];\n"
" const int i = g();\n"
" memset(is, 1.0f + i, 40);\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (portability) The 2nd memset() argument '1.0f+i' is a float, its representation is implementation defined.\n", errout_str());
}
void checkMissingReturn1() {
check("int f() {}");
ASSERT_EQUALS("[test.cpp:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
{
const char code[] = "int main(void) {}";
{
const Settings s = settingsBuilder().c(Standards::C89).build();
check(code, dinit(CheckOptions, $.cpp = false, $.s = &s)); // c code (c89)
ASSERT_EQUALS("[test.c:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
}
{
const Settings s = settingsBuilder().c(Standards::C99).build();
check(code, dinit(CheckOptions, $.cpp = false, $.s = &s)); // c code (c99)
ASSERT_EQUALS("", errout_str());
check(code, dinit(CheckOptions, $.s = &s)); // c++ code
ASSERT_EQUALS("", errout_str());
}
}
check("F(A,B) { x=1; }");
ASSERT_EQUALS("", errout_str());
check("auto foo4() -> void {}");
ASSERT_EQUALS("", errout_str());
check("void STDCALL foo() {}");
ASSERT_EQUALS("", errout_str());
check("void operator=(int y) { x=y; }");
ASSERT_EQUALS("", errout_str());
check("int f() {\n"
"back:\n"
" return 0;\n"
"ng:\n"
" x=y;\n"
" goto back;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
// unreachable code..
check("int foo(int x) {\n"
" return 1;\n"
" (void)x;\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int foo(int x) {\n"
" if (x) goto out;\n"
" return 1;\n"
"out:\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
// switch
check("int f() {\n"
" switch (x) {\n"
" case 1: break;\n" // <- error
" case 2: return 1;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
check("int f() {\n"
" switch (x) {\n"
" case 1: return 2; break;\n"
" default: return 1;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
check("bool test(unsigned char v1, int v2) {\n"
" switch (v1) {\n"
" case 0:\n"
" switch (v2) {\n"
" case 48000:\n"
" break;\n"
" }\n"
" return false;\n"
" default:\n"
" return true;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
// if/else
check("int f(int x) {\n"
" if (x) {\n"
" return 1;\n"
" }\n" // <- error (missing else)
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
check("int f(int x) {\n"
" if (x) {\n"
" ;\n" // <- error
" } else {\n"
" return 1;\n"
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
check("int f() {\n"
" if (!0) {\n"
" return 1;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
check("int f() {\n"
" if (!0) {}\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
// loop
check("int f(int x) {\n"
" while (1) {\n"
" dostuff();\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
// return {..}
check("std::pair<int, int> typeDecl(int tok) {\n"
" if (!tok)\n"
" return {};\n"
" else\n"
" return {1, 2};\n"
"}");
ASSERT_EQUALS("", errout_str());
// noreturn function
check("int f(int x) { exit(0); }");
ASSERT_EQUALS("", errout_str());
check("int f(int x) { assert(0); }");
ASSERT_EQUALS("", errout_str());
check("int f(int x) { if (x) return 1; else return bar({1}, {}); }");
ASSERT_EQUALS("", errout_str());
check("auto f() -> void {}"); // #10342
ASSERT_EQUALS("", errout_str());
check("struct S1 {\n" // #7433
" S1& operator=(const S1& r) { if (this != &r) { i = r.i; } }\n"
" int i;\n"
"};\n"
"struct S2 {\n"
" S2& operator=(const S2& s) { if (this != &s) { j = s.j; } return *this; }\n"
" int j;\n"
"};\n"
"struct S3 {\n"
" S3& operator=(const S3& t) { if (this != &t) { k = t.k; return *this; } }\n"
" int k;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:2]: (error) Found an exit path from function with non-void return type that has missing return statement\n"
"[test.cpp:10]: (error) Found an exit path from function with non-void return type that has missing return statement\n",
errout_str());
// #11171
check("std::enable_if_t<sizeof(uint64_t) == 8> f() {}");
ASSERT_EQUALS("", errout_str());
check("std::enable_if_t<sizeof(uint64_t) == 8, int> f() {}");
ASSERT_EQUALS(
"[test.cpp:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n",
errout_str());
check("template<class T> std::enable_if_t<std::is_same<T, int>{}, int> f(T) {}");
ASSERT_EQUALS(
"[test.cpp:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n",
errout_str());
check("template<class T> std::enable_if_t<std::is_same<T, int>{}> f(T) {}");
ASSERT_EQUALS("", errout_str());
check("typename std::enable_if<sizeof(uint64_t) == 8>::type f() {}");
ASSERT_EQUALS("", errout_str());
check("typename std::enable_if<sizeof(uint64_t) == 8, int>::type f() {}");
ASSERT_EQUALS(
"[test.cpp:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n",
errout_str());
check("template<class T> typename std::enable_if<std::is_same<T, int>{}, int>::type f(T) {}");
ASSERT_EQUALS(
"[test.cpp:1]: (error) Found an exit path from function with non-void return type that has missing return statement\n",
errout_str());
check("template<class T> typename std::enable_if<std::is_same<T, int>{}>::type f(T) {}");
ASSERT_EQUALS("", errout_str());
check("struct S {\n"
" [[noreturn]] void f();\n"
" int g() { this->f(); }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
check("struct S { [[noreturn]] void f(); };\n"
"int g(S& s) { s.f(); }\n");
ASSERT_EQUALS("", errout_str());
}
void checkMissingReturn2() { // #11798
check("int f(bool const a) {\n"
" switch (a) {\n"
" case true:\n"
" return 1;\n"
" case false:\n"
" return 2;\n"
" }\n"
"}\n"
"int main(int argc, char* argv[])\n"
"{\n"
" auto const b= f(true);\n"
" auto const c= f(false);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void checkMissingReturn3() {
check("enum Enum {\n"
" A,\n"
" B,\n"
" C,\n"
"};\n"
"int f(Enum e) {\n"
" switch (e) {\n"
" case A:\n"
" return 1;\n"
" case B:\n"
" return 2;\n"
" case C:\n"
" return 2;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void checkMissingReturn4() {
check("enum Enum {\n"
" A,\n"
" B,\n"
" C,\n"
"};\n"
"int f(Enum e) {\n"
" switch (e) {\n"
" case A:\n"
" return 1;\n"
" case B:\n"
" return 2;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
}
void checkMissingReturn5() {
check("enum Enum {\n"
" A,\n"
" B,\n"
" C,\n"
"};\n"
"int f(Enum e, bool b) {\n"
" switch (e) {\n"
" case A:\n"
" return 1;\n"
" case B:\n"
" return 2;\n"
" case C:\n"
" switch (b) {\n"
" case true:\n"
" return 3;\n"
" case false:\n"
" return 4;\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void checkMissingReturn6() {// #13180
check("int foo(void)\n"
"{\n"
" i = readData();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Found an exit path from function with non-void return type that has missing return statement\n", errout_str());
}
// NRVO check
void returnLocalStdMove1() {
check("struct A{}; A f() { A var; return std::move(var); }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Using std::move for returning object by-value from function will affect copy elision optimization."
" More: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-return-move-local\n", errout_str());
}
// RVO, C++03 ctor style
void returnLocalStdMove2() {
check("struct A{}; A f() { return std::move( A() ); }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Using std::move for returning object by-value from function will affect copy elision optimization."
" More: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-return-move-local\n", errout_str());
}
// RVO, new ctor style
void returnLocalStdMove3() {
check("struct A{}; A f() { return std::move(A{}); }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Using std::move for returning object by-value from function will affect copy elision optimization."
" More: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-return-move-local\n", errout_str());
}
// Function argument
void returnLocalStdMove4() {
check("struct A{}; A f(A a) { return std::move(A{}); }");
ASSERT_EQUALS("[test.cpp:1]: (performance) Using std::move for returning object by-value from function will affect copy elision optimization."
" More: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-return-move-local\n", errout_str());
}
void returnLocalStdMove5() {
check("struct A{} a; A f1() { return std::move(a); }\n"
"A f2() { volatile A var; return std::move(var); }");
ASSERT_EQUALS("", errout_str());
check("struct S { std::string msg{ \"abc\" }; };\n"
"std::unique_ptr<S> get(std::vector<std::unique_ptr<S>>& v) {\n"
" return std::move(v.front());\n"
"}\n"
"int main() {\n"
" std::vector<std::unique_ptr<S>> v;\n"
" v.emplace_back(std::make_unique<S>());\n"
" auto p = get(v);\n"
" std::cout << p->msg;\n"
"}\n");
ASSERT_EQUALS("", errout_str());
check("std::string&& f() {\n" // #11881
" std::string s;\n"
" return std::move(s);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
void negativeMemoryAllocationSizeError() { // #389
check("void f() {\n"
" int *a;\n"
" a = malloc( -10 );\n"
" free(a);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid malloc() argument nr 1. The value is -10 but the valid values are '0:'.\n", errout_str());
check("void f() {\n"
" int *a;\n"
" a = alloca( -10 );\n"
" free(a);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (warning) Obsolete function 'alloca' called.\n"
"[test.cpp:3]: (error) Invalid alloca() argument nr 1. The value is -10 but the valid values are '0:'.\n", errout_str());
}
void checkLibraryMatchFunctions() {
/*const*/ Settings s = settingsBuilder(settings).checkLibrary().debugwarnings().build();
s.daca = true;
check("void f() {\n"
" lib_func();"
"}", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("[test.cpp:2]: (information) --check-library: There is no matching configuration for function lib_func()\n", errout_str());
check("void f(void* v) {\n"
" lib_func(v);"
"}", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("[test.cpp:2]: (information) --check-library: There is no matching configuration for function lib_func()\n", errout_str());
// #10105
check("class TestFixture {\n"
"protected:\n"
" bool prepareTest(const char testname[]);\n"
"};\n"
"\n"
"class TestMemleak : private TestFixture {\n"
" void run() {\n"
" do { prepareTest(\"testFunctionReturnType\"); } while (false);\n"
" }\n"
"\n"
" void testFunctionReturnType() {\n"
" }\n"
"};", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
// #11183
check("#include <string>\n"
"\n"
"extern void cb(const std::string&);\n"
"\n"
"void f() {\n"
" cb(std::string(\"\"));\n"
"}", dinit(CheckOptions, $.s = &s));
TODO_ASSERT_EQUALS("", "[test.cpp:6]: (information) --check-library: There is no matching configuration for function cb()\n", errout_str());
// #7375
check("void f() {\n"
" struct S { int i; char c; };\n"
" size_t s = sizeof(S);\n"
" static_assert(s == 9);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f(char) {}\n"
"void g() {\n"
" f(int8_t(1));\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f(std::uint64_t& u) {\n"
" u = std::uint32_t(u) * std::uint64_t(100);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f() { throw(1); }\n", dinit(CheckOptions, $.s = &s)); // #8958
ASSERT_EQUALS("", errout_str());
check("using namespace std;\n"
"void f() { throw range_error(\"abc\"); }\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("class C {\n" // #9002
"public:\n"
" static int f() { return 1; }\n"
"};\n"
"void g() { C::f(); }\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f(const std::vector<std::string>& v) {\n" // #11223
" for (const auto& s : v)\n"
" s.find(\"\");\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("[test.cpp:3]: (warning) Return value of function s.find() is not used.\n", errout_str());
check("void f() {\n"
" auto* p = new std::vector<int>(5);\n"
" p->push_back(1);\n"
" auto* q = new std::vector<int>{ 5, 7 };\n"
" q->push_back(1);\n"
" auto* r = new std::vector<int>;\n"
" r->push_back(1);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" auto p = std::make_shared<std::vector<int>>();\n"
" p->push_back(1);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f(std::vector<std::vector<int>>& v) {\n"
" auto it = v.begin();\n"
" it->push_back(1);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" auto v = std::vector<int>{};\n"
" v.push_back(1);\n"
" auto w = std::vector<int>{ 1, 2, 3 };\n"
" w.push_back(1);\n"
" auto x = std::vector<int>(1);\n"
" x.push_back(1);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" auto p(std::make_shared<std::vector<int>>());\n"
" p->push_back(1);\n"
" auto q{ std::make_shared<std::vector<int>>{} };\n"
" q->push_back(1);\n"
"}\n", dinit(CheckOptions, $.s = &s));
TODO_ASSERT_EQUALS("",
"[test.cpp:2]: (debug) auto token with no type.\n"
"[test.cpp:4]: (debug) auto token with no type.\n"
"[test.cpp:3]: (information) --check-library: There is no matching configuration for function auto::push_back()\n"
"[test.cpp:5]: (information) --check-library: There is no matching configuration for function auto::push_back()\n",
errout_str());
check("struct F { void g(int); };\n"
"void f(std::list<F>& l) {\n"
" std::list<F>::iterator it;\n"
" for (it = l.begin(); it != l.end(); ++it)\n"
" it->g(0);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", filter_valueflow(errout_str()));
check("auto f() {\n"
" return std::runtime_error(\"abc\");\n"
"}\n", dinit(CheckOptions, $.s = &s));
TODO_ASSERT_EQUALS("",
"[test.cpp:1]: (debug) auto token with no type.\n",
errout_str());
check("struct S {\n" // #11543
" S() {}\n"
" std::vector<std::shared_ptr<S>> v;\n"
" void f(int i) const;\n"
"};\n"
"void S::f(int i) const {\n"
" for (const std::shared_ptr<S>& c : v)\n"
" c->f(i);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("namespace N {\n"
" struct S { static const std::set<std::string> s; };\n"
"}\n"
"void f() {\n"
" const auto& t = N::S::s;\n"
" if (t.find(\"abc\") != t.end()) {}\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", filter_valueflow(errout_str()));
check("void f(std::vector<std::unordered_map<int, std::unordered_set<int>>>& v, int i, int j) {\n"
" auto& s = v[i][j];\n"
" s.insert(0);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("int f(const std::vector<std::string>& v, int i, char c) {\n"
" const auto& s = v[i];\n"
" return s.find(c);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f() {\n" // #11604
" int (*g)() = nullptr;\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" INT (*g)() = nullptr;\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("struct T;\n"
"std::shared_ptr<T> get();\n"
"void f(int i) {\n"
" auto p = get();\n"
" p->h(i);\n"
" p.reset(nullptr);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("[test.cpp:5]: (information) --check-library: There is no matching configuration for function T::h()\n",
errout_str());
check("struct S : std::vector<int> {\n"
" void f(int i) { push_back(i); }\n"
"};\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" auto g = []() -> std::string { return \"abc\"; };\n"
" auto s = g();\n"
" if (s.at(0)) {}\n"
" auto h{ []() -> std::string { return \"xyz\"; } };\n"
" auto t = h();\n"
" if (t.at(0)) {}\n"
"};\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", filter_valueflow(errout_str()));
check("::std::string f(const char* c) {\n" // #12365
" return ::std::string(c);\n"
"}\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("template <typename T>\n"
"struct S : public std::vector<T> {\n"
" void resize(size_t n) { std::vector<T>::resize(n); }\n"
"};\n", dinit(CheckOptions, $.s = &s));
ASSERT_EQUALS("", errout_str());
check("struct P {\n" // #13105
" bool g(int i) const;\n"
" std::shared_ptr<std::map<int, int>> m;\n"
"};\n"
"bool P::g(int i) const {\n"
" auto it = m->find(i);\n"
" const bool b = it != m->end();\n"
" return b;\n"
"}\n", dinit(CheckOptions, $.s = &s));
TODO_ASSERT_EQUALS("", "[test.cpp:6]: (debug) auto token with no type.\n", errout_str());
}
void checkUseStandardLibrary1() {
check("void f(void* dest, void const* src, const size_t count) {\n"
" size_t i;\n"
" for (i = 0; count > i; ++i)\n"
" (reinterpret_cast<uint8_t*>(dest))[i] = (reinterpret_cast<const uint8_t*>(src))[i];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::memcpy instead of loop.\n", errout_str());
}
void checkUseStandardLibrary2() {
check("void f(void* dest, void const* src, const size_t count) {\n"
" for (size_t i = 0; i < count; i++) {\n"
" (reinterpret_cast<uint8_t*>(dest))[i] = (reinterpret_cast<const uint8_t*>(src))[i];\n"
"}}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::memcpy instead of loop.\n", errout_str());
}
void checkUseStandardLibrary3() {
check("void f(void* dst, const void* src, const size_t count) {\n"
" size_t i;\n"
" for (i = 0; count > i; i++)\n"
" ((char*)dst)[i] = ((const char*)src)[i];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::memcpy instead of loop.\n", errout_str());
}
void checkUseStandardLibrary4() {
check("void f(void* dst, void* src, const size_t size) {\n"
" for (size_t i = 0; i < size; i += 1) {\n"
" ((int8_t*)dst)[i] = ((int8_t*)src)[i];\n"
"}}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::memcpy instead of loop.\n", errout_str());
}
// different indexes
void checkUseStandardLibrary5() {
check("void f(void* dst, void* src, const size_t size, const size_t from_idx) {\n"
" for (size_t i = 0; i < size; ++i) {\n"
" ((int8_t*)dst)[i] = ((int8_t*)src)[from_idx];\n"
"}}\n");
ASSERT_EQUALS("", errout_str());
}
// unknown count
void checkUseStandardLibrary6() {
check("void f(void* dst, void* src, const size_t size) {\n"
" for (size_t i = 0; ((int8_t*)src)[i] != 0; ++i) {\n"
" ((int8_t*)dst)[i] = ((int8_t*)src)[i];\n"
"}}\n");
ASSERT_EQUALS("", errout_str());
}
// increment with 2
void checkUseStandardLibrary7() {
check("void f(void* dst, void* src, const size_t size) {\n"
" for (size_t i = 0; i < size; i += 2) {\n"
" ((int8_t*)dst)[i] = ((int8_t*)src)[i];\n"
"}}\n");
ASSERT_EQUALS("", errout_str());
}
// right argument of assignment could be static_cast, functional cast, c-style and implicit cast
// functional cast case not covered
void checkUseStandardLibrary8() {
check("void f(void* dest, const size_t count) {\n"
" size_t i;\n"
" for (i = 0; i < count; ++i)\n"
" (reinterpret_cast<int8_t*>(dest))[i] = static_cast<const int8_t>(0);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::memset instead of loop.\n", errout_str());
}
void checkUseStandardLibrary9() {
check("void f(void* dest, const size_t count) {\n"
" for (size_t i = 0; i < count; i++) {\n"
" (reinterpret_cast<uint8_t*>(dest))[i] = (static_cast<const uint8_t>(0));\n"
"}}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::memset instead of loop.\n", errout_str());
}
void checkUseStandardLibrary10() {
check("void f(void* dst, const size_t size) {\n"
" size_t i;\n"
" for (i = 0; i < size; i++)\n"
" ((char*)dst)[i] = (const char)0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Consider using std::memset instead of loop.\n", errout_str());
}
void checkUseStandardLibrary11() {
check("void f(void* dst, const size_t size) {\n"
" for (size_t i = 0; i < size; i += 1) {\n"
" ((int8_t*)dst)[i] = ((int8_t)0);\n"
"}}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::memset instead of loop.\n", errout_str());
}
void checkUseStandardLibrary12() {
check("void f(void* dst, const size_t size) {\n"
" for (size_t i = 0; i < size; i += 1) {\n"
" ((int8_t*)dst)[i] = 42;\n"
"}}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::memset instead of loop.\n", errout_str());
}
void checkUseStandardLibrary13() {
check("void f(void* dest, const size_t count) {\n"
" for (size_t i = 0; i < count; i++) {\n"
" reinterpret_cast<unsigned char*>(dest)[i] = '0';\n"
"}}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::memset instead of loop.\n", errout_str());
}
void checkUseStandardLibrary14() {
check("void f(void* dest) {\n"
" for (size_t i = 0; i < sizeof(int)*(15 + 42/2 - 7); i++) {\n"
" reinterpret_cast<unsigned char*>(dest)[i] = '0';\n"
"}}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Consider using std::memset instead of loop.\n", errout_str());
}
};
REGISTER_TEST(TestFunctions)
|