Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions auxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lua
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
Expand Down Expand Up @@ -357,7 +358,7 @@ func (ls *LState) LoadFile(path string) (*LFunction, error) {
reader := bufio.NewReader(file)
// get the first character.
c, err := reader.ReadByte()
if err != nil {
if err != nil && err != io.EOF {
return nil, newApiErrorE(ApiErrorFile, err)
}
if c == byte('#') {
Expand All @@ -368,7 +369,15 @@ func (ls *LState) LoadFile(path string) (*LFunction, error) {
return nil, newApiErrorE(ApiErrorFile, err)
}
}
reader.UnreadByte()

if err != io.EOF {
// if the file is not empty,
// unread the first character of the file or newline character(readBufioLine's last byte).
err = reader.UnreadByte()
if err != nil {
return nil, newApiErrorE(ApiErrorFile, err)
}
}

return ls.Load(reader, path)
}
Expand Down
16 changes: 16 additions & 0 deletions auxlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,19 @@ print("hello")
_, err = L.LoadFile(tmpFile.Name())
errorIfNotNil(t, err)
}

func TestLoadFileForEmptyFile(t *testing.T) {
tmpFile, err := ioutil.TempFile("", "")
errorIfNotNil(t, err)

defer func() {
tmpFile.Close()
os.Remove(tmpFile.Name())
}()

L := NewState()
defer L.Close()

_, err = L.LoadFile(tmpFile.Name())
errorIfNotNil(t, err)
}