Warning C4701 is sometimes suppressed when compiling with /RTC1
Asked Answered
S

1

8

This piece of code (note the commented line):

#include <malloc.h>

#pragma warning(error: 4701)

int main(){
    char buffer[1024];
    //buffer[0] = 0;

    void *p;
    int size = 1;
    if (size < 2)
        p = malloc(size);
    free(p); // C4701
    return 0;
}

Gives the following warning (as expected):

f:\d\warning.cpp(13) : error C4701: potentially uninitialized local variable 'p' used

However, when I uncomment the assignment in main(), the warning is no longer given. I am compiling with /RTC1 command line option to enable run-time error checks:

cl.exe /RTC1 warning.cpp

I've tried the latest 64-bit versions of compilers from Visual C++ 2013 and 2015. Both are producing the same behaviour.

Question is: is this a compiler bug, or is there an explanation for this? Microsoft's documentation mentions that /RTC1 might give run-time error in places where C4701 is given, but it says nothing about the warning being suppressed.

EDIT: The puzzling part is that the warning disappears only when buffer[0] = 0; is not in comment.

Stereophotography answered 13/2, 2016 at 11:2 Comment(7)
Try adding the compiler option /W4 . /W1 is default and you may need it to be higher. And/Or try /Wall . Source: msdn.microsoft.com/en-us/library/thxezb7y.aspxPianoforte
I think it should raise a warning in both cases. Otherwise, something wrong is going on. I do not think that line will solve the existence problem not even decrease the probability of it.Conditioning
No repro on VS 2010. I get an error for 4701 regardless of whether or not I compile with the /RTC1 switch.Garzon
Use connect.microsoft.com to report issues like this, we can't fix this here. I'd randomly guess they'll tell you that p is in fact initialized when you use /RTC1 so this is expected. That's accurate enough, be sure to mention that this is not the behavior you prefer.Declan
Maybe I've formulated it wrong: the warning is suppressed only when uncommenting the commented assignment, in combination with /RTC1. Problem is, this assignment is totally unrelated to p!Stereophotography
Richard Critten, the #pragma warning(error: 4701) statement overrules the /W option for specified warning.Stereophotography
I've logged the issue on MS connect: connect.microsoft.com/VisualStudio/feedback/details/2356140/…Stereophotography
O
1

There are many situations where something is sub-optimal, probably buggy or even undefined where the compiler has a very hard time detecting this. Thus, you should not rely on warnings (and/or runtime errors triggered by compiler instrumentation) to give you the complete truth.

Know that the compiler may warn when you do something stupid. It also may generate code to blow up at runtime when you do something stupid. Just never rely on that. It can't detect everything and you have to know the rules yourself.

Ortegal answered 16/3, 2016 at 22:17 Comment(2)
I couldn't agree more with your comment. But the problem here is: the warning is suppressed in debug builds that developers use, and pops up in release builds that the build server makes.Stereophotography
@Georgy Pashkov That's not unexpected. It's fairly common for optimizations to expose new warnings/problems that don't show up in unoptimized builds.Ortegal

© 2022 - 2024 — McMap. All rights reserved.