[Libreoffice-commits] core.git: compilerplugins/clang

Stephan Bergmann (via logerrit) logerrit at kemper.freedesktop.org
Mon Oct 21 18:07:14 UTC 2019


 compilerplugins/clang/stringconstant.cxx      |   24 ++++++++++++++++--------
 compilerplugins/clang/test/stringconstant.cxx |   18 ++++++++++++++++++
 2 files changed, 34 insertions(+), 8 deletions(-)

New commits:
commit 61dc9b6aab2870faeb7cf26cdfb3d3e941642a59
Author:     Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Mon Oct 21 17:00:06 2019 +0200
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Mon Oct 21 20:05:49 2019 +0200

    Make loplugin:stringcostant find some misuses of OUStringBuffer::append
    
    ...like the one manually found at 06845c14a17146b0ec50dd3704b6af9ee13927bc "This
    should have become a UTF-16 string literal".  (No further misuses were found
    across the code base.)
    
    Change-Id: I0b604bdaaa38bd9248440ff7bd7bf0545fc6426a
    Reviewed-on: https://gerrit.libreoffice.org/81250
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/compilerplugins/clang/stringconstant.cxx b/compilerplugins/clang/stringconstant.cxx
index 309bf900e377..2360ef2e3620 100644
--- a/compilerplugins/clang/stringconstant.cxx
+++ b/compilerplugins/clang/stringconstant.cxx
@@ -772,6 +772,12 @@ bool StringConstant::VisitCallExpr(CallExpr const * expr) {
         }
         return true;
     }
+    if (dc.Function("append").Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()
+        && fdecl->getNumParams() == 1)
+    {
+        handleChar(expr, 0, fdecl, "", TreatEmpty::Error, false);
+        return true;
+    }
     if ((dc.Function("appendAscii").Class("OUStringBuffer").Namespace("rtl")
          .GlobalNamespace())
         && fdecl->getNumParams() == 1)
@@ -1847,14 +1853,16 @@ void StringConstant::handleChar(
     }
     std::string repl(replacement);
     checkEmpty(expr, callee, treatEmpty, n, &repl);
-    reportChange(
-        expr, ChangeKind::Char, callee->getQualifiedNameAsString(), repl,
-        (literal
-         ? (n == 0
-            ? PassThrough::EmptyConstantString
-            : PassThrough::NonEmptyConstantString)
-         : PassThrough::No),
-        nonArray, rewriteFrom, rewriteTo);
+    if (!repl.empty()) {
+        reportChange(
+            expr, ChangeKind::Char, callee->getQualifiedNameAsString(), repl,
+            (literal
+             ? (n == 0
+                ? PassThrough::EmptyConstantString
+                : PassThrough::NonEmptyConstantString)
+             : PassThrough::No),
+            nonArray, rewriteFrom, rewriteTo);
+    }
 }
 
 void StringConstant::handleCharLen(
diff --git a/compilerplugins/clang/test/stringconstant.cxx b/compilerplugins/clang/test/stringconstant.cxx
index 066648c8871d..1e325633d856 100644
--- a/compilerplugins/clang/test/stringconstant.cxx
+++ b/compilerplugins/clang/test/stringconstant.cxx
@@ -13,6 +13,7 @@
 
 #include "com/sun/star/uno/Reference.hxx"
 #include "rtl/strbuf.hxx"
+#include "rtl/ustrbuf.hxx"
 
 extern void foo(OUString const &);
 
@@ -108,6 +109,23 @@ int main() {
     (void) OUString("\x80", 1, RTL_TEXTENCODING_UTF8); // expected-error {{suspicious 'rtl::OUString' constructor with text encoding 'RTL_TEXTENCODING_UTF8' but non-UTF-8 content [loplugin:stringconstant]}}
 
     (void) OUString("\xC2\x80", 2, RTL_TEXTENCODING_UTF8); // expected-error {{simplify construction of 'OUString' with UTF-8 content as OUString(u"\u0080") [loplugin:stringconstant]}}
+
+    OUStringBuffer ub;
+    ub.append(""); // expected-error {{call of 'rtl::OUStringBuffer::append' with suspicious empty string constant argument [loplugin:stringconstant]}}
+    ub.append("foo\0bar"); // expected-error {{call of 'rtl::OUStringBuffer::append' with string constant argument containing embedded NULLs [loplugin:stringconstant]}}
+    ub.append("foo\x80" "bar"); // expected-error {{call of 'rtl::OUStringBuffer::append' with string constant argument containing non-ASCII characters [loplugin:stringconstant]}}
+    char const sc1[] = "";
+    ub.append(sc1); // expected-error {{call of 'rtl::OUStringBuffer::append' with suspicious empty string constant argument [loplugin:stringconstant]}}
+    char const sc2[] = "foo\0bar";
+    ub.append(sc2); // expected-error {{call of 'rtl::OUStringBuffer::append' with string constant argument containing embedded NULLs [loplugin:stringconstant]}}
+    char const sc3[] = "foo\x80" "bar";
+    ub.append(sc3); // expected-error {{call of 'rtl::OUStringBuffer::append' with string constant argument containing non-ASCII characters [loplugin:stringconstant]}}
+    char const sc4[] = {'f', 'o', 'o', 'b', 'a', 'r'};
+    ub.append(sc4); // expected-error {{call of 'rtl::OUStringBuffer::append' with string constant argument lacking a terminating NULL [loplugin:stringconstant]}}
+    ub.append(static_cast<char const *>(sc1));
+    ub.append(static_cast<char const *>(sc2)); // at runtime: append "foo"
+    ub.append(static_cast<char const *>(sc3)); // at runtime: assert
+    ub.append(static_cast<char const *>(sc4)); // at runtime: UB
 }
 
 


More information about the Libreoffice-commits mailing list