Reviewed By: dulmarod Differential Revision: D2765074 fb-gh-sync-id: 5362f91master
parent
d48f33c1c3
commit
4feb93e91c
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2015 - present Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
|
@interface B : NSObject
|
||||||
|
+ foo:(id) obj;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation B
|
||||||
|
|
||||||
|
+ (B*) foo:(id) obj
|
||||||
|
{
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
@interface A : NSObject
|
||||||
|
- (NSMutableArray *) allResultsList: (NSArray *) allResults;
|
||||||
|
@end
|
||||||
|
|
||||||
|
|
||||||
|
@implementation A
|
||||||
|
|
||||||
|
// From a diff
|
||||||
|
- (NSMutableArray *) allResultsList: (NSArray *) allResults {
|
||||||
|
NSMutableArray *resultsList = [[NSMutableArray alloc] init];
|
||||||
|
[allResults enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
||||||
|
id *result = [B foo:obj];
|
||||||
|
if (result != nil) {
|
||||||
|
[resultsList addObject:result];
|
||||||
|
}
|
||||||
|
}];
|
||||||
|
return resultsList;
|
||||||
|
}
|
||||||
|
|
||||||
|
// How that code is translated in INFER
|
||||||
|
- (void) foo1: (NSArray*) a {
|
||||||
|
NSArray *objects = a;
|
||||||
|
NSMutableArray *resultsList = [[NSMutableArray alloc] init];
|
||||||
|
void (^enumerateObjectsUsingBlock)(id, NSUInteger, BOOL* ) =
|
||||||
|
^(id obj, NSUInteger idx, BOOL* stop) {
|
||||||
|
id *result = [B foo:obj];
|
||||||
|
if (result != nil) {
|
||||||
|
[resultsList addObject:result];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
BOOL *stop = malloc(sizeof(BOOL));
|
||||||
|
*stop = NO;
|
||||||
|
for (NSUInteger idx = 0; idx < objects.count; idx++) {
|
||||||
|
id object = objects[idx];
|
||||||
|
enumerateObjectsUsingBlock(object, idx, stop);
|
||||||
|
if ( *stop == YES) break;
|
||||||
|
}
|
||||||
|
free(stop);
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2013 - present Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package endtoend.objc;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import utils.DebuggableTemporaryFolder;
|
||||||
|
import utils.InferException;
|
||||||
|
import utils.InferResults;
|
||||||
|
import utils.InferRunner;
|
||||||
|
|
||||||
|
public class NullParamTest {
|
||||||
|
|
||||||
|
public static final String block_file =
|
||||||
|
"infer/tests/codetoanalyze/objc/errors/npe/blockenum.m";
|
||||||
|
|
||||||
|
private static ImmutableList<String> inferCmd;
|
||||||
|
|
||||||
|
public static final String PARAMETER_NOT_NULL_CHECKED = "PARAMETER_NOT_NULL_CHECKED";
|
||||||
|
|
||||||
|
@ClassRule
|
||||||
|
public static DebuggableTemporaryFolder folder =
|
||||||
|
new DebuggableTemporaryFolder();
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void runInfer() throws InterruptedException, IOException {
|
||||||
|
inferCmd = InferRunner.createObjCInferCommand(
|
||||||
|
folder,
|
||||||
|
block_file
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenInferRunsOnFoo1ParameterNilIsNotFound()
|
||||||
|
throws InterruptedException, IOException, InferException {
|
||||||
|
InferResults inferResults = InferRunner.runInferObjC(inferCmd);
|
||||||
|
assertThat(
|
||||||
|
"Results should not contain memory leak",
|
||||||
|
inferResults,
|
||||||
|
doesNotContain(
|
||||||
|
PARAMETER_NOT_NULL_CHECKED,
|
||||||
|
block_file,
|
||||||
|
"foo1:"));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void whenInferRunsOnAllResultsListParameterNilIsNotFound()
|
||||||
|
throws InterruptedException, IOException, InferException {
|
||||||
|
InferResults inferResults = InferRunner.runInferObjC(inferCmd);
|
||||||
|
assertThat(
|
||||||
|
"Results should not contain memory leak",
|
||||||
|
inferResults,
|
||||||
|
doesNotContain(
|
||||||
|
PARAMETER_NOT_NULL_CHECKED,
|
||||||
|
block_file,
|
||||||
|
"allResultsList:"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue