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

7z: Fix iteration bug #394

Merged
merged 1 commit into from Jan 23, 2024
Merged

7z: Fix iteration bug #394

merged 1 commit into from Jan 23, 2024

Conversation

SheltonZhu
Copy link
Contributor

@SheltonZhu SheltonZhu commented Jan 22, 2024

7z: Fix iteration bug that was already fixed in go 1.22.
The bug is same as https://github.com/mholt/archiver/blob/master/zip.go#L199

fix: fix iteration bug
@SheltonZhu SheltonZhu changed the title Update 7z.go fix: fix iteration bug in 7z.go that was already fixed in go 1.22 Jan 22, 2024
@SheltonZhu SheltonZhu changed the title fix: fix iteration bug in 7z.go that was already fixed in go 1.22 fix: fix iteration bug in 7z.go Jan 22, 2024
@SheltonZhu SheltonZhu changed the title fix: fix iteration bug in 7z.go 7z: fix iteration bug Jan 22, 2024
@SheltonZhu SheltonZhu changed the title 7z: fix iteration bug 7z: Fix iteration bug Jan 22, 2024
@mholt
Copy link
Owner

mholt commented Jan 22, 2024

Thanks! Go 1.22 hasn't been released yet, and I doubt everyone will upgrade right away. I'll probably hold this open for a while until the release has been out for a while.

@SheltonZhu
Copy link
Contributor Author

SheltonZhu commented Jan 23, 2024

Thanks! Go 1.22 hasn't been released yet, and I doubt everyone will upgrade right away. I'll probably hold this open for a while until the release has been out for a while.

I mean I fix the "for" loops bug (sharing of loop variables).
Please merge my pr.
And the issue is same as zip.go, The funtion archiver.File.Open() is a closure.
I copy code from https://github.com/mholt/archiver/blob/master/zip.go#L199

ref: go-reference-in-for-loop-sets-the-wrong-reference

@SheltonZhu
Copy link
Contributor Author

And go 1.22 has fix it. (No more sharing of loop variables)
ref: https://antonz.org/go-1-22/

@SheltonZhu
Copy link
Contributor Author

I can't get the excepted io.ReadCloser when I call archiver.File.Open() outside of the archiver.FileHandler.
But it is correct when I use archiver.Zip{}. (Translated from GPT)

type ArchiverExtractor struct {
	archiver.Extractor
	sourceArchive   io.Reader
	fileHandlerFunc FileHanderFunc
	pathsInArchive  []string
}

func (ae *ArchiverExtractor) ExtractFile(ctx context.Context, filePath string) (*archiver.File, error) {
	files := make([]archiver.File, 0)
	ff := FileFilter(&files, filePath)
	if ae.fileHandlerFunc != nil {
		ff = ae.fileHandlerFunc(&files)
	}
	err := ae.Extract(ctx, ae.sourceArchive, ae.pathsInArchive, ff)
	if len(files) == 0 {
		return nil, fmt.Errorf("file not found")
	}
	return &files[0], err
}

// ...
func main() {
    // ...

    frc, err := f.Open() 
    if err != nil {
       panic(err)
    }
    defer frc.Close()
    fileBytes, _ := io.ReadAll(frc) // empty 
}

@mholt
Copy link
Owner

mholt commented Jan 23, 2024

Oh, my bad -- I understand now. Thank you.

@mholt mholt merged commit 81f9e06 into mholt:master Jan 23, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants