alexcrichton / tar-rs

Tar file reading/writing for Rust
https://docs.rs/tar
Apache License 2.0
629 stars 190 forks source link

Doesn't work correctly when entries are collected into a Vec #344

Open KSXGitHub opened 1 year ago

KSXGitHub commented 1 year ago

Steps to reproduce

  1. Clone this repo: https://github.com/KSXGitHub/tar-rs-bug-collect-entries-343
  2. Run reproduce.bash

Expected behavior

It prints the content of the files correctly.

Actual behavior

It prints empty strings as content.

ChewingBever commented 1 year ago

Note that care must be taken to consider each entry within an archive in sequence. If entries are processed out of sequence (from what the iterator returns), then the contents read for each entry may be corrupted.

As the docs state in the description of the entries function, you cannot process the entries out of order., which is what you are doing. You have to process the entries one by one from what the iterator returns.

This isn't a bug, it's expected behavior.

namse commented 11 months ago

In my case, tar always read file's content as empty.

For example, when I save the content as Hello, but it shows '\0\0\0\0\0'.

assertion `left == right` failed
  left: "\0\0\0\0\0"
 right: "Hello"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

So I guess it's not a problem to use collect, but I think there are some problem with in-memory source?

-- edited

Nevermind my case, it become ok when I use for-loop instead of collect.

kaimast commented 2 weeks ago

This isn't a bug, it's expected behavior.

I still think this should be caught at compile time somehow. Maybe an entry could hold a mutable reference to the associated entries list, so grabbing the next entry will alert the borrow checker?

Also, maybe this should be mentioned in the documentation more prominently. The pages for Entry and Entries do not mention this.