Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Sep 2019 14:31:44 +0000
From:      bugzilla-noreply@freebsd.org
To:        bugs@FreeBSD.org
Subject:   [Bug 240509] segfault in basename(3)
Message-ID:  <bug-240509-227-cmxZrI4Wjh@https.bugs.freebsd.org/bugzilla/>
In-Reply-To: <bug-240509-227@https.bugs.freebsd.org/bugzilla/>
References:  <bug-240509-227@https.bugs.freebsd.org/bugzilla/>

next in thread | previous in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D240509

Mateusz Guzik <mjg@FreeBSD.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mjg@FreeBSD.org

--- Comment #1 from Mateusz Guzik <mjg@FreeBSD.org> ---
basename is allowed to modify the passes buffer. This is documented on both
FreeBSD and Linux. In the attached example the string is immutable, an atte=
mpt
to modify it is undefined behavior and a crash (as observed) the likely
outcome.

The FreeBSD implementation happens to always at least add a NUL-byte which
triggers the crash here, while Linux one does not modify the string if ther=
e is
nothing to do.

The program will trivially crash if the string is modified to get basename =
to
operate, e.g. by adding trailing slashes: "/home/tags///".

Finally, the Linux manpage explicitly shows usage with a copied buffer:

       The following code snippet demonstrates the use of basename() and
dirname():
           char *dirc, *basec, *bname, *dname;
           char *path =3D "/etc/passwd";

           dirc =3D strdup(path);
           basec =3D strdup(path);
           dname =3D dirname(dirc);
           bname =3D basename(basec);
           printf("dirname=3D%s, basename=3D%s\n", dname, bname);


That said, there is no bug in the FreeBSD version and the code does not cra=
sh
on Linux because of a combination of an artifact of the implementation and =
the
passed argument. FreeBSD variant can be modified to match but there is real
benefit to it.

--=20
You are receiving this mail because:
You are the assignee for the bug.=



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-240509-227-cmxZrI4Wjh>