Re: [PATCH] Fix concat_path() functions to work for edge-cases too.

Alejandro Colomar via Mutt-dev <[email protected]> Fri, 8 May 2026 15:39:31 +0200
Newsgroups gmane.mail.mutt.devel
Message-ID <af3nXaJ7MmgioVVm@devuan>
Hi Kevin, Rene, Derek,

On 2026-05-08T20:59:35+0800, Kevin J. McCarthy wrote:
> This fixes the edge cases tested by Derek Martin.
>   "foo/bar/" + "/baz"  gave: "foo/bar//baz" should be: "foo/bar/baz"
>   ""         + "/baz"  gave: "//baz"        should be "/bar"
>   "/"        + "/foo"  gave  "//foo"        should be "/foo"
> 
> All three bugs were triggered by a leading "/" in fname.
> 
> The one line strspn() fix is by Alejandro Colomar, but has also been
> applied to the lib.c version.
> 
> Many thanks to Derek Martin, for noticing the edge case failures,
> creating a suite of tests, and also working on proposed patches to fix
> the problem.  Also thanks to Rene Kita for his feedback and proposed
> version.
> 
> In the end, this was the simplest version that was easy to apply to
> both concat_path() functions.

Reviewed-by: Alejandro Colomar <[email protected]>

Thanks!

> ---
>  lib.c     | 2 ++
>  muttlib.c | 2 ++
>  2 files changed, 4 insertions(+)
> 
> diff --git a/lib.c b/lib.c
> index 44f0e869..649fdb3a 100644
> --- a/lib.c
> +++ b/lib.c
> @@ -763,6 +763,8 @@ char *mutt_concat_path(char *d, const char *dir, const char *fname, size_t l)
>    if (!*fname || (*dir && dir[strlen(dir)-1] == '/'))
>      fmt = "%s%s";
>  
> +  fname += strspn(fname, "/");
> +
>    snprintf(d, l, fmt, dir, fname);
>    return d;
>  }
> diff --git a/muttlib.c b/muttlib.c
> index 59a48378..a9fe8779 100644
> --- a/muttlib.c
> +++ b/muttlib.c
> @@ -1394,6 +1394,8 @@ void mutt_buffer_concat_path(BUFFER *d, const char *dir, const char *fname)
>    if (!*fname || (*dir && dir[strlen(dir)-1] == '/'))
>      fmt = "%s%s";
>  
> +  fname += strspn(fname, "/");
> +
>    mutt_buffer_printf(d, fmt, dir, fname);
>  }
>  
> -- 
> 2.54.0
> 

-- 
<https://www.alejandro-colomar.es>
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmn954kACgkQ64mZXMKQ
wqn6cRAArfBYCnwS/YAxt0tixymDoJnwxIEpsPnQNAKAxv186mrnNtBxsn/5vben
13J32xPRjPudecrfxPfy1eMEFfPyhNCbhLJzZK3Yz1IIFPJB9HvKaSKEXfDbI1ht
fwSwlNNW1tHiQCHi5/WqMow8z2BH3NZBQg7hQthns8xZO4ZErx8pTkeLnQu0SM78
DxhuHhwMF60TBtNaqxNjlO+/omYg9ECtyluYne92t7/kQwb/uBTE4+so+QMH4MoJ
plcnX9B5sPxDdoohUkM+L+8ZVn/yPTjfybHEWOcl3SdzAM5ExU5TuBqoS+QbO4oi
NcK5Nh/PUPiUErpo7QFIO9Acha2Mz5TctpxovnjUNpgyxWy0KE8wjxZ69+gpx+eM
TzzRDqK4/an6JjCouw7CnrH9RCE7pXFOCv9yL6oBQtN9gr1IRwqbPKFNS295jBJK
CHbupUBiypflCiUnP1Qole9p4cLSGiOdRCCHtufH5WMP4Ot/3ArKMJRSmWsLcz5q
UtxRL5pR7YlLdl1Q0IjkCj93EH4Hf7l8IHk0FEfLagT4wLF8KzViOGFQZyzr/RhU
2nv96UNBWCKAW4PLuo3xzZct3w6SQdUFLBO/kAGsqBapp1oF9EICP2DsUAJUmlr8
4dAvJAqFtUunmUtySG8C6u3/PDih/8/j+xiZq9Y9bgCfbePjZvA=
=WC71
-----END PGP SIGNATURE-----