[infer][clang] fix model of sockets

master
Jules Villard 9 years ago
parent b328ee1509
commit 768a850c58

@ -628,7 +628,7 @@ int open(const char *path, int oflag, ...) {
if(ret) { if(ret) {
__set_file_attribute(ret); __set_file_attribute(ret);
INFER_EXCLUDE_CONDITION(ret < (int *)1); // force result to be > 0 INFER_EXCLUDE_CONDITION(ret < (int *)1); // force result to be > 0
return (long) ret; return ret;
} }
return -1; return -1;
} }
@ -1881,8 +1881,8 @@ int socket (int namespace, int style, int protocol) {
int *ret = malloc(sizeof(int)); int *ret = malloc(sizeof(int));
if(ret) { if(ret) {
__set_file_attribute(ret); __set_file_attribute(ret);
INFER_EXCLUDE_CONDITION(ret < (int *)1); // force result to be >= 0 INFER_EXCLUDE_CONDITION(ret < (int *)1); // force result to be > 0
return (long) (ret-1); return ret;
} }
return -1; return -1;
} }

@ -17,6 +17,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
@ -40,3 +41,45 @@ void fileClosed()
close(fd); close(fd);
} }
} }
void socketNotClosed()
{
int fd = socket(AF_LOCAL, SOCK_RAW, 0);
if (fd != -1) {
char buffer[256];
// We can easily batch that by separating with space
write(fd, buffer, strlen(buffer));
}
}
int socketClosed()
{
int socketFD = socket(AF_LOCAL, SOCK_RAW, 0);
if (socketFD == -1) {
return -1;
}
int status;
status = fcntl(socketFD, F_SETFL, O_NONBLOCK);
if (status == -1) {
close(socketFD);
return -1;
}
int reuseaddr = 1;
status = setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
if (status == -1) {
close(socketFD);
return -1;
}
int nosigpipe = 1;
status = setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &nosigpipe, sizeof(nosigpipe));
if (status == -1) {
close(socketFD);
return -1;
}
return socketFD;
}

@ -10,8 +10,7 @@
package endtoend.c; package endtoend.c;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static utils.matchers.ResultContainsErrorInMethod.contains; import static utils.matchers.ResultContainsExactly.containsExactly;
import static utils.matchers.ResultContainsNoErrorInMethod.doesNotContain;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -36,29 +35,22 @@ public class ResourceLeakTest {
} }
@Test @Test
public void whenInferRunsOnFileNotClosedThenLeakFound() public void mathErrors()
throws InterruptedException, IOException, InferException { throws InterruptedException, IOException, InferException {
String[] functions = {
"fileNotClosed",
"socketNotClosed"
};
assertThat( assertThat(
"Results should not contain a resource leak", "Results should contain " + RESOURCE_LEAK,
inferResults, inferResults,
contains( containsExactly(
RESOURCE_LEAK, RESOURCE_LEAK,
SOURCE_FILE, SOURCE_FILE,
"fileNotClosed" functions
) )
); );
} }
@Test
public void whenInferRunsOnFileClosedThenLeakNotFound()
throws InterruptedException, IOException, InferException {
assertThat(
"Results should not contain a resource leak",
inferResults,
doesNotContain(
RESOURCE_LEAK,
SOURCE_FILE,
"fileClosed"));
}
} }

Loading…
Cancel
Save