4

We have a die function that outputs an error message and exits, e.g.:

void die(const char* msg) {
    fprintf(stderr, "Error: %s\n", msg);
    exit(1);
}

We use Parasoft C++test to statically analyze our code, but it doesn't realize that die is a non-returning function. So when it sees code like:

void foo(Bar* bar) {
    if(!bar) {
        die("bar is NULL");
    }
    Bar bar2 = *bar;
}

It warns that *bar might be dereferencing a null pointer, even though bar being NULL would prevent that line from ever executing. Is there a way to mark die as non-returning in a way Parasoft would recognize?


Edit: I need something that works in both GCC and VS 2003, but I'm not above #ifdefing my way around things if somebody has a solution that only works in VS

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • Could you just put a return after the call to die? Would that fix it? – Benjamin Lindley Apr 19 '11 at 14:31
  • @Benjamin Sorry, I misread your comment originally. I guess that would work, but it's kind of inconvenient, particularly since `die()` will get used in functions that have non-trivial return types – Michael Mrozek Apr 19 '11 at 14:48

3 Answers3

3

I figured it out. It turns out Parasoft has a built-in list of NRFs you can customize; they're called "terminating functions". You can edit them through the VS GUI or through the configuration file if you run Parasoft outside of VS

Through Visual Studio

Through the configuration file

Add lines like the following:

com.parasoft.xtest.checker.flowanalysis.terminators.api0.methods=active\=true|name\=die|type\=*|paramsn\=*|defsInSubClasses\=true;
com.parasoft.xtest.checker.flowanalysis.terminators.apis=active\=true|name\=foo;
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
2

In gcc, you should attribute the function with something like:

     die (const char *, ...) 
          __attribute__ ((format_arg (1)))
          __attribute__ ((noreturn))
sehe
  • 374,641
  • 47
  • 450
  • 633
1

If you're using Visual Studio 2005+, you can use __declspec(noreturn) like this:

__declspec(noreturn) void die(char const* msg){
  fprntf(stderr, "Error: %s\n"; msg);
  exit(1);
}

Maybe that helps Parasoft to recognize the function as non-returning.
Edit: GCC has __attribute__((noreturn)) (first example).

Xeo
  • 129,499
  • 52
  • 291
  • 397
  • I probably should've mentioned I need something that works in GCC and VS; I edited the question. I'll look into that though, if Parasoft picks up on it I can just `#ifdef` it out in GCC builds – Michael Mrozek Apr 19 '11 at 14:25
  • Sadly, it looks like Parasoft doesn't notice `__declspec(noreturn)`; adding it didn't change the results – Michael Mrozek Apr 19 '11 at 15:22
  • @Michael: Sorry then. :/ Maybe you can contact the parasoft support? – Xeo Apr 19 '11 at 15:58