LoadLibraryUsageChecker.cpp (1352B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "LoadLibraryUsageChecker.h" 6 #include "CustomMatchers.h" 7 8 // On MacOS the filesystem is UTF-8, on linux the canonical filename is 8-bit 9 // string. On Windows data loss conversion will occur. This checker restricts 10 // the use of ASCII file functions for loading libraries. 11 12 void LoadLibraryUsageChecker::registerMatchers(MatchFinder *AstMatcher) { 13 AstMatcher->addMatcher( 14 callExpr( 15 allOf(isFirstParty(), 16 callee(functionDecl(anyOf( 17 allOf(isInSystemHeader(), anyOf(hasName("LoadLibraryA"), 18 hasName("LoadLibraryExA"))), 19 hasName("PR_LoadLibrary")))), 20 unless(hasArgument(0, stringLiteral())))) 21 .bind("funcCall"), 22 this); 23 } 24 25 void LoadLibraryUsageChecker::check(const MatchFinder::MatchResult &Result) { 26 const CallExpr *FuncCall = Result.Nodes.getNodeAs<CallExpr>("funcCall"); 27 28 if (FuncCall) { 29 diag(FuncCall->getBeginLoc(), 30 "Usage of ASCII file functions (such as %0) is forbidden.", 31 DiagnosticIDs::Error) 32 << FuncCall->getDirectCallee()->getName(); 33 } 34 }