Bulletproofing C++ Code 
by Sergei Sokolov

Listing One

bool SetClient::contains(double d, SetType & h)
 {
   for (SetType::const_iterator it = h.begin(); it != h.end(); ++it) {
        // Pointer to a double of value d?
        if (*((double*)((*it)->getValue())) == d) {
            return true;
        }
   }
   return false;
 }

Listing Two

void Path::normalize(PathInfo& input) 
{ 
  PathElements result; 
  PathElements::const_iterator it = input.elements.begin(); 
  PathElements::const_iterator end = input.elements.end(); 
  for (; it != end; ++it) { 
      if (*it == "..") { 
          if (result.empty()) {   // BUG
              result.push_back(*it); 
          } else { 
              result.pop_back(); 
          } 
      } else if (*it != ".") { 
          result.push_back(*it); 
      } 
  } 
  input.elements.swap(result); 
} 


Listing Three

void Path::normalize(PathInfo& input) 
  PathElements result; 
  PathElements::const_iterator it  = input.elements.begin(); 
  PathElements::const_iterator end = input.elements.end(); 
  for (; it != end; ++it) { 
     if (*it == "..") { 
       if (result.empty() || result.back() == "..") { // BUG fixed
            result.push_back(*it); 
       } else { 
            result.pop_back(); 
       } 
     } else if (*it != ".") { 
        result.push_back(*it); 
    } 
  } 
  input.elements.swap(result); 
}




1


