diff --git a/libavformat/utils.c b/libavformat/utils.c
index 9ff6284dfb..fef57a26cf 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -4582,8 +4582,11 @@ int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number
         if (c == '%') {
             do {
                 nd = 0;
-                while (av_isdigit(*p))
+                while (av_isdigit(*p)) {
+                    if (nd >= INT_MAX / 10 - 255)
+                        goto fail;
                     nd = nd * 10 + *p++ - '0';
+                }
                 c = *p++;
             } while (av_isdigit(c));