Reviewed By: akotulski Differential Revision: D3264368 fb-gh-sync-id: 1f79e19 fbshipit-source-id: 1f79e19master
							parent
							
								
									a352c0ffa8
								
							
						
					
					
						commit
						c3fbd5af29
					
				@ -0,0 +1,18 @@
 | 
				
			||||
/*
 | 
				
			||||
 * 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 <assert.h>
 | 
				
			||||
 | 
				
			||||
// We should report here no NPE, but also we should report div0 to show that we
 | 
				
			||||
// get the
 | 
				
			||||
// non-assertion branch.
 | 
				
			||||
int report_div0_and_no_npe(int* p) {
 | 
				
			||||
  assert(p);
 | 
				
			||||
  return *p / 0;
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,58 @@
 | 
				
			||||
/*
 | 
				
			||||
 * 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.
 | 
				
			||||
 */
 | 
				
			||||
 | 
				
			||||
/*WARNING: This is not being tested in the endtoend tests because it requires
 | 
				
			||||
 glog library.
 | 
				
			||||
 Only to be run manually */
 | 
				
			||||
 | 
				
			||||
#include <glog/logging.h>
 | 
				
			||||
 | 
				
			||||
int log_fatal_example() {
 | 
				
			||||
  int* p = nullptr;
 | 
				
			||||
  if (p == nullptr) {
 | 
				
			||||
    LOG(FATAL) << "this will crash\n";
 | 
				
			||||
  }
 | 
				
			||||
  return *p; // program should never get here
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
int check_example(int* a) {
 | 
				
			||||
  CHECK(a);
 | 
				
			||||
  return *a; // no null deref flagged by Infer
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
int log_non_fatal_example() {
 | 
				
			||||
  int* a = nullptr;
 | 
				
			||||
  LOG_IF(INFO, a) << "well\n";
 | 
				
			||||
  LOG_IF(WARNING, a) << "well\n";
 | 
				
			||||
  LOG_IF(ERROR, a) << "well\n";
 | 
				
			||||
  return *a; // null deref flagged by Infer
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
int log_if_fatal_example() {
 | 
				
			||||
  int* a = nullptr;
 | 
				
			||||
  LOG_IF(FATAL, !a) << "well\n";
 | 
				
			||||
  return *a; // no null deref
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
// Still not modelled
 | 
				
			||||
 | 
				
			||||
int check_ne_example(int x) {
 | 
				
			||||
  CHECK_NE(x, 5);
 | 
				
			||||
  return x;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
int check_eq_example(int x, int y) {
 | 
				
			||||
  CHECK_EQ(x, y);
 | 
				
			||||
  return x;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
int check_not_null_example(int* p) {
 | 
				
			||||
  CHECK_NOTNULL(p);
 | 
				
			||||
  return *p;
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,61 @@
 | 
				
			||||
/*
 | 
				
			||||
 * 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.c;
 | 
				
			||||
 | 
				
			||||
import static org.hamcrest.MatcherAssert.assertThat;
 | 
				
			||||
import static utils.matchers.ResultContainsExactly.containsExactly;
 | 
				
			||||
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 AssertKeepBranchTest {
 | 
				
			||||
 | 
				
			||||
  public static final String source_file = "assertions/assertion_example.c";
 | 
				
			||||
 | 
				
			||||
  private static ImmutableList<String> inferCmd;
 | 
				
			||||
 | 
				
			||||
  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(AssertKeepBranchTest.class, source_file);
 | 
				
			||||
  }
 | 
				
			||||
 | 
				
			||||
  @Test
 | 
				
			||||
  public void whenRunsOnAssertionExampleThenDiv0AndNoNPEIsFound()
 | 
				
			||||
      throws InterruptedException, IOException, InferException {
 | 
				
			||||
    String[] methods = {
 | 
				
			||||
        "report_div0_and_no_npe",
 | 
				
			||||
    };
 | 
				
			||||
    assertThat(
 | 
				
			||||
        "Results should contain " + DIVIDE_BY_ZERO,
 | 
				
			||||
        inferResults,
 | 
				
			||||
        containsExactly(
 | 
				
			||||
            DIVIDE_BY_ZERO,
 | 
				
			||||
            source_file,
 | 
				
			||||
            methods
 | 
				
			||||
        )
 | 
				
			||||
    );
 | 
				
			||||
  }
 | 
				
			||||
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,41 @@
 | 
				
			||||
/*
 | 
				
			||||
 * 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 frontend.c;
 | 
				
			||||
 | 
				
			||||
import org.junit.Ignore;
 | 
				
			||||
import org.junit.Rule;
 | 
				
			||||
import org.junit.Test;
 | 
				
			||||
 | 
				
			||||
import java.io.IOException;
 | 
				
			||||
 | 
				
			||||
import utils.DebuggableTemporaryFolder;
 | 
				
			||||
import utils.InferException;
 | 
				
			||||
import utils.ClangFrontendUtils;
 | 
				
			||||
 | 
				
			||||
public class AssertionTest {
 | 
				
			||||
 | 
				
			||||
  String conditionalOperatorBasePath = "infer/tests/codetoanalyze/c/frontend/assertions/";
 | 
				
			||||
 | 
				
			||||
  @Rule
 | 
				
			||||
  public DebuggableTemporaryFolder folder = new DebuggableTemporaryFolder();
 | 
				
			||||
 | 
				
			||||
  void frontendTest(String fileRelative) throws InterruptedException, IOException, InferException {
 | 
				
			||||
    ClangFrontendUtils.createAndCompareCDotFiles(
 | 
				
			||||
        folder,
 | 
				
			||||
        conditionalOperatorBasePath + fileRelative);
 | 
				
			||||
  }
 | 
				
			||||
 | 
				
			||||
  @Ignore @Test
 | 
				
			||||
  public void whenCaptureRunOnAssertExampleThenDotFilesAreTheSame()
 | 
				
			||||
      throws InterruptedException, IOException, InferException {
 | 
				
			||||
    frontendTest("assert_example.c");
 | 
				
			||||
  }
 | 
				
			||||
 | 
				
			||||
}
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue