[FFmpeg-devel] [PATCH 3/4] lavu/avstring: add av_append_path_component() funcion

Michael Niedermayer michaelni at gmx.at
Thu Mar 26 15:40:46 CET 2015


On Thu, Mar 26, 2015 at 01:25:19AM +0100, Mariusz Szczepańczyk wrote:
> From: Lukasz Marek <lukasz.m.luki2 at gmail.com>
> 
> Convinient function to build paths.
> ---
>  libavutil/avstring.c    | 43 +++++++++++++++++++++++++++++++++++++++++++
>  libavutil/avstring.h    | 10 ++++++++++
>  tests/ref/fate/avstring |  9 +++++++++
>  3 files changed, 62 insertions(+)
> 
> diff --git a/libavutil/avstring.c b/libavutil/avstring.c
> index 25c65b4..f105aa7 100644
> --- a/libavutil/avstring.c
> +++ b/libavutil/avstring.c
> @@ -269,6 +269,35 @@ const char *av_dirname(char *path)
>      return path;
>  }
>  
> +char *av_append_path_component(const char *path, const char *component)
> +{
> +    size_t p_len, c_len;
> +    char *fullpath;
> +
> +    if (!path)
> +        return component ? av_strdup(component) : NULL;
> +    if (!component)
> +        return av_strdup(path);
> +
> +    p_len = strlen(path);
> +    c_len = strlen(component);

> +    fullpath = malloc(p_len + c_len + 2);

av_malloc()


> +    if (fullpath) {
> +        if (p_len) {

> +            strcpy(fullpath, path);

av_strlcpy() is more robust/secure


> +            if (c_len) {
> +                if (fullpath[p_len - 1] != '/' && component[0] != '/')
> +                    fullpath[p_len++] = '/';
> +                else if (fullpath[p_len - 1] == '/' && component[0] == '/')
> +                    p_len--;
> +            }
> +        }
> +        strcpy(&fullpath[p_len], component);

av_strlcpy()

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Old school: Use the lowest level language in which you can solve the problem
            conveniently.
New school: Use the highest level language in which the latest supercomputer
            can solve the problem without the user falling asleep waiting.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20150326/d1e893ab/attachment.asc>


More information about the ffmpeg-devel mailing list