diff --git a/gui/resultsview.cpp b/gui/resultsview.cpp index 698b6e82018..496c0793157 100644 --- a/gui/resultsview.cpp +++ b/gui/resultsview.cpp @@ -123,6 +123,7 @@ void ResultsView::clear(bool results) } mUI->mDetails->setText(QString()); + mUI->mCode->clear(); mStatistics->clear(); delete mCheckSettings; diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 8712800a867..37360519474 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -3099,8 +3099,7 @@ static const Token* findExpressionChangedImpl(const Token* expr, } bool global = false; if (tok->variable()) { - global = !tok->variable()->isLocal() && !tok->variable()->isArgument() && - !(tok->variable()->isMember() && !tok->variable()->isStatic()); + global = !tok->variable()->isLocal() && !tok->variable()->isArgument(); } else if (tok->isIncompleteVar() && !tok->isIncompleteConstant()) { global = true; } diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index ecaed652c5e..3d1a0ecccc5 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1422,6 +1422,8 @@ int CheckUninitVar::isFunctionParUsage(const Token *vartok, const Library& libra const bool isnullbad = library.isnullargbad(start->previous(), argumentNumber + 1); if (indirect == 0 && pointer && !address && isnullbad && alloc == NO_ALLOC) return 1; + if (vartok->varId() == 0 && vartok->valueType()) + indirect = vartok->valueType()->pointer; bool hasIndirect = false; const bool isuninitbad = library.isuninitargbad(start->previous(), argumentNumber + 1, indirect, &hasIndirect); if (alloc != NO_ALLOC) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 04532e1ecb9..6dd98de078e 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -565,7 +565,7 @@ static const Token* doAssignment(Variables &variables, const Token *tok, bool de variables.readAll(varid2, tok); } } - } else if (var1->mType == Variables::reference) { + } else if (var1->mType == Variables::reference || var1->mType == Variables::referenceArray) { variables.alias(varid1, varid2, true); } else if (var1->mType == Variables::standard && addressOf) { variables.alias(varid1, varid2, true); @@ -738,6 +738,8 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const if (type == Variables::none || isPartOfClassStructUnion(i->typeStartToken())) continue; const Token* defValTok = i->nameToken()->next(); + if (Token::Match(i->nameToken()->previous(), "& %var% )")) + defValTok = defValTok->next(); while (defValTok && defValTok->str() == "[") defValTok = defValTok->link()->next(); if (Token::simpleMatch(defValTok, ") (")) diff --git a/lib/reverseanalyzer.cpp b/lib/reverseanalyzer.cpp index 50aad01fdd9..4afd2556a24 100644 --- a/lib/reverseanalyzer.cpp +++ b/lib/reverseanalyzer.cpp @@ -110,8 +110,11 @@ namespace { { if (Token::simpleMatch(tok->tokAt(-2), "} else {")) tok = tok->linkAt(-2); - if (Token::simpleMatch(tok->previous(), ") {")) + if (Token::simpleMatch(tok->previous(), ") {")) { + if (Token::simpleMatch(tok->linkAt(-1)->astOperand2(), ";")) + return tok->linkAt(-1)->astOperand2(); return tok->linkAt(-1); + } if (Token::simpleMatch(tok->previous(), "do {")) return tok->previous(); return tok; @@ -232,7 +235,7 @@ namespace { if (!Token::Match(assignTop->astOperand1(), "%assign%")) { continueB &= updateRecursive(assignTop->astOperand1()); } - if (!assignTop->astParent()) + if (!assignTop->astParent() || Token::simpleMatch(assignTop->astParent(), ";")) break; assignTop = assignTop->astParent(); } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 34cb997a636..e173aabc30d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1115,14 +1115,18 @@ void Tokenizer::simplifyTypedef() syntaxError(t.second.getTypedefToken()); } else { const Token* const typedefToken = t.second.getTypedefToken(); + const Token* const nameToken = t.second.nameToken(); TypedefInfo typedefInfo; typedefInfo.name = t.second.name(); - typedefInfo.filename = list.file(typedefToken); - typedefInfo.lineNumber = typedefToken->linenr(); - typedefInfo.column = typedefToken->column(); - if (Token::Match(typedefToken->next(), "struct|enum|class|union %name% {") && typedefToken->strAt(2) == typedefInfo.name) { - typedefInfo.tagLine = typedefToken->tokAt(2)->linenr(); - typedefInfo.tagColumn = typedefToken->tokAt(2)->column(); + typedefInfo.filename = list.file(nameToken); + typedefInfo.lineNumber = nameToken->linenr(); + typedefInfo.column = nameToken->column(); + if (Token::Match(typedefToken->next(), "struct|enum|class|union %name% {")) { + typedefInfo.originalName = typedefToken->strAt(2); + if (typedefToken->strAt(2) == typedefInfo.name) { + typedefInfo.tagLine = typedefToken->tokAt(2)->linenr(); + typedefInfo.tagColumn = typedefToken->tokAt(2)->column(); + } } typedefInfo.used = t.second.isUsed(); typedefInfo.isFunctionPointer = isFunctionPointer(t.second.nameToken()); @@ -4496,6 +4500,10 @@ static void setVarIdStructMembers(Token *&tok1, tok->varId(it->second); } } + if (Token::Match(tok, "%name% = { . %name% =|{")) { + setVarIdStructMembers(tok, structMembers, varId); + tok = tok->linkAt(2); + } tok = tok->next(); } @@ -6376,6 +6384,12 @@ std::string Tokenizer::dumpTypedefInfo() const outs += typedefInfo.name; outs += "\""; + if (!typedefInfo.originalName.empty()) { + outs += " originalName=\""; + outs += typedefInfo.originalName; + outs += "\""; + } + outs += " file=\""; outs += ErrorLogger::toxml(typedefInfo.filename); outs += "\""; @@ -9000,7 +9014,7 @@ void Tokenizer::findGarbageCode() const syntaxError(tok); if (Token::Match(tok, "[;([{] %comp%|%oror%|%or%|%|/")) syntaxError(tok); - if (Token::Match(tok, "%cop%|= ]") && !Token::simpleMatch(tok, "*") && !(cpp && Token::Match(tok->previous(), "%type%|[|,|%num% &|=|> ]"))) + if (Token::Match(tok, "%cop%|= ]") && !Token::simpleMatch(tok, "*") && !(cpp && Token::Match(tok->previous(), "<|%type%|[|,|%num% &|=|> ]"))) syntaxError(tok); if (Token::Match(tok, "[+-] [;,)]}]") && !(cpp && Token::simpleMatch(tok->previous(), "operator"))) syntaxError(tok); diff --git a/lib/tokenize.h b/lib/tokenize.h index 9ee4b29466f..16cd88fa30e 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -698,6 +698,7 @@ class CPPCHECKLIB Tokenizer { }; struct TypedefInfo { std::string name; + std::string originalName; std::string filename; int lineNumber; int column; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 74763fde027..0f1c8a20ce7 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -430,12 +430,7 @@ static void valueFlowNumber(TokenList &tokenlist, const Settings& settings) if (tokenlist.isCPP() || settings.standards.c >= Standards::C23) { for (Token *tok = tokenlist.front(); tok; tok = tok->next()) { - if (tok->isName() && !tok->varId() && Token::Match(tok, "%bool%")) { - ValueFlow::Value value(tok->str() == "true"); - if (!tok->isTemplateArg()) - value.setKnown(); - setTokenValue(tok, std::move(value), settings); - } else if (Token::Match(tok, "[(,] NULL [,)]")) { + if (Token::Match(tok, "[(,] NULL [,)]")) { // NULL function parameters are not simplified in the // normal tokenlist ValueFlow::Value value(0); diff --git a/lib/vf_common.cpp b/lib/vf_common.cpp index ab01acc0f16..d6c6088b34b 100644 --- a/lib/vf_common.cpp +++ b/lib/vf_common.cpp @@ -154,6 +154,11 @@ namespace ValueFlow if (!tok->isTemplateArg()) value.setKnown(); setTokenValue(tok, std::move(value), settings); + } else if ((tok->isCpp() || settings.standards.c >= Standards::C23) && (tok->isName() && tok->varId() == 0 && Token::Match(tok, "%bool%"))) { + Value value(tok->str() == "true"); + if (!tok->isTemplateArg()) + value.setKnown(); + setTokenValue(tok, std::move(value), settings); } else if (Token::simpleMatch(tok, "sizeof (")) { if (tok->next()->astOperand2() && !tok->next()->astOperand2()->isLiteral() && tok->next()->astOperand2()->valueType() && (tok->next()->astOperand2()->valueType()->pointer == 0 || // <- TODO this is a bailout, abort when there are array->pointer conversions diff --git a/test/cli/other_test.py b/test/cli/other_test.py index e28a7305ce9..d10cb541e5b 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -420,7 +420,7 @@ def test_addon_misra(tmpdir): assert lines == [ 'Checking {} ...'.format(test_file) ] - assert stderr == '{}:2:1: style: misra violation (use --rule-texts= to get proper output) [misra-c2012-2.3]\ntypedef int MISRA_5_6_VIOLATION;\n^\n'.format(test_file) + assert stderr == '{}:2:13: style: misra violation (use --rule-texts= to get proper output) [misra-c2012-2.3]\ntypedef int MISRA_5_6_VIOLATION;\n ^\n'.format(test_file) def test_addon_y2038(tmpdir): @@ -2674,7 +2674,7 @@ def __test_addon_suppr(tmp_path, extra_args): assert exitcode == 0, stdout assert stdout == '' assert stderr.splitlines() == [ - '{}:4:1: style: misra violation (use --rule-texts= to get proper output) [misra-c2012-2.3]'.format(test_file), + '{}:4:13: style: misra violation (use --rule-texts= to get proper output) [misra-c2012-2.3]'.format(test_file), ] diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 990f105d941..afbcf14ca6d 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -5112,6 +5112,37 @@ class TestCondition : public TestFixture { " if (i < 0) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("struct A { int x; int y; };" + "void use(int);\n" + "void test(A a) {\n" + " int i = a.x;\n" + " int j = a.x;\n" + " use(j);\n" + " if (i == j) {}\n" + " if (i == a.x) {}\n" + " if (j == a.x) {}\n" + "}"); + ASSERT_EQUALS("[test.cpp:6:11]: (style) Condition 'i==j' is always true [knownConditionTrueFalse]\n" + "[test.cpp:7:11]: (style) Condition 'i==a.x' is always true [knownConditionTrueFalse]\n" + "[test.cpp:8:11]: (style) Condition 'j==a.x' is always true [knownConditionTrueFalse]\n", + errout_str()); + + check("struct S { int i; };\n" // #12795 + "struct T {\n" + " std::map m;\n" + " S* get(const std::string& s) { return m[s]; }\n" + " void modify() { for (const auto& e : m) e.second->i = 0; }\n" + "};\n" + "void f(T& t) {\n" + " const S* p = t.get(\"abc\");\n" + " const int o = p->i;\n" + " t.modify();\n" + " if (p->i == o) {}\n" + "}\n"); + TODO_ASSERT_EQUALS("", + "[test.cpp:11:14]: (style) Condition 'p->i==o' is always true [knownConditionTrueFalse]\n", + errout_str()); } void alwaysTrueInfer() { diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 5f6b67867f7..68715b78ae8 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -320,7 +320,6 @@ class TestGarbage : public TestFixture { void final_class_x() { - const char code[] = "class __declspec(dllexport) x final { };"; SimpleTokenizer tokenizer(settings, *this); ASSERT(tokenizer.tokenize(code)); @@ -1897,6 +1896,12 @@ class TestGarbage : public TestFixture { // #13892 ASSERT_NO_THROW(checkCode("void foovm(int x[const *]);")); + + // #14676 + ASSERT_NO_THROW(checkCode("int main() {\n" + " auto value = m[1 + qRow<>];\n" + "}\n")); + ignore_errout(); } }; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index a9ce5ff6f38..83a19c40805 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -3710,6 +3710,19 @@ class TestNullPointer : public TestFixture { " i++;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("void f(const int* p) {\n" // #6710 + " for (int i = *p; i < 5; ++i) {}\n" + " if (p) {}\n" + "}\n" + "struct S { int a; };\n" + "void g(const S* s) {\n" + " for (int i = s->a; i < 5; ++i) {}\n" + " if (s) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3:9] -> [test.cpp:2:19]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. [nullPointerRedundantCheck]\n" + "[test.cpp:8:9] -> [test.cpp:7:18]: (warning) Either the condition 's' is redundant or there is possible null pointer dereference: s. [nullPointerRedundantCheck]\n", + errout_str()); } void nullpointerDeadCode() { diff --git a/test/testother.cpp b/test/testother.cpp index cf83ee08f38..54676cc6814 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -200,6 +200,7 @@ class TestOther : public TestFixture { TEST_CASE(duplicateExpression18); TEST_CASE(duplicateExpression19); TEST_CASE(duplicateExpression20); + TEST_CASE(duplicateExpression21); TEST_CASE(duplicateExpressionLoop); TEST_CASE(duplicateValueTernary); TEST_CASE(duplicateValueTernarySizeof); // #13773 @@ -8186,6 +8187,35 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void duplicateExpression21() { + check("struct S { int i; };\n" // #12795 + "struct T {\n" + " std::map m;\n" + " S* get(const std::string& s) { return m[s]; }\n" + " void modify() { for (const auto& e : m) e.second->i = 0; }\n" + "};\n" + "void f(T& t) {\n" + " const S* p = t.get(\"abc\");\n" + " const int o = p->i;\n" + " t.modify();\n" + " if (p->i == o) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + + check("struct S { int i; };\n" + " struct T {\n" + " std::vector m;\n" + " void modify() { for (auto e : m) e->i = 0; }\n" + "};\n" + "void f(T& t) {\n" + " const S* p = t.m[0];\n" + " const int o = p->i;\n" + " t.modify();\n" + " if (p->i == o) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + } + void duplicateExpressionLoop() { check("void f() {\n" " int a = 1;\n" @@ -8897,8 +8927,7 @@ class TestOther : public TestFixture { " if (i == j) {}\n" "}"); ASSERT_EQUALS( - "[test.cpp:4:9] -> [test.cpp:3:9]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'. [duplicateAssignExpression]\n" - "[test.cpp:3:14] -> [test.cpp:4:14] -> [test.cpp:6:11]: (style) The comparison 'i == j' is always true because 'i' and 'j' represent the same value. [knownConditionTrueFalse]\n", + "[test.cpp:4:9] -> [test.cpp:3:9]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'. [duplicateAssignExpression]\n", errout_str()); check("struct A { int x; int y; };" @@ -8910,8 +8939,7 @@ class TestOther : public TestFixture { " if (i == a.x) {}\n" "}"); ASSERT_EQUALS( - "[test.cpp:4:9] -> [test.cpp:3:9]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'. [duplicateAssignExpression]\n" - "[test.cpp:3:14] -> [test.cpp:6:11]: (style) The comparison 'i == a.x' is always true because 'i' and 'a.x' represent the same value. [knownConditionTrueFalse]\n", + "[test.cpp:4:9] -> [test.cpp:3:9]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'. [duplicateAssignExpression]\n", errout_str()); check("struct A { int x; int y; };" @@ -8923,8 +8951,7 @@ class TestOther : public TestFixture { " if (j == a.x) {}\n" "}"); ASSERT_EQUALS( - "[test.cpp:4:9] -> [test.cpp:3:9]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'. [duplicateAssignExpression]\n" - "[test.cpp:4:14] -> [test.cpp:6:11]: (style) The comparison 'j == a.x' is always true because 'j' and 'a.x' represent the same value. [knownConditionTrueFalse]\n", + "[test.cpp:4:9] -> [test.cpp:3:9]: (style, inconclusive) Same expression used in consecutive assignments of 'i' and 'j'. [duplicateAssignExpression]\n", errout_str()); // Issue #8612 @@ -9992,9 +10019,7 @@ class TestOther : public TestFixture { " u.g();\n" " if (c == m->get()) {}\n" "}\n"); - TODO_ASSERT_EQUALS("", - "[test.cpp:16:33] -> [test.cpp:18:11]: (style) The comparison 'c == m->get()' is always true because 'c' and 'm->get()' represent the same value. [knownConditionTrueFalse]\n", - errout_str()); + ASSERT_EQUALS("", errout_str()); check("struct S {\n" // #12925 " const std::string & f() const { return str; }\n" diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 092c9e9df75..342cda59dc8 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -4615,7 +4615,7 @@ class TestSimplifyTypedef : public TestFixture { void typedefInfo1() { const std::string xml = dumpTypedefInfo("typedef int A;\nA x;"); ASSERT_EQUALS(" \n" - " \n" + " \n" " \n", xml); } @@ -4627,7 +4627,7 @@ class TestSimplifyTypedef : public TestFixture { " typedef fp16 ( *pfp16 ) ( void );\n" "}\n"); ASSERT_EQUALS(" \n" - " \n" + " \n" " \n" " \n" " \n" @@ -4639,7 +4639,7 @@ class TestSimplifyTypedef : public TestFixture { " \n" " \n" " \n" - " \n" + " \n" " \n" " \n" " \n" @@ -4677,7 +4677,7 @@ class TestSimplifyTypedef : public TestFixture { "} coord;\n" "coord c;"); ASSERT_EQUALS(" \n" - " \n" + " \n" " \n", xml); } }; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index d04f3c62e8e..960fee4ee87 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2173,6 +2173,14 @@ class TestUninitVar : public TestFixture { " return p;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + checkUninitVar("struct S { char c[10]; };\n" // #11290 + "S* f() {\n" + " S* s = (S*)malloc(sizeof(S));\n" + " sprintf(s->c, \"abc\");\n" + " return s;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } // class / struct.. diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 9a116bdaa2b..13d288a3fdd 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -198,6 +198,7 @@ class TestUnusedVar : public TestFixture { TEST_CASE(localvarreturn); // ticket #9167 TEST_CASE(localvarmaybeunused); TEST_CASE(localvarrvalue); // ticket #13977 + TEST_CASE(localvarreferencearray); // ticket #14637 TEST_CASE(localvarthrow); // ticket #3687 @@ -249,6 +250,7 @@ class TestUnusedVar : public TestFixture { TEST_CASE(localvarDelete); TEST_CASE(localvarLambda); // #8941, #8948 TEST_CASE(localvarStructuredBinding); // #10368 + TEST_CASE(localvarPtrToPtr); TEST_CASE(localvarCppInitialization); TEST_CASE(localvarCpp11Initialization); @@ -6557,6 +6559,16 @@ class TestUnusedVar : public TestFixture { ASSERT_EQUALS("[test.cpp:3:21]: (style) Variable 'm' is assigned a value that is never used. [unreadVariable]\n", errout_str()); } + void localvarreferencearray() { // ticket #14637 + functionVariableUsage("int f() {\n" + " int a[1];\n" + " int(&r)[1] = a;\n" + " r[0] = 0;\n" + " return r[0];\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + } + void localvarthrow() { // ticket #3687 functionVariableUsage("void foo() {\n" " try {}" @@ -6958,6 +6970,16 @@ class TestUnusedVar : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void localvarPtrToPtr() { + functionVariableUsage("int main() {\n" + " int *p, **pp = &p;\n" + " int i = 123;\n" + " *pp = &i;\n" + " return *p;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + } + void localvarCppInitialization() { functionVariableUsage("void foo() {\n" " int buf[6];\n" diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index b8b31e8d248..d8a5b533bf3 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1207,6 +1207,16 @@ class TestValueFlow : public TestFixture { ASSERT_EQUALS(1U, values.size()); ASSERT_EQUALS(123, values.empty() ? 0 : values.front().intvalue); + code = "x = true ? 2 : 3;\n"; // #14369 + values = tokenValues(code, "?"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(2, values.empty() ? 0 : values.front().intvalue); + + code = "x = false ? 2 : 3;\n"; // #14369 + values = tokenValues(code, "?"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(3, values.empty() ? 0 : values.front().intvalue); + code = "int f() {\n" " const int i = 1;\n" " int x = i < 0 ? 0 : 1;\n" diff --git a/test/testvarid.cpp b/test/testvarid.cpp index 007e16ffb29..64b7b6664be 100644 --- a/test/testvarid.cpp +++ b/test/testvarid.cpp @@ -4500,7 +4500,7 @@ class TestVarID : public TestFixture { "2: struct T { struct S s ; } ;\n" "3: struct U { struct T t ; } ;\n" "4: void f ( ) {\n" - "5: struct U u@4 ; u@4 = { .@UNIQUE t@5 = { . s = { . s = 1 } } } ;\n" + "5: struct U u@4 ; u@4 = { .@UNIQUE t@5 = { .@UNIQUE s@6 = { .@UNIQUE s@7 = 1 } } } ;\n" "6: }\n"; ASSERT_EQUALS(exp, tokenizeExpr(code)); } diff --git a/tools/test-my-pr.py b/tools/test-my-pr.py index 4a40f544ee1..e0ed773afef 100755 --- a/tools/test-my-pr.py +++ b/tools/test-my-pr.py @@ -8,9 +8,13 @@ import donate_cpu_lib as lib import argparse import glob +import gzip +import natsort import os import sys import random +import re +import requests import subprocess @@ -20,6 +24,100 @@ def format_float(a, b=1): return 'N/A' +def ftp_get(url): + try: + response = requests.get(url, timeout=300) + response.raise_for_status() + return response.content + except requests.RequestException as err: + print('Failed to fetch {}: {}'.format(url, err)) + return None + + +def latestvername(names): + s = natsort.natsorted(names, key=lambda x: x[x.index('_')+1:x.index('.orig.tar')]) + return s[-1] + + +def getpackages(): + debian = 'https://ftp.debian.org/debian/' + + data = ftp_get(debian + 'ls-lR.gz') + if data is None: + print('Failed to fetch ls-lR.gz') + sys.exit(1) + + lines = gzip.decompress(data).decode('utf-8', errors='replace').splitlines() + + # Example content in ls-lR: + #./pool/main/0/0xffff: + #total 1452 + #-rw-r--r-- 2 dak debadmin 6524 Dec 25 2016 0xffff_0.7-2.debian.tar.xz + #-rw-r--r-- 2 dak debadmin 1791 Dec 25 2016 0xffff_0.7-2.dsc + #-rw-r--r-- 2 dak debadmin 57168 Dec 25 2016 0xffff_0.7-2_amd64.deb + #-rw-r--r-- 2 dak debadmin 48578 Dec 26 2016 0xffff_0.7-2_arm64.deb + #-rw-r--r-- 2 dak debadmin 56730 Dec 26 2016 0xffff_0.7-2_armel.deb + #-rw-r--r-- 2 dak debadmin 57296 Dec 26 2016 0xffff_0.7-2_armhf.deb + #-rw-r--r-- 2 dak debadmin 60254 Dec 26 2016 0xffff_0.7-2_i386.deb + #-rw-r--r-- 2 dak debadmin 53130 Dec 26 2016 0xffff_0.7-2_mips.deb + #-rw-r--r-- 2 dak debadmin 52542 Dec 26 2016 0xffff_0.7-2_mips64el.deb + #-rw-r--r-- 2 dak debadmin 53712 Dec 26 2016 0xffff_0.7-2_mipsel.deb + #-rw-r--r-- 2 dak debadmin 51908 Dec 26 2016 0xffff_0.7-2_ppc64el.deb + #-rw-r--r-- 2 dak debadmin 53548 Dec 26 2016 0xffff_0.7-2_s390x.deb + #-rw-r--r-- 2 dak debadmin 65248 Dec 25 2016 0xffff_0.7.orig.tar.gz + #-rw-r--r-- 2 dak debadmin 6884 Jul 19 19:08 0xffff_0.8-1.debian.tar.xz + #-rw-r--r-- 2 dak debadmin 1807 Jul 19 19:08 0xffff_0.8-1.dsc + #-rw-r--r-- 2 dak debadmin 58908 Jul 19 19:08 0xffff_0.8-1_amd64.deb + #-rw-r--r-- 2 dak debadmin 51340 Jul 19 19:58 0xffff_0.8-1_arm64.deb + #-rw-r--r-- 2 dak debadmin 57612 Jul 19 20:13 0xffff_0.8-1_armel.deb + #-rw-r--r-- 2 dak debadmin 58584 Jul 19 19:58 0xffff_0.8-1_armhf.deb + #-rw-r--r-- 2 dak debadmin 57544 Jul 19 20:23 0xffff_0.8-1_hurd-i386.deb + #-rw-r--r-- 2 dak debadmin 62048 Jul 19 23:54 0xffff_0.8-1_i386.deb + #-rw-r--r-- 2 dak debadmin 55080 Jul 23 19:07 0xffff_0.8-1_kfreebsd-amd64.deb + #-rw-r--r-- 2 dak debadmin 58392 Jul 23 19:07 0xffff_0.8-1_kfreebsd-i386.deb + #-rw-r--r-- 2 dak debadmin 54144 Jul 19 22:28 0xffff_0.8-1_mips.deb + #-rw-r--r-- 2 dak debadmin 53648 Jul 20 00:56 0xffff_0.8-1_mips64el.deb + #-rw-r--r-- 2 dak debadmin 54740 Jul 19 22:58 0xffff_0.8-1_mipsel.deb + #-rw-r--r-- 2 dak debadmin 57424 Jul 19 19:58 0xffff_0.8-1_ppc64el.deb + #-rw-r--r-- 2 dak debadmin 53764 Jul 19 22:28 0xffff_0.8-1_s390x.deb + #-rw-r--r-- 2 dak debadmin 64504 Jul 19 19:08 0xffff_0.8.orig.tar.gz + # + + path = None + previous_path = '' + archives = [] + filename = None + filenames = [] + for line in lines: + line = line.strip() + if len(line) < 4: + if filename: + res1 = re.match(r'(.*)-[0-9.]+$', path) + if res1 is None: + res1 = re.match(r'(.*)[-.][0-9.]+$', path) + res2 = re.match(r'(.*)-[0-9.]+$', previous_path) + if res2 is None: + res2 = re.match(r'(.*)[-.][0-9.]+$', previous_path) + if res1 is None or res2 is None or res1.group(1) != res2.group(1): + archives.append(path + '/' + latestvername(filenames)) + else: + archives[-1] = path + '/' + latestvername(filenames) + if path: + previous_path = path + path = None + filename = None + filenames = [] + elif line.startswith('./pool/main/'): + path = debian + line[2:-1] + elif path and line.endswith(('.orig.tar.gz', '.orig.tar.bz2', '.orig.tar.xz')): + filename = line[1 + line.rfind(' '):] + filenames.append(filename) + + return archives + + + + if __name__ == "__main__": __my_script_name = os.path.splitext(os.path.basename(sys.argv[0]))[0] __work_path = os.path.expanduser(os.path.join('~', 'cppcheck-' + __my_script_name + '-workfolder')) @@ -40,6 +138,16 @@ def format_float(a, b=1): print(args) + if args.packages_path: + # You can download packages using daca2-download.py + args.packages = glob.glob(os.path.join(args.packages_path, '*.tar.xz')) + random.shuffle(args.packages) + elif args.packages is None: + args.packages = getpackages() + random.shuffle(args.packages) + + print('\n'.join(args.packages[:20])) + if not lib.check_requirements(): print("Error: Check requirements") sys.exit(1) @@ -100,35 +208,14 @@ def format_float(a, b=1): print('Failed to compile your version of Cppcheck') sys.exit(1) - if args.packages_path: - # You can download packages using daca2-download.py - args.packages = glob.glob(os.path.join(args.packages_path, '*.tar.xz')) - args.p = len(args.packages) - packages_idxs = list(range(args.p)) - random.shuffle(packages_idxs) - elif args.packages: - args.p = len(args.packages) - packages_idxs = [] - else: - packages_count = lib.get_packages_count() - if not packages_count: - print("network or server might be temporarily down..") - sys.exit(1) - - packages_idxs = list(range(packages_count)) - random.shuffle(packages_idxs) - packages_processed = 0 crashes = [] timeouts = [] - while (packages_processed < args.p and len(packages_idxs) > 0) or args.packages: - if args.packages: - package = args.packages.pop() - else: - package = lib.get_package(packages_idxs.pop()) + while packages_processed < args.p and args.packages: + package = args.packages.pop() - if package.startswith('ftp://') or package.startswith('http://'): + if package.startswith('ftp://') or package.startswith('https://'): tgz = lib.download_package(work_path, package, None) if tgz is None: print("No package downloaded")