Skip to content

Commit 3ce393f

Browse files
committed
Fixed model for getc and fgetc
1 parent 044df14 commit 3ce393f

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed

infer/models/c/src/libc_basic.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,16 @@ int fputs(const char *str, FILE *stream) {
702702
// return a nondeterministic value
703703
int getc(FILE *stream)
704704
{
705+
FILE tmp;
706+
tmp = *stream;
705707
return __infer_nondet_int();
706708
}
707709

708710
// return a nondeterministic value
709711
int fgetc(FILE *stream)
710712
{
713+
FILE tmp;
714+
tmp = *stream;
711715
return __infer_nondet_int();
712716
}
713717

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
6+
void crashgetc() {
7+
8+
9+
FILE *f;
10+
int i;
11+
12+
f=fopen("this_file_doesnt_exists", "r");
13+
i =getc(f);
14+
printf("i =%i\n", i);
15+
fclose(f);
16+
}
17+
18+
void nocrashgetc() {
19+
20+
21+
FILE *f;
22+
int i;
23+
24+
f=fopen("this_file_doesnt_exists", "r");
25+
26+
27+
if (f) {
28+
i =getc(f);
29+
printf("i =%i\n", i);
30+
fclose(f);
31+
}
32+
}
33+
34+
void crashfgetc() {
35+
36+
FILE *f;
37+
int i;
38+
39+
f=fopen("this_file_doesnt_exists", "r");
40+
i =fgetc(f);
41+
printf("i =%i\n", i);
42+
fclose(f);
43+
}
44+
45+
void nocrashfgetc() {
46+
47+
FILE *f;
48+
int i;
49+
50+
f=fopen("this_file_doesnt_exists", "r");
51+
if (f) {
52+
i =fgetc(f);
53+
printf("i =%i\n", i);
54+
fclose(f);
55+
}
56+
}
57+
58+

infer/tests/endtoend/c/NullDereferenceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class NullDereferenceTest {
2222
public static final String SOURCE_FILE =
2323
"null_dereference/null_pointer_dereference.c";
2424

25+
2526
public static final String NULL_DEREFERENCE = "NULL_DEREFERENCE";
2627

2728
private static InferResults inferResults;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2013- Facebook.
3+
* All rights reserved.
4+
*/
5+
6+
package endtoend.c;
7+
8+
import static org.hamcrest.MatcherAssert.assertThat;
9+
import static utils.matchers.ResultContainsErrorInMethod.contains;
10+
11+
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
import java.io.IOException;
16+
17+
import utils.InferException;
18+
import utils.InferResults;
19+
20+
public class NullDereferenceTest2 {
21+
22+
public static final String SOURCE_FILE =
23+
"null_dereference/get.c";
24+
25+
26+
public static final String NULL_DEREFERENCE = "NULL_DEREFERENCE";
27+
28+
private static InferResults inferResults;
29+
30+
@BeforeClass
31+
public static void runInfer() throws InterruptedException, IOException {
32+
inferResults = InferResults.loadCInferResults(
33+
NullDereferenceTest2.class,
34+
SOURCE_FILE);
35+
}
36+
37+
/*
38+
@Test
39+
public void nullDereferenceTest2() throws InterruptedException, IOException, InferException {
40+
assertThat(
41+
"Results should contain null pointer dereference error",
42+
inferResults,
43+
contains(
44+
NULL_DEREFERENCE,
45+
SOURCE_FILE,
46+
"crashgetc"
47+
)
48+
);
49+
}
50+
51+
@Test
52+
public void nullDereferenceTest2_fgetc() throws InterruptedException, IOException, InferException {
53+
assertThat(
54+
"Results should contain null pointer dereference error",
55+
inferResults,
56+
contains(
57+
NULL_DEREFERENCE,
58+
SOURCE_FILE,
59+
"crashfgetc"
60+
)
61+
);
62+
}
63+
*/
64+
}

0 commit comments

Comments
 (0)