Re: [PATCH v2] btrfs: send: fix is_current_inode_path() to avoid path resets for common prefixes

Filipe Manana <[email protected]>
Newsgroups org.kernel.vger.linux-btrfs
Message-ID <CAL3q7H4DGWe54UmJb4P+p3JZEwP=iMXjUQxXjQvghjRexA2f+g@mail.gmail.com>
On Tue, Jun 23, 2026 at 11:38 PM Qu Wenruo <[email protected]> wrote:
>
>
>
> 在 2026/6/16 02:44, [email protected] 写道:
> > From: Filipe Manana <[email protected]>
> >
> > In case the current inode's path is a prefix of the given path, the helper
> > is_current_inode_path() will return true, which causes the single caller
> > to reset the current inode's path. While this is not a functional issue,
> > it makes the caller recompute the current inode's path later. It could
> > also become a problem in the future in case get new callers for
> > is_current_inode_path() in more sensitive contexts.
> >
> > Example: the current inode path is "/foo/bar" and the path we compare
> > against is "/foo/bar_xyz".
> >
> > Fix this by returning true only if we have exact matches.
> >
> > Signed-off-by: Filipe Manana <[email protected]>
> > ---
> >
> > V2: Simplify and avoid false positives in case of something like
> >      "/foo/bar" vs "foo/bar/xyz".
> >
> >   fs/btrfs/send.c | 9 ++++++++-
> >   1 file changed, 8 insertions(+), 1 deletion(-)
> >
> > diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
> > index a3c8f2d889bb..d664e470fd77 100644
> > --- a/fs/btrfs/send.c
> > +++ b/fs/btrfs/send.c
> > @@ -627,7 +627,14 @@ static inline bool is_current_inode_path(const struct send_ctx *sctx,
> >   {
> >       const struct fs_path *cur = &sctx->cur_inode_path;
> >
> > -     return (strncmp(path->start, cur->start, fs_path_len(cur)) == 0);
> > +     /*
> > +      * Path may be a prefix of the current inode's path, so return false if
> > +      * the lengths are different.
> > +      */
> > +     if (fs_path_len(path) != fs_path_len(cur))
> > +             return false;
> > +
> > +     return (strcmp(path->start, cur->start) == 0);
>
> Can we just use strcmp without string length check? It looks like
> fs_path is always NUL terminated.
>
> E.g. fs_path_prepare_for_add() always reset *p->end to 0.
>
> So we can just use strcmp() to compare two NUL terminated paths.

Right, it's safe here. Updated in for-next, thanks.

>
> Thanks,
> Qu
>
> >   }
> >
> >   static struct btrfs_path *alloc_path_for_send(void)
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.