PVS-StudioWarnings

PVS-Studio warning V530 for MongoDB

Nov 13th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. void CmdLine::parseConfigFile( istream &f, stringstream &ss ) {
  2.   ....
  3.   f.getline(line, MAX_LINE_LENGTH);
  4.   s = line;
  5.   std::remove(s.begin(), s.end(), ' ');
  6.   std::remove(s.begin(), s.end(), '\t');
  7.   boost::to_upper(s);
  8.   ....
  9. }
  10.  
  11. The std::remove function does not remove elements from the container. It only shifts the elements and brings the iterator back to the beginning of the trash. Suppose we have the vector<int> container that contains elements 1,2,3,1,2,3,1,2,3. If we execute the code "remove( v.begin(), v.end(), 2 )", the container will contain elements 1,3,1,3,?,?,?, where ? is some trash. The function will bring the iterator back to the first senseless element, so if we want to remove these trash elements, we must write the code this way: "v.erase(remove(v.begin(), v.end(), 2), v.end())".
  12.  
  13. This suspicious code was found in MongoDB project by PVS-Studio static code analyzer.
  14. Warning message is:
  15. V530 The return value of function 'remove' is required to be utilized. cmdline.cpp 106
  16.  
  17. 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