From 40077fe30919494f0ecd04c4620cac2334d3d382 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Sat, 30 Apr 2022 09:10:48 +0200 Subject: new loplugin:stringviewdangle to find places where string_view is pointing into a temporary String Change-Id: Ib530b36f441e95d83d8f687d40a97516a0806721 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133656 Tested-by: Jenkins Reviewed-by: Noel Grandin --- compilerplugins/clang/stringviewdangle.cxx | 117 ++++++++++++++++++++++++ compilerplugins/clang/test/stringviewdangle.cxx | 37 ++++++++ 2 files changed, 154 insertions(+) create mode 100644 compilerplugins/clang/stringviewdangle.cxx create mode 100644 compilerplugins/clang/test/stringviewdangle.cxx (limited to 'compilerplugins') diff --git a/compilerplugins/clang/stringviewdangle.cxx b/compilerplugins/clang/stringviewdangle.cxx new file mode 100644 index 000000000000..99cb852d03b6 --- /dev/null +++ b/compilerplugins/clang/stringviewdangle.cxx @@ -0,0 +1,117 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +#ifndef LO_CLANG_SHARED_PLUGINS + +#include +#include +#include +#include +#include + +#include "plugin.hxx" +#include "check.hxx" +#include "compat.hxx" +#include "config_clang.h" +#include "clang/AST/CXXInheritance.h" +#include "clang/AST/StmtVisitor.h" + +/** +Look for places where we are assigning a temporary O[U]String to a std::*string_view, which leads +to a view pointing to freed memory. +*/ + +namespace +{ +class StringViewDangle : public loplugin::FilteringPlugin +{ +public: + explicit StringViewDangle(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + bool preRun() override { return true; } + + virtual void run() override + { + if (!preRun()) + return; + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const*); + bool VisitVarDecl(VarDecl const*); +}; + +static const Expr* IgnoreImplicitAndConversionOperator(const Expr* expr) +{ + expr = expr->IgnoreImplicit(); + if (auto memberCall = dyn_cast(expr)) + { + if (auto conversionDecl = dyn_cast_or_null(memberCall->getMethodDecl())) + { + if (!conversionDecl->isExplicit()) + expr = memberCall->getImplicitObjectArgument()->IgnoreImpCasts(); + } + } + return expr; +} + +bool StringViewDangle::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* cxxOperatorCallExpr) +{ + if (ignoreLocation(cxxOperatorCallExpr)) + return true; + + auto op = cxxOperatorCallExpr->getOperator(); + if (op != OO_Equal) + return true; + if (!loplugin::TypeCheck(cxxOperatorCallExpr->getType()) + .ClassOrStruct("basic_string_view") + .StdNamespace()) + return true; + auto expr = IgnoreImplicitAndConversionOperator(cxxOperatorCallExpr->getArg(1)); + auto tc = loplugin::TypeCheck(expr->getType()); + if (!tc.Class("OUString").Namespace("rtl").GlobalNamespace() + && !tc.Class("OString").Namespace("rtl").GlobalNamespace()) + return true; + if (!isa(expr)) + return true; + report(DiagnosticsEngine::Warning, "view pointing into temporary i.e. dangling", + cxxOperatorCallExpr->getExprLoc()) + << cxxOperatorCallExpr->getSourceRange(); + return true; +} + +bool StringViewDangle::VisitVarDecl(VarDecl const* varDecl) +{ + if (ignoreLocation(varDecl)) + return true; + if (!loplugin::TypeCheck(varDecl->getType()).ClassOrStruct("basic_string_view").StdNamespace()) + return true; + if (!varDecl->hasInit()) + return true; + auto expr = IgnoreImplicitAndConversionOperator(varDecl->getInit()); + auto tc = loplugin::TypeCheck(expr->getType()); + if (!tc.Class("OUString").Namespace("rtl").GlobalNamespace() + && !tc.Class("OString").Namespace("rtl").GlobalNamespace()) + return true; + if (!isa(expr)) + return true; + report(DiagnosticsEngine::Warning, "view pointing into temporary i.e. dangling", + varDecl->getLocation()) + << varDecl->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration stringviewdangle("stringviewdangle"); +} + +#endif // LO_CLANG_SHARED_PLUGINS + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/test/stringviewdangle.cxx b/compilerplugins/clang/test/stringviewdangle.cxx new file mode 100644 index 000000000000..0a8d2aa54b44 --- /dev/null +++ b/compilerplugins/clang/test/stringviewdangle.cxx @@ -0,0 +1,37 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include + +#include +#include + +#include +#include +#include +#include +#include + +namespace test1 +{ +OUString foo1(); +OUString& foo2(); +void f1() +{ + // expected-error@+1 {{view pointing into temporary i.e. dangling [loplugin:stringviewdangle]}} + std::u16string_view v = foo1(); + // expected-error@+1 {{view pointing into temporary i.e. dangling [loplugin:stringviewdangle]}} + v = foo1(); + + // no warning expected + std::u16string_view v2 = foo2(); + v2 = foo2(); +} +} +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit