From 9e9a27cc40449695a0c0e97cd364b05ead53ffc4 Mon Sep 17 00:00:00 2001 From: kohkimakimoto Date: Fri, 27 May 2016 13:06:29 +0900 Subject: [PATCH 1/2] add failed test to find a bug to load empty file. --- auxlib_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/auxlib_test.go b/auxlib_test.go index 383f1c86..5dff1df1 100644 --- a/auxlib_test.go +++ b/auxlib_test.go @@ -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) +} From 3cc745e07eec08ccec71852fb0a6daa50d3e7841 Mon Sep 17 00:00:00 2001 From: kohkimakimoto Date: Fri, 27 May 2016 13:36:42 +0900 Subject: [PATCH 2/2] Issue #74 : fix the bug to load empty file --- auxlib.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/auxlib.go b/auxlib.go index cb3dd24d..15199390 100644 --- a/auxlib.go +++ b/auxlib.go @@ -3,6 +3,7 @@ package lua import ( "bufio" "fmt" + "io" "os" "strings" ) @@ -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('#') { @@ -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) }