Add std::string as container-method for objc, and change string matching to regexes.

Reviewed By: jeremydubreil

Differential Revision: D8989344

fbshipit-source-id: e716cb50b
master
Martin Trojer 6 years ago committed by Facebook Github Bot
parent 6e44cae7cd
commit 46e2c153f1

@ -65,7 +65,7 @@ endif
ifneq ($(XCODE_SELECT),no) ifneq ($(XCODE_SELECT),no)
BUILD_SYSTEMS_TESTS += xcodebuild_no_xcpretty objc_getters_setters objc_missing_fld BUILD_SYSTEMS_TESTS += xcodebuild_no_xcpretty objc_getters_setters objc_missing_fld
DIRECT_TESTS += \ DIRECT_TESTS += \
objc_frontend objc_errors objc_linters objc_ioslints objcpp_errors \ objc_frontend objc_errors objc_linters objc_ioslints objcpp_errors objcpp_nullable \
objcpp_frontend objcpp_linters objc_linters-for-test-only objcpp_linters-for-test-only \ objcpp_frontend objcpp_linters objc_linters-for-test-only objcpp_linters-for-test-only \
objc_linters-def-folder objc_nullable objc_liveness objcpp_liveness objc_uninit objc_linters-def-folder objc_nullable objc_liveness objcpp_liveness objc_uninit
ifneq ($(XCPRETTY),no) ifneq ($(XCPRETTY),no)

@ -59,22 +59,15 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
List.exists ~f:(String.equal simplified_callee_pname) blacklist List.exists ~f:(String.equal simplified_callee_pname) blacklist
let is_objc_container_add_method : Typ.Procname.t -> bool = let container_method_regex =
let method_prefixes = Str.regexp @@ "^\\(NS.*_\\(arrayByAddingObject\\|arrayWithObjects\\|"
[ "addObject:" ^ "dictionaryWithObjects\\|dictionaryWithObjectsAndKeys\\|initWithObjectsAndKeys\\|"
; "arrayByAddingObject:" ^ "addObject\\|insertObject\\|setObject\\):\\|" ^ "std::basic_string\\).*"
; "arrayWithObjects:"
; "dictionaryWithObjects:"
; "dictionaryWithObjectsAndKeys:" let is_objc_container_add_method proc_name =
; "initWithObjectsAndKeys:" let callee_pname = Typ.Procname.to_string proc_name in
; "insertObject:" Str.string_match container_method_regex callee_pname 0
; "setObject:" ]
in
fun proc_name ->
let simplified_callee_pname = Typ.Procname.to_simplified_string proc_name in
List.exists
~f:(fun prefix -> String.is_prefix ~prefix simplified_callee_pname)
method_prefixes
let is_conflicting_report summary report_location = let is_conflicting_report summary report_location =
@ -273,11 +266,11 @@ module TransferFunctions (CFG : ProcCfg.S) = struct
Annotations.ia_is_nullable -> Annotations.ia_is_nullable ->
let call_site = CallSite.make callee_pname loc in let call_site = CallSite.make callee_pname loc in
add_nullable_ap (ret_var, []) (CallSites.singleton call_site) astate add_nullable_ap (ret_var, []) (CallSites.singleton call_site) astate
| Call (_, Direct callee_pname, args, _, loc) when is_objc_container_add_method callee_pname ->
check_nil_in_objc_container proc_data loc args astate
| Call (_, Direct callee_pname, HilExp.AccessExpression receiver :: _, _, loc) | Call (_, Direct callee_pname, HilExp.AccessExpression receiver :: _, _, loc)
when is_non_objc_instance_method callee_pname -> when is_non_objc_instance_method callee_pname ->
check_ap proc_data loc (AccessExpression.to_access_path receiver) astate check_ap proc_data loc (AccessExpression.to_access_path receiver) astate
| Call (_, Direct callee_pname, args, _, loc) when is_objc_container_add_method callee_pname ->
check_nil_in_objc_container proc_data loc args astate
| Call | Call
( ((_, ret_typ) as ret_var) ( ((_, ret_typ) as ret_var)
, Direct callee_pname , Direct callee_pname

@ -0,0 +1,22 @@
/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/NSString.h>
#import <string>
std::string stdStringOK(NSString* s) {
const char* s1 = [s UTF8String];
if (s1) {
std::string s2 = std::string([s UTF8String]);
return s2;
} else
return "";
}
std::string stdStringBad(NSString* s) {
std::string s2 = std::string([s UTF8String]);
return s2;
}

@ -0,0 +1,17 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
TESTS_DIR = ../../..
ANALYZER = checkers
CLANG_OPTIONS = -c
INFER_OPTIONS = --debug-exceptions --no-default-checkers --suggest-nullable --check-nullable --project-root $(TESTS_DIR)
INFERPRINT_OPTIONS = --issues-tests
SOURCES = $(wildcard *.mm)
include $(TESTS_DIR)/clang.make
infer-out/report.json: $(MAKEFILE_LIST)

@ -0,0 +1 @@
codetoanalyze/objcpp/nullable/Examples.mm, stdStringBad, 1, NULLABLE_DEREFERENCE, no_bucket, ERROR, [dereferencing the return of UTF8String,assignment of the nullable value,definition of UTF8String]
Loading…
Cancel
Save