New Infer rule for dispatch_once_t

Reviewed By: martintrojer

Differential Revision: D12888401

fbshipit-source-id: 1c4af8922
master
Dino Distefano 6 years ago committed by Facebook Github Bot
parent 9e9deb93be
commit 5d533bba5c

@ -267,3 +267,16 @@ DEFINE-CHECKER DISCOURAGED_WEAK_PROPERTY_CUSTOM_SETTER = {
SET severity = "WARNING";
SET mode = "OFF";
};
DEFINE-CHECKER WRONG_SCOPE_FOR_DISPATCH_ONCE_T = {
SET report_when =
WHEN
NOT (is_global_var() OR is_static_local_var()) AND
has_type("dispatch_once_t")
HOLDS-IN-NODE VarDecl;
SET message = "Variables of type dispatch_once_t must have global or static scope. The result of using this type with automatic or dynamic allocation is undefined.";
SET severity = "WARNING";
SET mode = "OFF";
};

@ -0,0 +1,41 @@
/*
* 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/Foundation.h>
@interface A : NSObject
@end
@implementation A
+ (instancetype)sharedInstanceOK {
static id _sharedInstance = nil;
static dispatch_once_t onceTokenA;
dispatch_once(&onceTokenA, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
@end
@interface B : NSObject
@end
@implementation B
+ (instancetype)sharedInstanceBAD {
static id _sharedInstance = nil;
dispatch_once_t onceTokenB;
dispatch_once(&onceTokenB, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
@end

@ -30,6 +30,7 @@ codetoanalyze/objc/linters/badpointer.m, bad5, 97, BAD_POINTER_COMPARISON, no_bu
codetoanalyze/objc/linters/badpointer.m, bad6, 104, BAD_POINTER_COMPARISON, no_bucket, WARNING, []
codetoanalyze/objc/linters/badpointer.m, bad8, 119, BAD_POINTER_COMPARISON, no_bucket, WARNING, []
codetoanalyze/objc/linters/badpointer.m, bad9, 126, BAD_POINTER_COMPARISON, no_bucket, WARNING, []
codetoanalyze/objc/linters/dispatch.m, B_sharedInstanceBAD, 33, WRONG_SCOPE_FOR_DISPATCH_ONCE_T, no_bucket, WARNING, []
codetoanalyze/objc/linters/implicit_cast.m, Implicit_cast_call_with_boxed_int, 67, POINTER_TO_INTEGRAL_IMPLICIT_CAST, no_bucket, WARNING, []
codetoanalyze/objc/linters/implicit_cast.m, Implicit_cast_call_with_string, 60, POINTER_TO_INTEGRAL_IMPLICIT_CAST, no_bucket, WARNING, []
codetoanalyze/objc/linters/implicit_cast.m, Implicit_cast_ivar_dictionary_item_call_funct_with_int, 45, POINTER_TO_INTEGRAL_IMPLICIT_CAST, no_bucket, WARNING, []

Loading…
Cancel
Save