[FFmpeg-devel] [PATCH v3 1/3] avutil/random_seed: use fread() in read_random()

Rémi Denis-Courmont remi at remlab.net
Wed Jul 5 18:34:51 EEST 2023


Le keskiviikkona 5. heinäkuuta 2023, 2.26.12 EEST James Almer a écrit :
> This ensures the requested amount of bytes is read.

You're moving the problem though. Now, you may read more than necessary (and 
block longer than necessary) due to stdio internal buffering, which you did not 
disable.

> Also remove /dev/random as it's no longer necessary.
> 
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
>  libavutil/random_seed.c | 23 ++++++++++++-----------
>  1 file changed, 12 insertions(+), 11 deletions(-)
> 
> diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
> index 66dd504ef0..a51149235b 100644
> --- a/libavutil/random_seed.c
> +++ b/libavutil/random_seed.c
> @@ -49,17 +49,20 @@
>  static int read_random(uint32_t *dst, const char *file)
>  {
>  #if HAVE_UNISTD_H
> -    int fd = avpriv_open(file, O_RDONLY);
> -    int err = -1;
> +    FILE *fp = avpriv_fopen_utf8(file, "r");
> +    size_t err;
> 
> -    if (fd == -1)
> -        return -1;
> -    err = read(fd, dst, sizeof(*dst));
> -    close(fd);
> +    if (!fp)
> +        return AVERROR_UNKNOWN;
> +    err = fread(dst, 1, sizeof(*dst), fp);
> +    fclose(fp);
> 
> -    return err;
> +    if (err != sizeof(*dst))
> +        return AVERROR_UNKNOWN;
> +
> +    return 0;
>  #else
> -    return -1;
> +    return AVERROR(ENOSYS);
>  #endif
>  }
> 
> @@ -138,9 +141,7 @@ uint32_t av_get_random_seed(void)
>      return arc4random();
>  #endif
> 
> -    if (read_random(&seed, "/dev/urandom") == sizeof(seed))
> -        return seed;
> -    if (read_random(&seed, "/dev/random")  == sizeof(seed))
> +    if (!read_random(&seed, "/dev/urandom"))
>          return seed;
>      return get_generic_seed();
>  }


-- 
雷米‧德尼-库尔蒙
http://www.remlab.net/





More information about the ffmpeg-devel mailing list