Summary: Infer doesn't go looking into field values when looking for unsigned expressions, which could cause some unintended reports. Reviewed By: sblackshear Differential Revision: D3724232 fbshipit-source-id: 9c4cd97master
parent
7de52e7649
commit
26a6594b90
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// integers
|
||||
|
||||
unsigned int returnUnsigned();
|
||||
void nonnegative_int() {
|
||||
unsigned int x = returnUnsigned();
|
||||
if (x < 0) {
|
||||
int y = x / 0; // shouldn't report
|
||||
}
|
||||
}
|
||||
|
||||
int returnSigned();
|
||||
void signed_int() {
|
||||
int x = returnSigned();
|
||||
if (x < 0) {
|
||||
int y = 1 / 0; // should report
|
||||
}
|
||||
}
|
||||
|
||||
// pointers to integers
|
||||
|
||||
unsigned int* returnUnsignedPointer();
|
||||
void nonnegative_int_ptr() {
|
||||
unsigned int* x = returnUnsignedPointer();
|
||||
if (*x < 0) {
|
||||
int y = 1 / 0; // shouldn't report
|
||||
}
|
||||
}
|
||||
|
||||
int* returnSignedPointer();
|
||||
void signed_int_ptr() {
|
||||
int* x = returnSigned();
|
||||
if (*x < 0) {
|
||||
int y = 1 / 0; // should report
|
||||
}
|
||||
}
|
||||
|
||||
// struct with integer fields
|
||||
|
||||
struct foo {
|
||||
unsigned int unsigned_int;
|
||||
int signed_int;
|
||||
};
|
||||
|
||||
struct foo* returnFoo();
|
||||
|
||||
void nonnegative_field() {
|
||||
struct foo* x = returnFoo();
|
||||
if (x->unsigned_int < 0) {
|
||||
int y = 1 / 0; // shouldn't report
|
||||
}
|
||||
}
|
||||
|
||||
void signed_field() {
|
||||
struct foo* x = returnFoo();
|
||||
if (x->signed_int < 0) {
|
||||
int y = 1 / 0; // should report
|
||||
}
|
||||
}
|
||||
|
||||
// array of integers
|
||||
|
||||
unsigned int* returnUnsignedArray();
|
||||
int nonnegative_array() {
|
||||
unsigned int* a = returnUnsignedArray();
|
||||
if (a[0] < 0) {
|
||||
int y = 1 / 0; // shouldn't report
|
||||
}
|
||||
}
|
||||
|
||||
int* returnSignedArray();
|
||||
int signed_array() {
|
||||
int* a = returnSignedArray();
|
||||
if (a[0] < 0) {
|
||||
int y = 1 / 0; // should report
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.c.infer;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static utils.matchers.ResultContainsExactly.containsExactly;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import utils.InferException;
|
||||
import utils.InferResults;
|
||||
|
||||
public class UnsignedIsNonnegativeTest {
|
||||
|
||||
public static final String SOURCE_FILE = "arithmetic/unsigned_values.c";
|
||||
|
||||
public static final String DIVIDE_BY_ZERO = "DIVIDE_BY_ZERO";
|
||||
|
||||
private static InferResults inferResults;
|
||||
|
||||
@BeforeClass
|
||||
public static void runInfer() throws InterruptedException, IOException {
|
||||
inferResults = InferResults.loadCInferResults(DivideByZeroTest.class, SOURCE_FILE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInferRunsOnDivideByZeroThenDivideByZeroIsFound()
|
||||
throws InterruptedException, IOException, InferException {
|
||||
String[] procedures = {"signed_int", "signed_int_ptr", "signed_field", "signed_array"};
|
||||
assertThat(
|
||||
"Results should contain divide by zero error",
|
||||
inferResults,
|
||||
containsExactly(
|
||||
DIVIDE_BY_ZERO,
|
||||
SOURCE_FILE,
|
||||
procedures
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue