iOS APP静态检测实践之 -- infer
$[timeformat('2021-10-08T10:38:38+08:00')]
#静态检测#代码检测

[toc]

infer简介

参考文章 :https://www.jianshu.com/p/b6204ed62510

Facebook 的 Infer 是一个静态分析工具。Infer 可以分析 Objective-C, Java 或者 C 代码,报告潜在的问题。任何人都可以使用 Infer 检测应用,这可以将那些严重的 bug 扼杀在发布之前,同时防止应用崩溃和性能低下。Infer 可检查 Android 和 Java 代码中的 NullPointException 和 资源泄露。除了以上,Infer 还可发现 iOS 和 C 代码中的内存泄露。

Infer
Infer

安装

Homebrew安装

安装Homebrew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

使用Homebrew安装Infer所需环境

# 安装环境
brew install autoconf automake cmake opam pkg-config sqlite gmp mpfr
brew cask install java

使用Homebrew安装infer

# 安装Infer
brew install infer

源码安装

infer release页面获取最新版本infer-osx-vXX.tar.xz (以osx标识),然后执行下面命令来安装Infer.

使用Infer分析代码

准备工作

  • 分析项目之前先把项目clean 一下
  • 连续两次使用Infer分析项目时,也需要先clean下项目 或者在命令中使用 --reactive命令
  • 分析单个文件的时候 不需要增量分析

使用infer 检测单个文件

  • cd 指定文件InferTest.m 所在的目录
  • infer -- clang -c InferTest.m
  • No issues found 代表没有问题

使用infer检测整个项目

Infer分析普通项目

infer -- xcodebuild -project App.xcodeproj -target App -configuration Debug -sdk iphonesimulator

Infer分析带Workspace(Cocoapods)的项目

infer --  xcodebuild -workspace ./App.xcworkspace -configuration Debug -scheme KooApp

遇到警告错误不中断分析 --keep-going

infer --keep-going -- xcodebuild -project App.xcodeproj -target App -configuration Debug -sdk iphonesimulator
#!/bin/bash

kTargetName="LXCategory"
kProjectName="LXCategory.xcodeproj"
kConfigurationType="Debug"
kSDKName="iphonesimulator"

infer -- xcodebuild clean 

INFER_ARGS="--skip-analysis-in-path^[\"Third\"]" infer -- xcodebuild -target $kTargetName -configuration $kConfigurationType -sdk $kSDKName

忽略分析指定文件

  • 命令模式
INFER_ARGS="--skip-analysis-in-path^[\"Third\"]" infer -- xcodebuild -target $kTargetName -configuration $kConfigurationType -sdk $kSDKName
  • 配置文件模式

在项目目录下增加这样一个.inferconfig的文件, 内容如图, 可以过滤掉Pods文件夹下的第三方库, skip-analysis-in-path是一个数组, 想要过滤其他文件, 只需要增加路径即可。

文件内容:

{
  "skip-analysis-in-path":[
    "test",
    "Third"
  ]
}

指定导出分析文件路径

infer 后面,-- xcodebuild 前面。 -o OuptputPath 能指定导出文件的路径。

infer --keep-going -o $kOuptputPath -- xcodebuild -project App.xcodeproj -target App -configuration Debug -sdk iphonesimulator

导出html文件

这个需要在编译 或者 分析完之后执行此命令(指定导出分析文件路径后,需要在 修改的路径下使用才行。)

infer-explore --html

静态分析(不编译)

infer analyze

编译遇到的问题

  • framework问题
    framework 编译问题设置
    framework 编译问题设置

OCLint

参考文章:https://www.jianshu.com/p/6555048d6a17