Advertisement
PVS-StudioWarnings

PVS-Studio warning V507 for TortoiseSVN

Nov 27th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. LRESULT CALLBACK CMainWindow::WinMsgHandler(....)
  2. {
  3.   ...
  4.   if (pNMHDR->code == TTN_GETDISPINFO)
  5.   {
  6.     LPTOOLTIPTEXT lpttt;
  7.  
  8.     lpttt = (LPTOOLTIPTEXT) lParam;
  9.     lpttt->hinst = hResource;
  10.  
  11.     // Specify the resource identifier of the
  12.     // descriptive text for the given button.
  13.     TCHAR stringbuf[MAX_PATH] = {0};
  14.     ...
  15.     lpttt->lpszText = stringbuf;
  16.   }
  17.   ...
  18. }
  19.  
  20. It's not that simple with this code. Theoretically, the code contains an error. Practically, it works well.
  21. The V507 diagnostic message warns you that an object is used after being destroyed. The 'stringbuf' buffer will be used after exiting the body of the 'if' operator.
  22. If 'stringbuf' was an object of the class std::string, for instance, its behavior would be incorrect. We would use an already destroyed object in that case. But here 'stringbuf' is an array created in the stack. The Visual C++ compiler doesn't use this stack area again, so the buffer will exist until the 'CMainWindow::WinMsgHandler' function finishes its work. Thus, no error occurs, although the code is potentially dangerous.
  23.  
  24.  
  25. This suspicious code was found in TortoiseSVN project by PVS-Studio static code analyzer.
  26. Warning message is:
  27. V507 Pointer to local array 'stringbuf' is stored outside the scope of this array. Such a pointer will become invalid. mainwindow.cpp 277
  28.  
  29. PVS-Studio is a static analyzer for detecting bugs in the source code of applications written in C, C++, C++11, C++/CX. Site: http://www.viva64.com/en/pvs-studio/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement