[PATCH v1 1/2] tools: mkimage: fix get_basename crash on paths with dotted directories

Marek Vasut marek.vasut at mailbox.org
Thu May 21 18:17:05 CEST 2026


On 5/21/26 6:12 PM, Quentin Schulz wrote:

Hello Quentin,

>>> +    p = strrchr(start, '.');
>>> +    end = p ? p : start + strlen(start);
>>>       len = end - start;
>>>       if (len >= size)
>>>           len = size - 1;
>>
>> Why not call basename(3) directly in here ? Why reimplement it ?
> 
> """
> Warning: there are two different functions basename() - see below.
> 
> [...]
> 
> Notes
> 
> There are two different versions of basename() - the POSIX version 
> described above, and the GNU version, which one gets after
> 
>      #define _GNU_SOURCE         /* See feature_test_macros(7) */
>      #include <string.h>
> 
> The GNU version never modifies its argument, and returns the empty 
> string when path has a trailing slash, and in particular also when it is 
> "/". There is no GNU version of dirname().
> 
> With glibc, one gets the POSIX version of basename() when <libgen.h> is 
> included, and the GNU version otherwise.
> 
> Bugs
> 
> In the glibc implementation of the POSIX versions of these functions 
> they modify their argument, and segfault when called with a static 
> string like "/usr/". Before glibc 2.2.1, the glibc version of dirname() 
> did not correctly handle pathnames with trailing '/' characters, and 
> generated a segfault if given a NULL argument.
> """
> 
> Not very reassuring, tbh.

Writing our own variant with its own set of bugs is worse than using a 
common implementation which has the bugs removed over time due to effort 
of the various users.

If basename(3) is not an option, is there another common alternative ?


More information about the U-Boot mailing list