2022-03-22 21:45:24 -07:00
|
|
|
// RUN: %clang_analyze_cc1 -verify %s \
|
2023-06-01 09:20:36 +02:00
|
|
|
// RUN: -analyzer-checker=core,alpha.unix.StdCLibraryFunctions \
|
2023-07-18 08:47:05 +02:00
|
|
|
// RUN: -analyzer-config alpha.unix.StdCLibraryFunctions:ModelPOSIX=true \
|
2022-03-22 21:45:24 -07:00
|
|
|
// RUN: -analyzer-output=text
|
|
|
|
|
2023-07-18 08:47:05 +02:00
|
|
|
#include "Inputs/std-c-library-functions-POSIX.h"
|
2022-03-22 21:45:24 -07:00
|
|
|
#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}}
|
|
|
|
}
|
|
|
|
|
2023-07-18 08:47:05 +02:00
|
|
|
int test_isalpha(int *x, char c, char d) {
|
|
|
|
int iad = isalpha(d);// \
|
|
|
|
// expected-note{{Assuming the character is non-alphabetical}}
|
2022-03-22 21:45:24 -07:00
|
|
|
if (isalpha(c)) {// \
|
2023-07-18 08:47:05 +02:00
|
|
|
// expected-note{{Taking true branch}} \
|
|
|
|
// expected-note{{Assuming the character is alphabetical}}
|
2022-03-22 21:45:24 -07:00
|
|
|
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)) {// \
|
2023-07-18 08:47:05 +02:00
|
|
|
// expected-note{{Assuming the character is not a digit}} \
|
|
|
|
// expected-note{{Taking true branch}}
|
2022-03-22 21:45:24 -07:00
|
|
|
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')}}
|
|
|
|
}
|
2023-07-18 08:47:05 +02:00
|
|
|
|
|
|
|
int test_bugpath_notes(FILE *f1, char c, FILE *f2) {
|
|
|
|
int f = fileno(f2); // \
|
|
|
|
// expected-note{{Assuming that 'fileno' is successful}}
|
|
|
|
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}}
|
|
|
|
}
|