Summary: This helps avoid some unintended reports where the actual is known to point to a specific object before a call to a skipped function. This requires a change in the plugin to export more info about const types. Reviewed By: dulmarod Differential Revision: D3711901 fbshipit-source-id: f5c903emaster
parent
7603395ee9
commit
145cb744f6
@ -1 +1 @@
|
||||
Subproject commit 02753b33ffcdc9607907270b3d555c4c50738b95
|
||||
Subproject commit 9ce2266db9bdf23d70ddaed093decdeeb6bf8db3
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
|
||||
struct lol {
|
||||
int f;
|
||||
};
|
||||
|
||||
typedef const std::shared_ptr<lol>& const_lol;
|
||||
|
||||
void skip_const(const std::shared_ptr<lol>& foo);
|
||||
void skip_const2(int x, const std::shared_ptr<lol>& foo, int y, bool z);
|
||||
void skip_no_const(std::shared_ptr<lol>& foo);
|
||||
void skip_typedef(const_lol foo);
|
||||
|
||||
void test_pointer(const std::shared_ptr<lol>& foo) {
|
||||
// create a case split where one post-condition has foo == null
|
||||
if (foo) {
|
||||
foo->f = 4;
|
||||
}
|
||||
}
|
||||
|
||||
void skip_then_split_case() {
|
||||
auto foo = std::make_shared<lol>();
|
||||
skip_no_const(foo); // Infer havocs foo here since it's not const
|
||||
test_pointer(
|
||||
foo); // this call creates a case split, foo can be null in one branch
|
||||
foo->f = 12; // error
|
||||
}
|
||||
|
||||
void const_skip_then_split_case() {
|
||||
auto foo = std::make_shared<lol>();
|
||||
skip_const(foo); // Infer shouldn't havoc foo here since it's const...
|
||||
test_pointer(foo); /* ...so foo cannot be null here, even if there is an
|
||||
explicit null post... */
|
||||
foo->f = 12; // no error
|
||||
}
|
||||
|
||||
// same as above but make sure infer pinpoints the correct const argument
|
||||
void const_skip2_then_split_case() {
|
||||
auto foo = std::make_shared<lol>();
|
||||
skip_const2(0, foo, 0, 0);
|
||||
test_pointer(foo);
|
||||
foo->f = 12; // no error
|
||||
}
|
||||
|
||||
// same as above but hide the type under a typedef
|
||||
void typedef_skip_then_split_case() {
|
||||
auto foo = std::make_shared<lol>();
|
||||
skip_typedef(foo);
|
||||
test_pointer(foo);
|
||||
foo->f = 12; // no error
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (c) 2016 - 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.cpp.infer;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static utils.matchers.ResultContainsExactly.containsExactly;
|
||||
|
||||
|
||||
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 SkipFunctionWithConstFormalsTest {
|
||||
|
||||
public static final String FILE =
|
||||
"infer/tests/codetoanalyze/cpp/errors/npe/skip_function_with_const_formals.cpp";
|
||||
|
||||
private static ImmutableList<String> inferCmd;
|
||||
|
||||
public static final String NULL_DEREFERENCE = "NULL_DEREFERENCE";
|
||||
|
||||
@ClassRule
|
||||
public static DebuggableTemporaryFolder folder =
|
||||
new DebuggableTemporaryFolder();
|
||||
|
||||
@BeforeClass
|
||||
public static void runInfer() throws InterruptedException, IOException {
|
||||
inferCmd = InferRunner.createCPPInferCommand(folder, FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInferRunsNullDerefFunctionsErrorIsFound()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
String[] procedures = {
|
||||
"skip_then_split_case",
|
||||
"typedef_skip_then_split_case", // Infer should not report there
|
||||
// but currently we don't look
|
||||
// at const qualifiers
|
||||
// underneath typedef's
|
||||
};
|
||||
InferResults inferResults = InferRunner.runInferCPP(inferCmd);
|
||||
assertThat(
|
||||
"Results should contain null dereference",
|
||||
inferResults,
|
||||
containsExactly(
|
||||
NULL_DEREFERENCE,
|
||||
FILE,
|
||||
procedures
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue