infer的安裝
- 直接通過
Homebrew
進行安裝
brew install infer
- 通過源碼進行安裝铺罢,可以參考GitHub上的說明
使用infer過程中遇到的問題及解決方案
問題1
- 問題描述
# 直接分析我這里沒有成功
infer run --skip-analysis-in-path Pods --no-xcpretty --keep-going -- xcodebuild -workspace xxxx.xcworkspace -scheme xxxx -configuration Debug -sdk iphoneos
# 使用上述分析命令會有編譯失敗的問題(已脫敏)
** BUILD FAILED **
The following build commands failed:
這一段省略了...
(8 failures)
Internal Error: /usr/local/Cellar/infer/0.17.0/lib/infer/infer/bin/../lib/python/infer.py
-j 8 --project-root /Users/xxx/Desktop/Code/xxx-iOS/xxx --out
/Users/xxx/Desktop/Code/xxx-iOS/xxx/infer-out -- xcodebuild
-workspace xxx.xcworkspace -scheme xxx -configuration Debug -sdk
iphoneos:
exited with code 65
Error backtrace:
Raised at file "string.ml", line 145, characters 16-31
Called from file "string.ml" (inlined), line 149, characters 17-46
Called from file "src/string.ml", line 407, characters 12-33
Called from file "src/string.ml", line 416, characters 11-33
Re-raised at file "base/Die.ml", line 26, characters 8-56
Called from file "integration/Driver.ml", line 171, characters 2-16
Called from file "integration/Driver.ml", line 272, characters 6-409
Called from file "integration/Driver.ml", line 323, characters 2-29
Called from file "base/Utils.ml", line 398, characters 16-20
Called from file "scuba/ScubaLogging.ml", line 66, characters 29-44
Called from file "infer.ml", line 20, characters 2-36
Called from file "base/Utils.ml", line 398, characters 16-20
Called from file "scuba/ScubaLogging.ml", line 66, characters 29-44
Called from file "infer.ml", line 137, characters 8-54
- 解決辦法:編譯數(shù)據(jù)生成
compile_commands.json
文件纵潦,再對生成的數(shù)據(jù)文件進行分析伴挚,官方文檔中也有說明The most robust way is to generate a compilation database, then pass that database to Infer
,后面會給出完整命令酪我,繼續(xù)往下看
問題2
- 問題描述
Error message:
clang-8: error: unknown argument: '-index-store-path'
*** Infer needs a working compilation command to run.
-
clang-8: error: unknown argument: '-index-store-path'
錯誤- 分析:由于從Xcode 9.0開始將
-index-store-path
參數(shù)添加到構(gòu)建命令中。而infer所依賴的clang還不支持此參數(shù)且叁。在項目Build Setting中搜索index
并將Enable Index-While-Building Functionality
選項設(shè)置為NO -
主工程設(shè)置如下圖
1.jpg -
Pods工程設(shè)置如下圖
2.jpg
- 分析:由于從Xcode 9.0開始將
-
Infer needs a working compilation command to run.
錯誤- 解決方案:在分析命令中添加
--no-xcpretty
- 解決方案:在分析命令中添加
- 在分析過程中由于一些錯誤會中斷分析都哭,可以在分析命令中添加
--keep-going
參數(shù)來忽略這些錯誤,繼續(xù)分析
使用infer進行項目分析
-
未使用
Pods工程的項目比較簡單谴古,命令如下
infer run -- xcodebuild -target <target name> -configuration <build configuration> -sdk iphoneos
-
使用了Pods工程
的項目
# 進入項目根目錄
cd projectRootMenu
# infer默認(rèn)是增量編譯质涛,只會分析變動的代碼,如果我們想整體編譯的話掰担,需要clean一下項目
xcodebuild clean
或者
xcodebuild -workspace xxx.xcworkspace -scheme xxx -sdk iphoneos clean
# 可以通過如下命令查看-sdk版本汇陆,如果使用iphoneos需要**檢查證書**
xcodebuild -showsdks
# 分析項目命令步驟如下
# 第1步:編譯工程生成編譯日志xcodebuild.log文件
xcodebuild -workspace xxx.xcworkspace -scheme xxx -configuration Debug -sdk iphoneos COMPILER_INDEX_STORE_ENABLE=NO | tee xcodebuild.log
# 第2步:根據(jù)編譯日志生成編譯數(shù)據(jù)compile_commands.json文件
xcpretty -r json-compilation-database -o compile_commands.json < xcodebuild.log > /dev/null
# 第3步:基于編譯數(shù)據(jù)compile_commands.json文件進行靜態(tài)分析
infer run --skip-analysis-in-path Pods --no-xcpretty --keep-going --compilation-database-escaped compile_commands.json
infer檢測的錯誤類型說明
-
RESOURCE_LEAK
資源泄漏- 此問題在Java/C/OC都存在,資源代表文件带饱、sockets連接等毡代,使用后需要關(guān)閉
-
MEMORY_LEAK
內(nèi)存泄漏- 項目代碼全面啟動了ARC進行內(nèi)存管理,在OC層沒有掃描出內(nèi)存泄露勺疼。目前掃描出的內(nèi)存泄露問題都是使用了malloc或者ralloc等c語言內(nèi)存申請函數(shù)教寂,在函數(shù)提前return前沒有及時free
-
Retain cycle
內(nèi)存死鎖只存在OC中,A 創(chuàng)造B执庐,B也創(chuàng)造了A酪耕,然后你等我,我等你轨淌,都無法釋放 -
NULL_DEREFERENCE
空指針的錯誤- 傳參為0的情況下迂烁。例如代碼中,在調(diào)用showAlertViewA()時递鹉,將tag傳參為0盟步,infer檢測此處傳0,判斷為一個NULL空指針躏结,所以爆出警告却盘。這里可以理解為誤報,不會出現(xiàn)問題。
- 通過malloc黄橘、calloc兆览、realloc等函數(shù)申請內(nèi)存,當(dāng)內(nèi)存不足時旬陡,有可能會在該函數(shù)中返回NULL拓颓,如果沒有做NULL的判斷,則infer會警告
- 在創(chuàng)建NSArray或者NSDictionary時描孟,傳入的參數(shù)有可能會nil驶睦。由于NSArray與NSDictionary不接受空指針,所以在對其addObject或者setObject:forKey: 時需要進行判斷一下是否為nil
- 當(dāng)一個對象聲明后匿醒,沒有初始化场航,就被引用了,這個時候會報空指針錯誤廉羔。
-
ASSIGN_POINTER_WARNING
- 由于在mrc時代溉痢,沒有weak指針,所以一些view的屬性聲明是
__unsafe__unretain__
的形式憋他,在arc中孩饼,這個屬性被判斷為assign,需要將其修改為weak或者strong
- 由于在mrc時代溉痢,沒有weak指針,所以一些view的屬性聲明是
-
DIRECT_ATOMIC_PROPERTY_ACCESS
- 在代碼中使用了使用了一個atomic的成員變量竹挡,infer建議我們將atomic修改為nonatomic镀娶。由于OC中,屬性會被默認(rèn)設(shè)置為atomic屬性揪罕,我們需要顯示將屬性聲明為nonatomic梯码。
-
IVAR_NOT_NULL_CHECKED
- 在代碼中調(diào)用block,運行代碼時好啰,沒有做判空處理轩娶。即需要改動為,if(block){block()}
-
BAD_POINTER_COMPARISON
- 沒有判斷一個NSNumber類型的對象是不是空框往?
-
TAINTED_VALUE_REACHING_SENSITIVE_FUNCTION
- 代碼中使用了cookie的value鳄抒。可以理解為誤報
-
PARAMETER_NOT_NULL_CHECKED
- 傳參時沒有判斷是否為null椰弊,加一次判斷就可以了
-
STRONG_DELEGATE_WARNING
- 將一個delegate屬性設(shè)置為strong的類型嘁酿。
-
PREMATURE_NIL_TERMINATION_ARGUMENT
- 沒有判斷是否為空
-
REGISTERED_OBSERVER_BEING_DEALLOCATED
- 創(chuàng)建一個對象后,監(jiān)聽了某些通知男应,但是沒有在dealloc中釋放該通知。項目中出現(xiàn)這種問題的類娱仔,基本都是單例沐飘,不會被銷毀。
-
UNSAFE_CALL_TO_OPTIONAL_METHOD
- This is a call to an
@optional
protocol method. Calling it without checking if its implemented
- This is a call to an
-
DEAD_STORE
- 未使用的變量
-
UNINITIALIZED_VALUE
- 值未初始化
-
POINTER_TO_CONST_OBJC_CLASS
- const錯誤用法
-
RETAIN_CYCLE
- 循環(huán)引用