go.rice icon indicating copy to clipboard operation
go.rice copied to clipboard

Fix: virtualFile.Read returns EOF with valid data

Open cmaglie opened this issue 5 years ago • 1 comments

It happens that the read method in VirtualFile may return EOF together with data. This is not how the os.File behaves normally as explained in https://golang.org/pkg/os/#File.Read

func (f *File) Read(b []byte) (n int, err error)

Read reads up to len(b) bytes from the File. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.

This bug leads to an infinite loop when I tried to read openpgp keyring using the golang builting openpgp package. After some digging I found that the library relies on the fact that Read sends the EOF alone (without valid data).

This pull request fix the wrong behaviour.

I've added also a test to check how the os.File.Read behaves compared to rice.File.Read.

=== RUN   TestVirtualFileRead
    TestVirtualFileRead: virtual_test.go:33: 
                Error Trace:    virtual_test.go:33
                                                        virtual_test.go:37
                Error:          Not equal: 
                                expected: "n=4 err=<nil> buff=[84 69 83 84]"
                                actual  : "n=4 err=EOF buff=[84 69 83 84]"
                            
                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -n=4 err=<nil> buff=[84 69 83 84]
                                +n=4 err=EOF buff=[84 69 83 84]
                Test:           TestVirtualFileRead
--- FAIL: TestVirtualFileRead (0.00s)
FAIL
exit status 1
FAIL    github.com/GeertJohan/go.rice   0.004s

I would like to also thank you for go.rice, we are using it successfully in the arduino-cli.

cmaglie avatar Jul 06 '20 16:07 cmaglie

@GeertJohan is there any chance this will be merged?

silvanocerza avatar Aug 10 '21 10:08 silvanocerza