Prevent Xcode / clang from raising logical error on intentionally erroneous code

For testing purposes only, I enable the intentional crash function of my application (testing my application in case of unexpected failures). For this, I use:

strcpy(0, "crash");

Of course, when parsing my code, Xcode reports a logical error Null pointer argument in call to string copy function. I tried to wrap the violating code like this:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
    strcpy(0, "crash");
#pragma clang diagnostic pop

But Xcode (v9.2 (9C40b)) is still complaining (this is not a warning, this is a logical error, I understand). Is there any other way for Xcode / clang not to mark this code? Is there a better way to cause crashes that can be protected from an Xcode / clang parsing error?

+4
source share
3 answers

( ). Xcode/clang , :

char *x = (char *)@"0".integerValue; strcpy(x, "crash");
+2

volatile char* x=0; strcpy(x, "crash")

, , x, ; , , .

0

, - - . Objective-C, , , 1--1 .

- , , :

[@"" characterAtIndex:0]; // or
[@[] objectAtIndex:0];

Even if the analyzer knew that the default implementations of these methods threw an exception for access outside the bounds, it cannot know that you did not change them at run time for an implementation that handles the problem gracefully.

0
source

Source: https://habr.com/ru/post/1691941/


All Articles