Search code examples
cxcodestatic-analysisclang-static-analyzer

Is it possible to suppress since instances of issues reported by the Xcode (clang) analyzer?


My use case is as follows. In the automated testing of one of my libraries I use the mktemp function in order to obtain a filename in order to create a temporary file. Xcode correctly complains about this as a security risk, but in this case I have no option (the API I must follow demands filenames) and I am willing to take the risk since the code is only the test code and not in an actual service. (Hence the security risk is not applicable.)

I suppose I could create my own version of a mktemp that is local to my testing, but I would prefer not to write things that have already been written.

So what I am wondering is if there is a way that I can tell the analyzer to stop complaining this instance of the problem? Note that this differs from the question asked in Is it possible to suppress Xcode 4 static analyzer warnings? in that this is not a false positive, and I do not want to suppress analyzing the file or all instances of this check. I just want to suppress this one instance. (i.e. something similar to cppcheck-suppress comment in Cppcheck)


Solution

  • @JonathanLeffler last comment was absolutely correct and I don't know how I missed it when I read the question I referenced. The following code segment does exactly what I want - it suppresses the analyzer warning in this instance of mktemp while leaving it active for all other instances that would use mktemp.

    #if defined(__clang_analyzer__)
       char* filename = "/tmp/somename";
    #else
       char* filename = mktemp("/tmp/prefixXXXX");
    #endif