File: std-c-library-functions-path-notes.c

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (82 lines) | stat: -rw-r--r-- 2,732 bytes parent folder | download | duplicates (2)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// RUN: %clang_analyze_cc1 -verify %s \
// RUN:     -analyzer-checker=core,alpha.unix.StdCLibraryFunctions \
// RUN:     -analyzer-config alpha.unix.StdCLibraryFunctions:ModelPOSIX=true \
// RUN:     -analyzer-output=text

#include "Inputs/std-c-library-functions-POSIX.h"
#define NULL ((void *)0)

char *getenv(const char *);
int isalpha(int);
int isdigit(int);
int islower(int);

char test_getenv() {
  char *env = getenv("VAR"); // \
  // expected-note{{Assuming the environment variable does not exist}} \
  // expected-note{{'env' initialized here}}

  return env[0]; // \
  // expected-warning{{Array access (from variable 'env') results in a null pointer dereference}} \
  // expected-note   {{Array access (from variable 'env') results in a null pointer dereference}}
}

int test_isalpha(int *x, char c, char d) {
  int iad = isalpha(d);
  if (isalpha(c)) {// \
    // expected-note{{Taking true branch}}
    x = NULL; // \
    // expected-note{{Null pointer value stored to 'x'}}
  }

  return *x; // \
  // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} \
  // expected-note   {{Dereference of null pointer (loaded from variable 'x')}}
}

int test_isdigit(int *x, char c) {
  if (!isdigit(c)) {// \
  // expected-note{{Taking true branch}}
    x = NULL; // \
    // expected-note{{Null pointer value stored to 'x'}}
  }

  return *x; // \
  // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} \
  // expected-note   {{Dereference of null pointer (loaded from variable 'x')}}
}

int test_islower(int *x) {
  char c = 'c';
  // No "Assuming..." note. We aren't assuming anything. We *know*.
  if (islower(c)) { // \
    // expected-note{{Taking true branch}}
    x = NULL; // \
    // expected-note{{Null pointer value stored to 'x'}}
  }

  return *x; // \
  // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} \
  // expected-note   {{Dereference of null pointer (loaded from variable 'x')}}
}

int test_bugpath_notes(FILE *f1, char c, FILE *f2) {
  int f = fileno(f2);
  if (f == -1) // \
    // expected-note{{Taking false branch}}
    return 0;
  int l = islower(c);
  f = fileno(f1); // \
  // expected-note{{Value assigned to 'f'}} \
  // expected-note{{Assuming that 'fileno' fails}}
  return dup(f); // \
  // expected-warning{{The 1st argument to 'dup' is -1 but should be >= 0}} \
  // expected-note{{The 1st argument to 'dup' is -1 but should be >= 0}}
}

int test_fileno_arg_note(FILE *f1) {
  return dup(fileno(f1)); // \
  // expected-warning{{The 1st argument to 'dup' is < 0 but should be >= 0}} \
  // expected-note{{The 1st argument to 'dup' is < 0 but should be >= 0}} \
  // expected-note{{Assuming that 'fileno' fails}}
}