archives: Nil pointer deref on opening a file within a zip file

What version of the package or command are you using?

v0.0.0-20241119192853-c1ebc8406725

What are you trying to do?

Use an existing ZIP file as a file system.

What steps did you take?

package main

import (
	"context"
	"github.com/mholt/archives"
	"github.com/sanity-io/litter"
	"os"
)

func main() {
	compressed, err := os.Open("C:\\artifacts.zip")
	if err != nil {
		panic(err)
	}
	defer compressed.Close()

	compressedContent := new(bytes.Buffer)
	_, err = io.Copy(compressedContent, compressed)
	if err != nil {
		panic(err)
	}

	afs, err := archives.FileSystem(context.Background(), "", bytes.NewReader(compressedContent.Bytes()))
	if err != nil {
		panic(err)
	}

	file, err := afs.Open("response")
	if err != nil {
		panic(err)
	}
	defer file.Close()
        ...
}

What did you expect to happen, and what actually happened instead?

The program from above panics with a nil pointer dereference:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x28 pc=0x64a8c8]

goroutine 1 [running]:
github.com/mholt/archives.(*Archive).OpenReader(0x993710?, {0x781f20?, 0xc000022ea0?})
	<autogenerated>:1 +0x28
github.com/mholt/archives.ArchiveFS.Open({{0x0, 0x0}, 0xc000022e70, {0x781f80, 0xc000022e40}, {0x0, 0x0}, {0x784b10, 0x9eb960}, 0x0, ...}, ...)
	C:/Users/<user>/go/pkg/mod/github.com/mholt/archives@v0.0.0-20241119192853-c1ebc8406725/fs.go:354 +0xbff
main.main()
	C:/Users/<user>/AppData/Roaming/JetBrains/GoLand2024.2/scratches/main.go:22 +0xca

How do you think this should be fixed?

From the debugger it seems to panic exactly here: https://github.com/mholt/archives/blob/main/fs.go#L354 I used the stream option, since in my use case the file will be in memory and I really would want to avoid writing it to a temp-fs or so.

Please link to any related issues, pull requests, and/or discussion

🤷🏻

Bonus: What do you use this package for, and do you have any other suggestions or feedback?

Here you return “just” the fs.FS interface: https://github.com/mholt/archives/blob/main/fs.go#L45 even though the object returned implements much more then just fs.FS (and its .Open()) e.g. fs.ReadDirFile (like in https://github.com/mholt/archives/blob/main/README.md?plain=1#L193).

Downcasting is always possible, but upcasting requires code and err handling (that need to be tested) which can be avoided if you use the broadest interface that the struct implements.

If there is no interface that covers all create a new one including all interfaces which are implemented and return that since then its implementing all the “child” interfaces.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Just heard a talk, some part addressed our very discussion 😄 image

Then your proposed version might panic without proper error handling if the underlying struct is changes. So we’re back to my original version.

I guess you referring to “be lose with inputs and strict with output” though this applies to adhering to standards (and so interfaces), however they might look like. Adhering strictly to a broader interface does not violate this.

I think it is just inconvenient and that I have superfluous code to be save if thats avoidable in the first place. fs.ReadDirFS -> fs.FS -(error checking)-> fs.ReadDirFS while one could just to use the fs.ReadDirFS in the first place.

The user has no downside in receiving a broader interface and its even more flexible for him/her.

But I get the point you want to keep it minimal for (possibly) future development.

It depends on what you value more, UX (I do as I am a user) or DX (you do as the developer/maintainer)

As I said. Its your show 😄, you decide and if I havent convinced you till now I dont have more arguments to do so 🤷🏻 So no big deal, I do the type assertion and you will be flexible for future development.

@mholt Can I convince you with my suggestion about the interface? Since all the structs archives.FileSystem(...) returns, seem to implement at least:

fs.FS
fs.ReadDirFS
fs.StatFS