aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/fd-1.c
blob: 5b85a3316e832864ac9c8d6884b0e1a609b13e55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
int open(const char *, int mode);
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2

typedef enum {
  S_IRWXU
  // etc
} mode_t;

int creat (const char *, mode_t mode);

void
test_1 (const char *path)
{
  int fd = open (path, O_RDONLY); /* { dg-message "\\(1\\) opened here" } */
  return; /* { dg-warning "leak of file descriptor 'fd' \\\[CWE-775\\\]" "warning" } */
 /* { dg-message "\\(2\\) 'fd' leaks here; was opened at \\(1\\)" "event" { target *-*-* } .-1 } */
}   

void
test_2 (const char *path)
{
  int fd = open (path, O_RDWR); /* { dg-message "\\(1\\) opened here" } */
  if (fd >= 0) /* { dg-message "\\(2\\) assuming 'fd' is a valid file descriptor" "event1" } */
  /* { dg-message "\\(3\\) following 'true' branch \\(when 'fd >= 0'\\)..." "event2" { target *-*-* } .-1 } */
  {
    return; /* { dg-warning "leak of file descriptor 'fd' \\\[CWE-775\\\]" "warning" } */
    /* { dg-message "\\(4\\) ...to here" "event1" { target *-*-* } .-1 } */
    /* { dg-message "\\(5\\) 'fd' leaks here; was opened at \\(1\\)" "event2" { target *-*-* } .-2 } */
  } 
}

void
test_3 (const char *path)
{
  int fd = open (path, O_WRONLY); /* { dg-message "\\(1\\) opened here" } */
  return; /* { dg-warning "leak of file descriptor 'fd' \\\[CWE-775\\\]" "warning" } */
}

void test_4 (const char *path)
{
  open(path, O_RDONLY); /* { dg-warning "leak of file descriptor \\\[CWE-775\\\]" } */
  /* { dg-message "\\(1\\) leaks here" "" { target *-*-* } .-1 } */
}

void 
test_5 (const char *path, mode_t mode)
{
  creat (path, mode); /* { dg-warning "leak of file descriptor \\\[CWE-775\\\]" } */
}

void
test_6 (const char *path, mode_t mode)
{
  int fd = creat (path, mode);
  return; /* { dg-warning "leak of file descriptor 'fd' \\\[CWE-775\\\]" } */
}