From dde6f5eaea5f1611238e9b68f6d9989fb48ddb42 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 20 Jun 2016 09:30:41 +0200 Subject: [PATCH] Do not warn about alloca() in C89 mode (#7558) --- lib/checkfunctions.cpp | 15 ++++++++------- test/testfunctions.cpp | 6 ++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 88d79b992..e7fac6dcc 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -48,13 +48,14 @@ void CheckFunctions::checkProhibitedFunctions() if (tok->isName() && tok->varId() == 0 && tok->strAt(1) == "(") { // alloca() is special as it depends on the code being C or C++, so it is not in Library if (checkAlloca && Token::simpleMatch(tok, "alloca (") && (!tok->function() || tok->function()->nestedIn->type == Scope::eGlobal)) { - if (_tokenizer->isC()) - reportError(tok, Severity::warning, "allocaCalled", - "Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n" - "The obsolete function 'alloca' is called. In C99 and later it is recommended to use a variable length array or " - "a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons " - "(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca)."); - else + if (_tokenizer->isC()) { + if (_settings->standards.c > Standards::C89) + reportError(tok, Severity::warning, "allocaCalled", + "Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n" + "The obsolete function 'alloca' is called. In C99 and later it is recommended to use a variable length array or " + "a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons " + "(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca)."); + } else reportError(tok, Severity::warning, "allocaCalled", "Obsolete function 'alloca' called.\n" "The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or " diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index fb2a547ff..653ce3290 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -246,6 +246,12 @@ private: "}", "test.cpp"); // #4382 - there are no VLAs in C++ ASSERT_EQUALS("", errout.str()); + check("void f()\n" + "{\n" + " char *x = alloca(10);\n" + "}", "test.c"); // #7558 - no alternative to alloca in C89 + ASSERT_EQUALS("", errout.str()); + check("void f()\n" "{\n" " char *x = alloca(10);\n"