Skip to content

silent miscompilation of code sending objc messages to pointers and ints #4138

@llvmbot

Description

@llvmbot
Bugzilla Link 3766
Resolution FIXED
Resolved on Feb 22, 2010 12:54
Version unspecified
OS MacOS X
Reporter LLVM Bugzilla Contributor
CC @tkremenek

Extended Description

clang version: 0.169

The following example generates a 'Branch condition evaluates to an uninitialized value' false positive:

[johne@MacBook] checker-0.169% cat ../bug2.m #import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSGarbageCollector.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#include <stdio.h>
//#include <objc/runtime.h> // <-- Uncomment to make the false positive go away.

BOOL collectingEnabled (void) {
BOOL gcEnabled = ([objc_getClass("NSGarbageCollector") defaultCollector] != NULL) ? YES : NO;
if(gcEnabled == YES) { // <-- False positive here.
printf("GC is enabled.\n");
}
return(gcEnabled);
}

int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL gcEnabled = collectingEnabled();

printf("GC enabled: %d\n", gcEnabled);

[pool release];
pool = NULL;

return(0);
}

[johne@MacBook] checker-0.169% ./scan-build gcc-4.2 -std=gnu99 -c -g -pedantic -Wall -Wextra -Wmissing-prototypes -Wredundant-decls ../bug2.m
../bug2.m:1:2: warning: #import is a GCC extension
../bug2.m:2:2: warning: #import is a GCC extension
../bug2.m:3:2: warning: #import is a GCC extension
../bug2.m:4:2: warning: #import is a GCC extension
../bug2.m:8: warning: no previous prototype for 'collectingEnabled'
../bug2.m:16: warning: unused parameter 'argc'
../bug2.m:16: warning: unused parameter 'argv'
../bug2.m:9:21: warning: bad receiver type 'int'
BOOL gcEnabled = ([objc_getClass("NSGarbageCollector") defaultCollector] != NULL) ? YES : NO;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ANALYZE: ../bug2.m collectingEnabled
../bug2.m:10:3: warning: Branch condition evaluates to an uninitialized value.
if(gcEnabled == YES) { // <-- False positive here.
^ ~~~~~~~~~
ANALYZE: ../bug2.m main
2 diagnostics generated.
scan-build: 1 bugs found.
scan-build: Run 'scan-view /var/folders/Ee/Eeuv+JUYEF4h5NFY82FFvE+++TI/-Tmp-/scan-build-2009-03-09-15' to examine bug reports.

Rerun with #include <objc/runtime.h> uncommented:

[johne@MacBook] checker-0.169% ./scan-build gcc-4.2 -std=gnu99 -c -g -pedantic -Wall -Wextra -Wmissing-prototypes -Wredundant-decls ../bug2.m
../bug2.m:1:2: warning: #import is a GCC extension
../bug2.m:2:2: warning: #import is a GCC extension
../bug2.m:3:2: warning: #import is a GCC extension
../bug2.m:4:2: warning: #import is a GCC extension
../bug2.m:8: warning: no previous prototype for 'collectingEnabled'
../bug2.m:16: warning: unused parameter 'argc'
../bug2.m:16: warning: unused parameter 'argv'
ANALYZE: ../bug2.m collectingEnabled
ANALYZE: ../bug2.m main
scan-build: Removing directory '/var/folders/Ee/Eeuv+JUYEF4h5NFY82FFvE+++TI/-Tmp-/scan-build-2009-03-09-15' because it contains no reports.

If I had to guess, this is probably because the compiler/analyzer is not agnostic about objc_getClass(). That is to say that it is not relying on the headers to properly declare objc_getClass(), but instead has a hidden and internal prototype built directly in to the compiler.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions