Ignore:
Timestamp:
Nov 30, 2015, 8:32:25 AM (3 years ago)
Author:
cameron
Message:

Bug fix for mmap of empty file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/do_grep.cpp

    r4826 r4883  
    211211    mFileSize = file_size(file);
    212212    mapped_file mFile;
    213     try {
    214         mFile.open(mFileName, mapped_file::priv, mFileSize, 0);
    215     } catch (std::ios_base::failure e) {
    216         std::cerr << "Error: Boost mmap " << e.what() << std::endl;
    217         return;
    218     }
    219     mFileBuffer = mFile.data();
     213    if (mFileSize == 0) {
     214        mFileBuffer = nullptr;
     215    }
     216    else {
     217        try {
     218            mFile.open(mFileName, mapped_file::priv, mFileSize, 0);
     219        } catch (std::ios_base::failure e) {
     220            std::cerr << "Error: Boost mmap of " << mFileName << ": " << e.what() << std::endl;
     221            return;
     222        }
     223        mFileBuffer = mFile.data();
     224    }
    220225#else
    221226    struct stat infile_sb;
     
    235240    }
    236241    mFileSize = infile_sb.st_size;
    237     mFileBuffer = (char *) mmap(NULL, mFileSize, PROT_READ, MAP_PRIVATE, fdSrc, 0);
    238     if (mFileBuffer == MAP_FAILED) {
    239         if (errno ==  ENOMEM) {
    240             std::cerr << "Error:  mmap of " << mFileName << " failed: out of memory\n";
    241         }
    242         else {
    243             std::cerr << "Error: mmap of " << mFileName << " failed with errno " << errno << ". Skipped.\n";
    244         }
    245         return;
     242    if (mFileSize == 0) {
     243        mFileBuffer = nullptr;
     244    }
     245    else {
     246        mFileBuffer = (char *) mmap(NULL, mFileSize, PROT_READ, MAP_PRIVATE, fdSrc, 0);
     247        if (mFileBuffer == MAP_FAILED) {
     248            if (errno ==  ENOMEM) {
     249                std::cerr << "Error:  mmap of " << mFileName << " failed: out of memory\n";
     250                close (fdSrc);
     251            }
     252            else {
     253                std::cerr << "Error: mmap of " << mFileName << " failed with errno " << errno << ". Skipped.\n";
     254                close (fdSrc);
     255            }
     256            return;
     257        }
    246258    }
    247259#endif
Note: See TracChangeset for help on using the changeset viewer.