[llvm-bugs] [Bug 25279] New: warn when capturing an __autoreleasing object in a block

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 21 17:21:59 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=25279

            Bug ID: 25279
           Summary: warn when capturing an __autoreleasing object in a
                    block
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Static Analyzer
          Assignee: kremenek at apple.com
          Reporter: scarff at gmail.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

In Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn), and
possibly other versions, there is no warning generated when capturing an
__autoreleasing object in a block, even though that is ill-formed.

The following compiles without generating any relevant warnings, but can crash
at runtime, either in objc_msgSend on the dealloc'd object, or in
objc_storeStrong/objc_retain when copying the __autoreleasing temporary to the
__strong as part of the implicit writeback logic in main().

/*
 clang -x objective-c -fobjc-arc -fmodules -framework Foundation -Weverything
-O -g test.m && lldb ./a.out
 run
 */
@import Foundation;

// Implicitly, the parameter is NSError *__autoreleasing *error:
//
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#indirect-parameters
BOOL PerformOperationInNestedBlockWithError(NSError **error) {
  NSDictionary *dict = @{ @"a" : @"foo", @"b" : @"bar" };
  [dict enumerateKeysAndObjectsUsingBlock:^(id key, id val, BOOL *stop) {
    // Capturing |error| here is ill-formed:
    //
http://clang.llvm.org/docs/AutomaticReferenceCounting.html#storage-duration-of-autoreleasing-objects
    *error = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSFeatureUnsupportedError userInfo:nil];
    *stop = YES;
  }];
  return NO;
}

int
main(int argc, char **argv) {
  NSError *error;
  if (!PerformOperationInNestedBlockWithError(&error)) {
    NSLog(@"error: %@", error);
  }
  return 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20151022/62d412c7/attachment.html>


More information about the llvm-bugs mailing list