aom: Restrict the range of duration to UINT32_MAX

From fdd6af31b1f6ea19464daf961b46ad6a5befd89f Mon Sep 17 00:00:00 2001
From: Wan-Teh Chang <[EMAIL REDACTED]>
Date: Thu, 14 Mar 2024 15:10:53 -0700
Subject: [PATCH] Restrict the range of duration to UINT32_MAX

Bug: aomedia:3510
Change-Id: I0487eb99204b46070dcca51aef08086722a9441b
---
 aom/aom_encoder.h     | 5 +++++
 aom/src/aom_encoder.c | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/aom/aom_encoder.h b/aom/aom_encoder.h
index 6a6254dafe..9bdadd6938 100644
--- a/aom/aom_encoder.h
+++ b/aom/aom_encoder.h
@@ -1044,6 +1044,11 @@ aom_fixed_buf_t *aom_codec_get_global_headers(aom_codec_ctx_t *ctx);
  *     Interface is not an encoder interface.
  * \retval #AOM_CODEC_INVALID_PARAM
  *     A parameter was NULL, the image format is unsupported, etc.
+ *
+ * \note
+ * `duration` is of the unsigned long type, which can be 32 or 64 bits.
+ * `duration` must be less than or equal to UINT32_MAX so that its range is
+ * independent of the size of unsigned long.
  */
 aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img,
                                  aom_codec_pts_t pts, unsigned long duration,
diff --git a/aom/src/aom_encoder.c b/aom/src/aom_encoder.c
index 70e0b75bcd..f188567b94 100644
--- a/aom/src/aom_encoder.c
+++ b/aom/src/aom_encoder.c
@@ -23,6 +23,7 @@
 #endif
 
 #include <limits.h>
+#include <stdint.h>
 #include <string.h>
 
 #include "aom/aom_encoder.h"
@@ -178,6 +179,10 @@ aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img,
   else if (img && ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) != 0) !=
                       ((ctx->init_flags & AOM_CODEC_USE_HIGHBITDEPTH) != 0)) {
     res = AOM_CODEC_INVALID_PARAM;
+#if ULONG_MAX > UINT32_MAX
+  } else if (duration > UINT32_MAX) {
+    res = AOM_CODEC_INVALID_PARAM;
+#endif
   } else {
     /* Execute in a normalized floating point environment, if the platform
      * requires it.