Alpha Checkers
Experimental checkers in addition to the Default Checkers. These are checkers with known issues or limitations that keep them from being on by default. They are likely to have false positives. Bug reports are welcome but will likely not be investigated for some time. Patches welcome!- Clone Alpha Checkers
- Core Alpha Checkers
- C++ Alpha Checkers
- LLVM Checkers
- Variable Argument Alpha Checkers
- Dead Code Alpha Checkers
- OS X Alpha Checkers
- Security Alpha Checkers
- Unix Alpha Checkers
- Non-determinism Alpha Checkers
Clone Alpha Checkers
| Name, Description | Example |
alpha.clone.CloneChecker
(C, C++, ObjC)
Reports similar pieces of code. |
void log();
int max(int a, int b) { // warn
log();
if (a > b)
return a;
return b;
}
int maxClone(int x, int y) { // similar code here
log();
if (x > y)
return x;
return y;
}
|
Core Alpha Checkers
C++ Alpha Checkers
| Name, Description | Example |
alpha.cplusplus.DeleteWithNonVirtualDtor
(C++)
Reports destructions of polymorphic objects with a non-virtual destructor in
their base class
|
NonVirtual *create() {
NonVirtual *x = new NVDerived(); // note: Casting from 'NVDerived' to
// 'NonVirtual' here
return x;
}
void sink(NonVirtual *x) {
delete x; // warn: destruction of a polymorphic object with no virtual
// destructor
}
|
alpha.cplusplus.InvalidatedIterator
(C++)
Check for use of invalidated iterators.
|
void bad_copy_assign_operator_list1(std::list |
alpha.cplusplus.IteratorRange
(C++)
Check for iterators used outside their valid ranges.
|
void simple_bad_end(const std::vector |
alpha.cplusplus.MismatchedIterator
(C++)
Check for use of iterators of different containers where iterators of the same
container are expected.
|
void bad_insert3(std::vector |
alpha.cplusplus.Move
(C++)
Method calls on a moved-from object and copying a moved-from object will be
reported.
|
struct A {
void foo() {}
};
void f() {
A a;
A b = std::move(a); // note: 'a' became 'moved-from' here
a.foo(); // warn: method call on a 'moved-from' object 'a'
}
|
Dead Code Alpha Checkers
| Name, Description | Example |
alpha.deadcode.UnreachableCode
(C, C++, ObjC)
Check unreachable code. |
// C
int test() {
int x = 1;
while(x);
return x; // warn
}
// C++
void test() {
int a = 2;
while (a > 1)
a--;
if (a > 1)
a++; // warn
}
// Objective-C
void test(id x) {
return;
[x retain]; // warn
}
|
LLVM Checkers
OS X Alpha Checkers
| Name, Description | Example |
alpha.osx.cocoa.DirectIvarAssignment
(ObjC)
Check that Objective C properties follow the following rule: the property
should be set with the setter, not though a direct assignment. |
@interface MyClass : NSObject {}
@property (readonly) id A;
- (void) foo;
@end
@implementation MyClass
- (void) foo {
_A = 0; // warn
}
@end
|
alpha.osx.cocoa.DirectIvarAssignmentForAnnotatedFunctions
(ObjC)
Check for direct assignments to instance variables in the methods annotated
with objc_no_direct_instance_variable_assignment. |
@interface MyClass : NSObject {}
@property (readonly) id A;
- (void) fAnnotated __attribute__((
annotate("objc_no_direct_instance_variable_assignment")));
- (void) fNotAnnotated;
@end
@implementation MyClass
- (void) fAnnotated {
_A = 0; // warn
}
- (void) fNotAnnotated {
_A = 0; // no warn
}
@end
|
alpha.osx.cocoa.InstanceVariableInvalidation
(ObjC)
Check that the invalidatable instance variables are invalidated in the methods
annotated with objc_instance_variable_invalidator. |
@protocol Invalidation <NSObject>
- (void) invalidate
__attribute__((annotate("objc_instance_variable_invalidator")));
@end
@interface InvalidationImpObj : NSObject <Invalidation>
@end
@interface SubclassInvalidationImpObj : InvalidationImpObj {
InvalidationImpObj *var;
}
- (void)invalidate;
@end
@implementation SubclassInvalidationImpObj
- (void) invalidate {}
@end
// warn: var needs to be invalidated or set to nil
|
alpha.osx.cocoa.MissingInvalidationMethod
(ObjC)
Check that the invalidation methods are present in classes that contain
invalidatable instance variables. |
@protocol Invalidation <NSObject>
- (void)invalidate
__attribute__((annotate("objc_instance_variable_invalidator")));
@end
@interface NeedInvalidation : NSObject <Invalidation>
@end
@interface MissingInvalidationMethodDecl : NSObject {
NeedInvalidation *Var; // warn
}
@end
@implementation MissingInvalidationMethodDecl
@end
|
alpha.osx.cocoa.localizability.PluralMisuseChecker
(ObjC)
Warns against using one vs. many plural pattern in code
when generating localized strings.
|
NSString *reminderText =
NSLocalizedString(@"None", @"Indicates no reminders");
if (reminderCount == 1) {
// Warning: Plural cases are not supported across all languages.
// Use a .stringsdict file instead
reminderText =
NSLocalizedString(@"1 Reminder", @"Indicates single reminder");
} else if (reminderCount >= 2) {
// Warning: Plural cases are not supported across all languages.
// Use a .stringsdict file instead
reminderText =
[NSString stringWithFormat:
NSLocalizedString(@"%@ Reminders", @"Indicates multiple reminders"),
reminderCount];
}
|
Security Alpha Checkers
| Name, Description | Example |
alpha.security.ArrayBound
(C)
Warn about buffer overflows (older checker). |
void test() {
char *s = "";
char c = s[1]; // warn
}
struct seven_words {
int c[7];
};
void test() {
struct seven_words a, *p;
p = &a;
p[0] = a;
p[1] = a;
p[2] = a; // warn
}
// note: requires unix.Malloc or
// alpha.unix.MallocWithAnnotations checks enabled.
void test() {
int *p = malloc(12);
p[3] = 4; // warn
}
void test() {
char a[2];
int *b = (int*)a;
b[1] = 3; // warn
}
|
alpha.security.ArrayBoundV2
(C)
Warn about buffer overflows (newer checker). |
void test() {
char *s = "";
char c = s[1]; // warn
}
void test() {
int buf[100];
int *p = buf;
p = p + 99;
p[1] = 1; // warn
}
// note: compiler has internal check for this.
// Use -Wno-array-bounds to suppress compiler warning.
void test() {
int buf[100][100];
buf[0][-1] = 1; // warn
}
// note: requires alpha.security.taint check turned on.
void test() {
char s[] = "abc";
int x = getchar();
char c = s[x]; // warn: index is tainted
}
|
alpha.security.MallocOverflow
(C)
Check for overflows in the arguments to malloc(). |
void test(int n) {
void *p = malloc(n * sizeof(int)); // warn
}
|
alpha.security.MmapWriteExec
(C)
Warn on mmap() |
void test(int n) {
void *c = mmap(NULL, 32, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON, -1, 0);
// warn: Both PROT_WRITE and PROT_EXEC flags are set. This can lead to
// exploitable memory regions, which could be overwritten with malicious
// code
}
|
alpha.security.ReturnPtrRange
(C)
Check for an out-of-bound pointer being returned to callers. |
static int A[10];
int *test() {
int *p = A + 10;
return p; // warn
}
int test(void) {
int x;
return x; // warn: undefined or garbage returned
}
|
alpha.security.taint.TaintPropagation
(C)
Generate taint information used by other checkers. |
void test() {
char x = getchar(); // 'x' marked as tainted
system(&x); // warn: untrusted data is passed to a system call
}
// note: compiler internally checks if the second param to
// sprintf is a string literal or not.
// Use -Wno-format-security to suppress compiler warning.
void test() {
char s[10], buf[10];
fscanf(stdin, "%s", s); // 's' marked as tainted
sprintf(buf, s); // warn: untrusted data as a format string
}
void test() {
size_t ts;
scanf("%zd", &ts); // 'ts' marked as tainted
int *p = (int *)malloc(ts * sizeof(int));
// warn: untrusted data as buffer size
}
|
Unix Alpha Checkers
Non-determinism Alpha Checkers
| Name, Description | Example |
alpha.nondeterminism.PointerIteration
(C++)
Check for non-determinism caused by iterating unordered containers of pointers. |
// C++
void test() {
int a = 1, b = 2;
std::unordered_set |
alpha.nondeterminism.PointerSorting
(C++)
Check for non-determinism caused by sorting of pointers. |
// C++
void test() {
int a = 1, b = 2;
std::vector |