[FFmpeg-devel] [PATCH v2 1/2] avutil/random_seed: add av_random()

Marton Balint cus at passwd.hu
Wed Jul 5 01:18:22 EEST 2023



On Tue, 4 Jul 2023, James Almer wrote:

> Uses the existing code for av_get_random_seed() to return a buffer with
> cryptographically secure random data, or an error if none could be generated.
>
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
> libavutil/random_seed.c | 54 ++++++++++++++++++++++++++++-------------
> libavutil/random_seed.h | 12 +++++++++
> 2 files changed, 49 insertions(+), 17 deletions(-)
>
> diff --git a/libavutil/random_seed.c b/libavutil/random_seed.c
> index 66dd504ef0..0ed8f89cc6 100644
> --- a/libavutil/random_seed.c
> +++ b/libavutil/random_seed.c
> @@ -46,20 +46,22 @@
> #define TEST 0
> #endif
>
> -static int read_random(uint32_t *dst, const char *file)
> -{
> #if HAVE_UNISTD_H
> +static int read_random(uint8_t *dst, size_t len, const char *file)

Maybe it is cleaner if you also use ffmpeg error codes for this function, 
and no special -1, 1 or 0 return value.

> +{
>     int fd = avpriv_open(file, O_RDONLY);
> -    int err = -1;
> +    ssize_t err = -1;
>
> +    if (len > SSIZE_MAX)
> +        return -1;
>     if (fd == -1)
>         return -1;
> -    err = read(fd, dst, sizeof(*dst));
> +    err = read(fd, dst, len);

As Anton pointed out, this can read less than requested. I suggest using 
avpriv_fopen_utf8() and fread(), that should loop reading internally 
until the whole length is filled.

Regards,
Marton


More information about the ffmpeg-devel mailing list