diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index c670d48c981214ea6ccbacc3be1f4121d4d6c512..dd6f9b6c954459a182b341802a5adc77c949b09d 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -107,9 +107,7 @@ void put_buffer(ByteIOContext *s, const unsigned char *buf, int size)
     int len;
 
     while (size > 0) {
-        len = (s->buf_end - s->buf_ptr);
-        if (len > size)
-            len = size;
+        len = FFMIN(s->buf_end - s->buf_ptr, size);
         memcpy(s->buf_ptr, buf, len);
         s->buf_ptr += len;