diff --git a/h3.c b/h3.c index 5c921763..e2619587 100644 --- a/h3.c +++ b/h3.c @@ -510,7 +510,8 @@ static int h3_valid_params(h3_ctx *ctx, const h3_params *params) { return 0; } } - if (params->frames < 5 || h3_align_frame_count(params->frames) > 362) { + int aligned_frames = h3_align_frame_count(params->frames); + if (params->frames < 5 || !aligned_frames || aligned_frames > 362) { h3_set_error(ctx, "frames must align within the released 5..362 range"); return 0; } diff --git a/h3_host.c b/h3_host.c index a04a2a0a..94d0409e 100644 --- a/h3_host.c +++ b/h3_host.c @@ -13,11 +13,11 @@ static const int h3_frame_per_token[5] = {1, 4, 4, 4, 4}; static const double h3_frame_rescale = 5.0 / 3.0; int h3_align_frame_count(int requested) { - int value = requested < 5 ? 5 : requested; - int remainder = (value - 5) % 17; + int64_t value = requested < 5 ? 5 : requested; + int64_t remainder = (value - 5) % 17; if (remainder < 0) remainder += 17; if (remainder != 0) value += 17 - remainder; - return value; + return value > INT_MAX ? 0 : (int)value; } int h3_video_latent_t(int frame_count) { diff --git a/h3_host.h b/h3_host.h index f71eded7..3fbfe4a7 100644 --- a/h3_host.h +++ b/h3_host.h @@ -92,6 +92,7 @@ typedef struct { int has_spare; } h3_rng; +/* Return the next legal frame count, or zero if it cannot fit in an int. */ int h3_align_frame_count(int requested); int h3_video_latent_t(int frame_count); int h3_video_encoder_latent_t(int frame_count); diff --git a/tests/test_h3.c b/tests/test_h3.c index 3f66fa32..69a50fb3 100644 --- a/tests/test_h3.c +++ b/tests/test_h3.c @@ -37,6 +37,7 @@ static void test_temporal_and_canvas(void) { CHECK(got.video_t == cases[index].video_t); CHECK(got.audio_t == cases[index].audio_t); } + CHECK(h3_align_frame_count(INT32_MAX) == 0); CHECK(h3_video_encoder_latent_t(1) == 1); CHECK(h3_video_encoder_latent_t(5) == 2); CHECK(h3_video_encoder_latent_t(22) == 6);