PVS-StudioWarnings

PVS-Studio warning V507 for MySQL

Nov 20th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. int ha_myisam::assign_to_keycache(THD* thd,
  2.                                   HA_CHECK_OPT *check_opt)
  3. {
  4.   ...
  5.   const char *errmsg= 0;
  6.   ...
  7.   if ((error= mi_assign_to_key_cache(file, map, new_key_cache)))
  8.   {
  9.     char buf[STRING_BUFFER_USUAL_SIZE];
  10.     my_snprintf(buf, sizeof(buf),
  11.       "Failed to flush to index file (errno: %d)", error);
  12.     errmsg = buf;
  13.     error = HA_ADMIN_CORRUPT;
  14.   }
  15.   ...
  16. }
  17.  
  18. It's not that simple with this code. Theoretically, the code contains an error. Practically, it works well.
  19. The V507 diagnostic message warns you that an object is used after being destroyed. The 'buf' buffer will be used after exiting the body of the 'if' operator.
  20. If 'buf' 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 'buf' 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 'ha_myisam::assign_to_keycache' function finishes its work. Thus, no error occurs, although the code is potentially dangerous.
  21.  
  22. This suspicious code was found in MySQL project by PVS-Studio static code analyzer.
  23. Warning message is:
  24. V507 Pointer to local array 'buf' is stored outside the scope of this array. Such a pointer will become invalid. myisam ha_myisam.cc 1156
  25.  
  26. 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