Menu

Out of bounds access in expression

2020-09-09
2020-09-10
  • jeniffer1234

    jeniffer1234 - 2020-09-09

    Minimum working example

    #include <boost/filesystem.hpp>
    #include <boost/scoped_array.hpp>
    #include <boost/foreach.hpp>
    #include <string>
    #include <iostream>
    
    void TEST(const std::string& file_name)
    {
    
       if (!file_name.empty())
       {
          std::ifstream input(file_name.c_str(), std::ios::binary);
          if (!input)
          {
             return;
          }
    
          int file_size = boost::filesystem::file_size(file_name);
          if (file_size <= 0)
          {
             return;
          }
          boost::scoped_array<char> buffer;
          buffer.reset(new char[file_size]);
          buffer[0] = '\0';
          std::cout << buffer;
       }
    }
    

    Cppcheck:
    error/containerOutOfBounds: Out of bounds access in expression 'buffer[0]' because 'buffer' is empty

     
  • Daniel Marjamäki

    I hope this is fixed. I can reproduce with cppcheck-2.0 but I can't reproduce with cppcheck-HEAD.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.