edsrzf / mmap-go

A portable mmap package for Go
BSD 3-Clause "New" or "Revised" License
938 stars 129 forks source link

map empty file on ubuntu #16

Closed mrka124 closed 2 years ago

mrka124 commented 6 years ago

On windows, I specific file size and it works normally but not on ubuntu. It causes bus error like https://stackoverflow.com/questions/20587935/bus-error-opening-and-mmaping-a-file

gsauthof commented 6 years ago

When calling mmap.Map() for an empty file it returns a proper error for me that can be displayed like this:

if err != nil {
  panic(err.Error())
}

That means: no bus error is caused

Example output:

panic: invalid argument

This is due to how POSIX specifies mmap() (cf. mmap(3p)):

If len is zero, mmap() shall fail and no mapping shall be established.

And on Linux is thus fails with EINVAL.

This package could catch this case, i.e. if file size is zero then return an empty slice instead of doing a real mapping.

PS: Newer versions of this package test for zero length but return a custom error message instead of returning an empty slice:

https://github.com/edsrzf/mmap-go/blob/master/mmap.go#L73

edsrzf commented 2 years ago

Closing since this is addresses in newer releases.