PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/project/dexter-vd-cpp/src/java/com/samsung/sec/dexter/vdcpp/checkerlogic/NoFreeOfReturnValueCheckerLogic.java

https://gitlab.com/github-cloud-corporation/Dexter
Java | 323 lines | 250 code | 58 blank | 15 comment | 27 complexity | c94f0257cc0ae2b9df3535d67c68194e MD5 | raw file
  1. /**
  2. * @file NoFreeOfReturnValue.java
  3. * @brief NoFreeOfReturnValue class source file
  4. * @author adarsh.t
  5. *
  6. * Copyright 2015 by Samsung Electronics, Inc.
  7. * All rights reserved.
  8. *
  9. * Project Description :
  10. * This software is the confidential and proprietary information
  11. * of Samsung Electronics, Inc. ("Confidential Information"). You
  12. * shall not disclose such Confidential Information and shall use
  13. * it only in accordance with the terms of the license agreement
  14. * you entered into with Samsung Electronics.
  15. */
  16. package com.samsung.sec.dexter.vdcpp.checkerlogic;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.eclipse.cdt.core.dom.ast.ASTVisitor;
  21. import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
  22. import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
  23. import org.eclipse.cdt.core.dom.ast.IASTExpression;
  24. import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
  25. import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
  26. import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
  27. import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
  28. import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
  29. import org.eclipse.cdt.core.dom.ast.IASTName;
  30. import org.eclipse.cdt.core.dom.ast.IASTNode;
  31. import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
  32. import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
  33. import org.eclipse.cdt.core.dom.ast.IBinding;
  34. import com.samsung.sec.dexter.core.analyzer.AnalysisConfig;
  35. import com.samsung.sec.dexter.core.analyzer.AnalysisResult;
  36. import com.samsung.sec.dexter.core.checker.Checker;
  37. import com.samsung.sec.dexter.core.defect.PreOccurence;
  38. import com.samsung.sec.dexter.vdcpp.plugin.DexterVdCppPlugin;
  39. import com.samsung.sec.dexter.vdcpp.util.CppUtil;
  40. public class NoFreeOfReturnValueCheckerLogic implements ICheckerLogic{
  41. private IASTTranslationUnit translationUnit;
  42. private String[] lstMethods=null;
  43. @Override
  44. public void analyze(final AnalysisConfig config, final AnalysisResult result,
  45. final Checker checker, IASTTranslationUnit unit) {
  46. translationUnit =unit;
  47. lstMethods= checker.getProperty("method-list").split(",");
  48. ASTVisitor visitor = createVisitor(config, result, checker);
  49. visitor.shouldVisitDeclarations = true;
  50. unit.accept(visitor);
  51. }
  52. private ASTVisitor createVisitor(final AnalysisConfig config,
  53. final AnalysisResult result, final Checker checker) {
  54. ASTVisitor visitor = new ASTVisitor() {
  55. @Override
  56. public int visit(IASTDeclaration ast ) {
  57. if(ast instanceof IASTFunctionDefinition)
  58. {
  59. visitFunction(config, result, checker, ast);
  60. }
  61. else if(ast instanceof IASTSimpleDeclaration)
  62. {
  63. visitOtherCompoundDeclaration(config, result, checker, ast);
  64. }
  65. return super.visit(ast);
  66. }
  67. private void visitOtherCompoundDeclaration(
  68. final AnalysisConfig config, final AnalysisResult result,
  69. final Checker checker, final IASTDeclaration ast) {
  70. ASTVisitor visitor = new ASTVisitor() {
  71. public int visit(IASTExpression astExpression ) {
  72. if(astExpression instanceof IASTFunctionCallExpression)
  73. {
  74. visitFunctionCallExpressionForCompoundBlocks(
  75. config, result, checker, ast, astExpression);
  76. }
  77. return ASTVisitor.PROCESS_CONTINUE;
  78. }
  79. private void visitFunctionCallExpressionForCompoundBlocks(
  80. final AnalysisConfig config,
  81. final AnalysisResult result, final Checker checker,
  82. final IASTDeclaration ast,
  83. IASTExpression astExpression) {
  84. IASTExpression functionCallExpression = ((IASTFunctionCallExpression) astExpression).getFunctionNameExpression();
  85. String functionName =functionCallExpression.getRawSignature();
  86. if(functionCallExpression instanceof IASTIdExpression)
  87. {
  88. functionName =((IASTIdExpression) functionCallExpression).getName().toString();
  89. }
  90. for (String methodName : lstMethods)
  91. {
  92. if(functionName.equals(methodName))
  93. {
  94. IASTNode node =astExpression.getParent();
  95. if(node instanceof IASTBinaryExpression)
  96. {
  97. visitBinaryExpression(config, result,
  98. checker, ast, astExpression,
  99. functionName, node);
  100. }
  101. }
  102. }
  103. }
  104. private void visitBinaryExpression(
  105. final AnalysisConfig config,
  106. final AnalysisResult result, final Checker checker,
  107. final IASTDeclaration ast,
  108. IASTExpression astExpression, String functionName,
  109. IASTNode node) {
  110. IASTExpression binaryExpression =((IASTBinaryExpression) node).getOperand1();
  111. if(binaryExpression instanceof IASTIdExpression)
  112. {
  113. IASTName name =((IASTIdExpression) binaryExpression).getName();
  114. String ExpName =name.toString();
  115. final IBinding binding = name.resolveBinding();
  116. if ((binding != null) )
  117. {
  118. boolean status= checkforFreeFunctionCall(
  119. ast, ExpName, binding);
  120. if(!status)
  121. {
  122. fillDefectData( config,
  123. result, checker,
  124. astExpression.getFileLocation(), checker.getDescription(), functionName);
  125. }
  126. }
  127. }
  128. }
  129. };
  130. visitor.shouldVisitExpressions = true;
  131. ast.accept(visitor);
  132. }
  133. private void visitFunction(final AnalysisConfig config,
  134. final AnalysisResult result, final Checker checker,
  135. final IASTDeclaration ast) {
  136. ASTVisitor visitor = new ASTVisitor() {
  137. public int visit(IASTExpression astExpression ) {
  138. if(astExpression instanceof IASTFunctionCallExpression)
  139. {
  140. visitFunctionCallExpressionForFunctionBlocks(
  141. config, result, checker, ast, astExpression);
  142. }
  143. return ASTVisitor.PROCESS_CONTINUE;
  144. }
  145. private void visitFunctionCallExpressionForFunctionBlocks(
  146. final AnalysisConfig config,
  147. final AnalysisResult result, final Checker checker,
  148. final IASTDeclaration ast,
  149. IASTExpression astExpression) {
  150. IASTExpression functionCallExpression = ((IASTFunctionCallExpression) astExpression).getFunctionNameExpression();
  151. String functionName =functionCallExpression.getRawSignature();
  152. if(functionCallExpression instanceof IASTIdExpression)
  153. {
  154. functionName =((IASTIdExpression) functionCallExpression).getName().toString();
  155. }
  156. for (String methodName : lstMethods)
  157. {
  158. if(functionName.equals(methodName))
  159. {
  160. IASTNode node =astExpression.getParent();
  161. if(node instanceof IASTBinaryExpression)
  162. {
  163. visitBinaryExpressionForFunctionBlocks(
  164. config, result, checker, ast,
  165. astExpression, functionName, node);
  166. }
  167. }
  168. }
  169. }
  170. private void visitBinaryExpressionForFunctionBlocks(
  171. final AnalysisConfig config,
  172. final AnalysisResult result, final Checker checker,
  173. final IASTDeclaration ast,
  174. IASTExpression astExpression, String functionName,
  175. IASTNode node) {
  176. IASTExpression binaryExpression =((IASTBinaryExpression) node).getOperand1();
  177. if(binaryExpression instanceof IASTIdExpression)
  178. {
  179. IASTName name =((IASTIdExpression) binaryExpression).getName();
  180. String ExpName =name.toString();
  181. final IBinding binding = name.resolveBinding();
  182. if ((binding != null) )
  183. {
  184. boolean status= checkforFreeFunctionCall(
  185. ast, ExpName, binding);
  186. if(!status)
  187. {
  188. fillDefectData( config,
  189. result, checker,
  190. astExpression.getFileLocation(), checker.getDescription(), functionName);
  191. }
  192. }
  193. }
  194. }
  195. };
  196. visitor.shouldVisitExpressions = true;
  197. ast.accept(visitor);
  198. }
  199. private boolean checkforFreeFunctionCall(
  200. final IASTDeclaration ast, String ExpName,
  201. final IBinding binding) {
  202. boolean status =false;
  203. final IASTName[] references = ast.getTranslationUnit().getReferences(binding);
  204. for (IASTName reference : references)
  205. {
  206. IASTNode parent =reference.getParent().getParent();
  207. if(parent instanceof IASTFunctionCallExpression)
  208. {
  209. IASTExpression expression = ((IASTFunctionCallExpression) parent).getFunctionNameExpression();
  210. IASTInitializerClause[] expParameter =((IASTFunctionCallExpression) parent).getArguments();
  211. List<String> parameter =new ArrayList<String>();
  212. for (IASTInitializerClause string : expParameter) {
  213. parameter.add(string.toString());
  214. }
  215. if(expression instanceof IASTIdExpression)
  216. {
  217. String functionName =((IASTIdExpression) expression).getName().toString();
  218. if(functionName.equals("free") && parameter.contains(ExpName))
  219. {
  220. status =true;
  221. }
  222. }
  223. }
  224. }
  225. return status;
  226. }
  227. private void fillDefectData(AnalysisConfig config,
  228. AnalysisResult result, Checker checker,
  229. IASTFileLocation fileLocation, String message, String declaratorName) {
  230. PreOccurence preOcc = createPreOccurence(config, checker, fileLocation, message,declaratorName);
  231. result.addDefectWithPreOccurence(preOcc);
  232. }
  233. private PreOccurence createPreOccurence(AnalysisConfig config,
  234. Checker checker, IASTFileLocation fileLocation, String msg,String decName) {
  235. final int startLine = fileLocation.getStartingLineNumber();
  236. final int endLine = fileLocation.getEndingLineNumber();
  237. final int startOffset = fileLocation.getNodeOffset();
  238. final int endOffset = startOffset + fileLocation.getNodeLength();
  239. Map<String,String> tempmap =CppUtil.extractModuleName(translationUnit, startLine);
  240. String className =tempmap.get("className");
  241. String methodName =tempmap.get("methodName");
  242. PreOccurence preOcc = new PreOccurence();
  243. preOcc.setCheckerCode(checker.getCode());
  244. preOcc.setFileName(config.getFileName());
  245. preOcc.setModulePath(config.getModulePath());
  246. preOcc.setClassName(className);
  247. preOcc.setMethodName(methodName);
  248. preOcc.setLanguage(config.getLanguageEnum().toString());
  249. preOcc.setSeverityCode(checker.getSeverityCode());
  250. preOcc.setMessage(checker.getDescription());
  251. preOcc.setToolName(DexterVdCppPlugin.PLUGIN_NAME);
  252. preOcc.setStartLine(startLine);
  253. preOcc.setEndLine(endLine);
  254. preOcc.setCharStart(startOffset);
  255. preOcc.setCharEnd(endOffset);
  256. preOcc.setVariableName(decName);
  257. msg =msg.replace("${methodName}", decName);
  258. preOcc.setMessage(msg);
  259. preOcc.setStringValue(msg);
  260. return preOcc;
  261. }
  262. };
  263. return visitor;
  264. }
  265. }