Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird behaviour of DagReader.Seek() when using with SEEK_END #2567

Closed
sahib opened this issue Apr 15, 2016 · 2 comments
Closed

Weird behaviour of DagReader.Seek() when using with SEEK_END #2567

sahib opened this issue Apr 15, 2016 · 2 comments
Labels
help wanted Seeking public contribution on this issue kind/bug A bug in existing code (including security flaws)

Comments

@sahib
Copy link
Contributor

sahib commented Apr 15, 2016

Seek(0, SEEK_END) might sometimes return either the size of the stream in bytes (correct) or
0 (and io.EOF) on other occasions. I put together a minimal working example here:

https://gist.github.com/sahib/4363c36dc2352d1fba7634fd46eb7978

Steps to reproduce:

# Build the testcase:
$ curl -L http://tiny.cc/yo5tay > ipfsbug.go
$ go build ipfsbug.go

# Create a 257KB big file with all zeroes and throw it into ipfs
$ export HASH_257=`dd if=/dev/zero bs=1K count=257 2>/dev/null | ipfs add -q`
$ ./ipfsbug -repo ~/.ipfs -hash $HASH_257
263168 <nil>   
263167 <nil>
1 <nil>
0 EOF            # This should be the same as the first line.

# For files <= 256K it still seems to work (256K == chunksize?):
$ export HASH_256=`dd if=/dev/zero bs=1K count=256 2>/dev/null | ipfs add -q` 
$ ./ipfsbug -repo ~/.ipfs -hash $HASH_256
262144 <nil>
262143 <nil>
1 <nil>
262144 <nil>

Another oddity: ipfs seems to use a positive offset for going back from SEEK_END, while other implementation usually use negative offsets to go back from the end and positive to extend above the file limits. Example:

// ipfs semantics: Go back one byte from the end...
r.Seek(1, os.SEEK_END)
// ...but usually you'd expect this:
r.Seek(-1, os.SEEK_END)

I failed to find a good reference that suggests that behaviour,
but the go docs say this about SEEK_END:

const (
        SEEK_SET int = 0 // seek relative to the origin of the file
        SEEK_CUR int = 1 // seek relative to the current offset
        SEEK_END int = 2 // seek relative to the end
)

...where relative to (hopefully) should mean the same direction for all whences.
This should be easy to change, since (after a quick look) ipfs only ever uses Seek(0, SEEK_END).

@whyrusleeping
Copy link
Member

@sahib good catch there. I definitely had a misunderstanding as to the argument to SEEK_END

@whyrusleeping whyrusleeping added the kind/bug A bug in existing code (including security flaws) label Apr 15, 2016
@whyrusleeping whyrusleeping added the help wanted Seeking public contribution on this issue label Aug 18, 2016
@momack2 momack2 added this to Inbox in ipfs/go-ipfs May 9, 2019
@magik6k
Copy link
Member

magik6k commented May 14, 2019

This was fixed in ipfs/go-unixfs#60

@magik6k magik6k closed this as completed May 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Seeking public contribution on this issue kind/bug A bug in existing code (including security flaws)
Projects
No open projects
Development

No branches or pull requests

3 participants