Do not use #pragma warning(default:X)

The fragment is taken from TortoiseGIT project. The error is detected by the following diagnostic: V665 Possibly, the usage of ‘#pragma warning(default: X)’ is incorrect in this context. The ‘#pragma warning(push/pop)’ should be used instead.

#pragma warning(disable:4996)
LONG result = regKey.QueryValue(buf, _T(""), &buf_size);
#pragma warning(default:4996)


Programmers often assume that warnings disabled with the “pragma warning(disable: X)” directive earlier will start working again after using the “pragma warning(default : X)” directive. But it is not so. The ‘pragma warning(default : X)’ directive sets the ‘X’ warning to the DEFAULT state, which is quite not the same thing.

понял о чем речь, не то, опять понял, не то и дошло

Suppose that a file is compiled with the /Wall switch used. The C4061 warning must be generated in this case. If you add the “#pragma warning(default : 4061)” directive, this warning will not be displayed, as it is turned off by default.

Correct code

#pragma warning(push)
#pragma warning(disable:4996)
LONG result = regKey.QueryValue(buf, _T(""), &buf_size);
#pragma warning(pop)


The correct way to return the previous state of a warning is to use directives “#pragma warning(push[ ,n ])” and “#pragma warning(pop)”. See the Visual C++ documentation for descriptions of these directives: Pragma Directives. Warnings.

Library developers should pay special attention to the V665 warning. Careless warning customization may cause a whole lot of trouble on the library users’ side.

Written by Andrey Karpov.

This error was found with PVS-Studio static analysis tool.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.