ARC + goto into protected scope error

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ARC + goto into protected scope error

Dave Keck
Hi all,

I use the "check for success/goto failed" pattern everywhere, but
Clang refuses to compile such code with ARC enabled if the goto
bypasses a variable declaration (giving a "goto into protected scope"
error.)

Is this something that's likely to be fixed? Compiling the program
below triggers the error.

    // compile with: clang -framework Foundation -fobjc-arc <file>.m
    #import <Foundation/Foundation.h>
    int main() {
        goto cleanup;
        NSString *str;
        cleanup:
        return 0;
    }
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: ARC + goto into protected scope error

Jordan Rose
If you put the variable in its own scope, preventing accidental access, you won't get the error:

int main() {
  goto cleanup;
  {
     NSString *str;
  }
cleanup:
  return 0;
}

If you don't do this, you bypass the zero-initialization of 'str', and when ARC tries to clean it up, it'll end up trying to release some random bit of memory.

This should handle any valid case; if you want to use 'goto' but can't enclose the variables in their own scope, you'll have to move the declarations before the goto (as K&R intended *grin*).

Jordan


On Aug 16, 2012, at 18:47 , Dave Keck <[hidden email]> wrote:

> Hi all,
>
> I use the "check for success/goto failed" pattern everywhere, but
> Clang refuses to compile such code with ARC enabled if the goto
> bypasses a variable declaration (giving a "goto into protected scope"
> error.)
>
> Is this something that's likely to be fixed? Compiling the program
> below triggers the error.
>
>    // compile with: clang -framework Foundation -fobjc-arc <file>.m
>    #import <Foundation/Foundation.h>
>    int main() {
>        goto cleanup;
>        NSString *str;
>        cleanup:
>        return 0;
>    }
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: ARC + goto into protected scope error

Jordan Rose
Note that (IIRC) goto can /leave/ a protected scope, so you can also just put one set of braces around the /rest/ of your function, and only leave the cleanup code outside. You won't need to nest new braces after every goto.


On Aug 16, 2012, at 18:56 , Jordan Rose <[hidden email]> wrote:

> If you put the variable in its own scope, preventing accidental access, you won't get the error:
>
> int main() {
>  goto cleanup;
>  {
>     NSString *str;
>  }
> cleanup:
>  return 0;
> }
>
> If you don't do this, you bypass the zero-initialization of 'str', and when ARC tries to clean it up, it'll end up trying to release some random bit of memory.
>
> This should handle any valid case; if you want to use 'goto' but can't enclose the variables in their own scope, you'll have to move the declarations before the goto (as K&R intended *grin*).
>
> Jordan
>
>
> On Aug 16, 2012, at 18:47 , Dave Keck <[hidden email]> wrote:
>
>> Hi all,
>>
>> I use the "check for success/goto failed" pattern everywhere, but
>> Clang refuses to compile such code with ARC enabled if the goto
>> bypasses a variable declaration (giving a "goto into protected scope"
>> error.)
>>
>> Is this something that's likely to be fixed? Compiling the program
>> below triggers the error.
>>
>>   // compile with: clang -framework Foundation -fobjc-arc <file>.m
>>   #import <Foundation/Foundation.h>
>>   int main() {
>>       goto cleanup;
>>       NSString *str;
>>       cleanup:
>>       return 0;
>>   }
>> _______________________________________________
>> cfe-dev mailing list
>> [hidden email]
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: ARC + goto into protected scope error

John McCall
In reply to this post by Dave Keck
On Aug 16, 2012, at 6:47 PM, Dave Keck wrote:
> I use the "check for success/goto failed" pattern everywhere, but
> Clang refuses to compile such code with ARC enabled if the goto
> bypasses a variable declaration (giving a "goto into protected scope"
> error.)
>
> Is this something that's likely to be fixed? Compiling the program
> below triggers the error.

This restriction is not likely to be lifted;  there are a number of challenges
with branches that enter scopes.

I assume your failure label is always at the end of the function;  in that case,
you can wrap everything up to that point in curly braces, like so:

- (void) broadcastChanges {
  {
    if (self.error) goto failure;
    NSString *foo = self.name;
    [self broadcastName: foo];
    return;
  }
  failure:
  abort();  
}

You might also consider moving to a structural programming pattern.

John.
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev