@@ -2468,13 +2468,14 @@ class mmap {
24682468
24692469private:
24702470#if defined(_WIN32)
2471- HANDLE hFile_;
2472- HANDLE hMapping_;
2471+ HANDLE hFile_ = NULL ;
2472+ HANDLE hMapping_ = NULL ;
2473+ bool is_open_empty_file_on_windows_ = false ;
24732474#else
2474- int fd_;
2475+ int fd_ = - 1 ;
24752476#endif
2476- size_t size_;
2477- void *addr_;
2477+ size_t size_ = 0 ;
2478+ void *addr_ = nullptr ;
24782479};
24792480
24802481} // namespace detail
@@ -2895,14 +2896,7 @@ inline void stream_line_reader::append(char c) {
28952896 }
28962897}
28972898
2898- inline mmap::mmap (const char *path)
2899- #if defined(_WIN32)
2900- : hFile_(NULL ), hMapping_(NULL )
2901- #else
2902- : fd_(-1 )
2903- #endif
2904- ,
2905- size_ (0 ), addr_(nullptr ) {
2899+ inline mmap::mmap (const char *path) {
29062900 open (path);
29072901}
29082902
@@ -2946,6 +2940,13 @@ inline bool mmap::open(const char *path) {
29462940 hMapping_ = ::CreateFileMappingW (hFile_, NULL , PAGE_READONLY, 0 , 0 , NULL );
29472941#endif
29482942
2943+ // TODO: Special treatment for an empty file on Windows... (#1933)
2944+ if (hMapping_ == NULL && size_ == 0 ) {
2945+ close ();
2946+ is_open_empty_file_on_windows_ = true ;
2947+ return true ;
2948+ }
2949+
29492950 if (hMapping_ == NULL ) {
29502951 close ();
29512952 return false ;
@@ -2956,6 +2957,11 @@ inline bool mmap::open(const char *path) {
29562957#else
29572958 addr_ = ::MapViewOfFile (hMapping_, FILE_MAP_READ, 0 , 0 , 0 );
29582959#endif
2960+
2961+ if (addr_ == nullptr ) {
2962+ close ();
2963+ return false ;
2964+ }
29592965#else
29602966 fd_ = ::open (path, O_RDONLY);
29612967 if (fd_ == -1 ) { return false ; }
@@ -2968,21 +2974,33 @@ inline bool mmap::open(const char *path) {
29682974 size_ = static_cast <size_t >(sb.st_size );
29692975
29702976 addr_ = ::mmap (NULL , size_, PROT_READ, MAP_PRIVATE, fd_, 0 );
2971- #endif
29722977
2973- if (addr_ == nullptr ) {
2978+ if (addr_ == MAP_FAILED ) {
29742979 close ();
29752980 return false ;
29762981 }
2982+ #endif
29772983
29782984 return true ;
29792985}
29802986
2981- inline bool mmap::is_open () const { return addr_ != nullptr ; }
2987+ inline bool mmap::is_open () const {
2988+ #if defined(_WIN32)
2989+ if (is_open_empty_file_on_windows_) {
2990+ return true ;
2991+ }
2992+ #endif
2993+ return addr_ != nullptr ;
2994+ }
29822995
29832996inline size_t mmap::size () const { return size_; }
29842997
29852998inline const char *mmap::data () const {
2999+ #if defined(_WIN32)
3000+ if (is_open_empty_file_on_windows_) {
3001+ return " " ;
3002+ }
3003+ #endif
29863004 return static_cast <const char *>(addr_);
29873005}
29883006
@@ -3002,6 +3020,8 @@ inline void mmap::close() {
30023020 ::CloseHandle (hFile_);
30033021 hFile_ = INVALID_HANDLE_VALUE;
30043022 }
3023+
3024+ is_open_empty_file_on_windows_ = false ;
30053025#else
30063026 if (addr_ != nullptr ) {
30073027 munmap (addr_, size_);
0 commit comments