Skip to content

libFLAC: reset has_stats in FLAC__metadata_simple_iterator_init() - #955

Open
GeekQAQ wants to merge 1 commit into
xiph:masterfrom
GeekQAQ:simple-iterator-has-stats
Open

GeekQAQ wants to merge 1 commit into
xiph:masterfrom
GeekQAQ:simple-iterator-has-stats

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown

FLAC__metadata_simple_iterator_init() sets iterator->has_stats only when the file is opened with read_only == false and preserve_file_stats == true; otherwise it leaves the previous value. When an iterator that was first initialized on file A with preserve_file_stats is initialized again on file B (read-only, or without preserve_file_stats), has_stats is still true and stats still holds A's attributes. When B is closed (FLAC__metadata_simple_iterator_delete() or the next init()), simple_iterator_free_guts_() calls set_file_stats_() on B: B gets A's permissions, modification time and owner/group.

init() already handles being called on a used iterator: it starts with simple_iterator_free_guts_(), which closes the previous file and restores that file's attributes. The header documents preserve_file_stats as keeping the owner and modification time of the file being edited; a read-only walk over B should not change B at all.

Reproduction

Tested on master 59fa340. The program metareuse.c in https://gist.github.com/GeekQAQ/4a3ebaafe40342e6c035c086f93c1916 (with flac_common.h) writes a.flac (mode 0644, mtime 1000000000) and b.flac (mode 0600, mtime 1200000000) to a temporary directory:

cc -o metareuse metareuse.c $(pkg-config --cflags --libs flac ogg)
./metareuse stats reuse   # init(a, read_only=false, preserve_file_stats=true), walk; init(b, read_only=true), walk; delete
                          # -> b after delete: mode=644 mtime=1000000000
./metareuse stats fresh   # new iterator, init(b, read_only=true), walk, delete -> b: mode=600 mtime=1200000000

The second init(b, read_only=false, preserve_file_stats=false) behaves the same (reuse-writable). If the first init() did not ask to preserve the attributes there is no difference (reuse-nopreserve).

Change

Clear has_stats in init() right after simple_iterator_free_guts_(), before it is set again for this file. With the change, b.flac keeps mode 0600 and its mtime in all modes. test_libFLAC passes (ctest libFLAC, ASan+UBSan build).

`FLAC__metadata_simple_iterator_init()` sets `iterator->has_stats` only
when the file is opened with `read_only == false` and
`preserve_file_stats == true`; otherwise it leaves the previous value.
When an iterator that was first initialized on file A with
`preserve_file_stats` is initialized again on file B (read-only, or
without `preserve_file_stats`), `has_stats` is still true and `stats`
still holds A's attributes. When B is closed
(`FLAC__metadata_simple_iterator_delete()` or the next `init()`),
`simple_iterator_free_guts_()` calls `set_file_stats_()` on B: B gets
A's permissions, modification time and owner/group.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 19:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

2 participants