edsrzf / mmap-go

A portable mmap package for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

map empty file on ubuntu

mrka124 opened this issue · comments

commented

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

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

Closing since this is addresses in newer releases.