Commit 9f629b91dd for aom

commit 9f629b91dd1ba8d1c02b6e5ff84de18261c20f55
Author: Alejandro Pacheco <alepacheco99@gmail.com>
Date:   Wed Oct 14 10:00:11 2026 +0200

    grain_table: check the point counts and AR lag read from the file

    grain_table_entry_read() stored num_y_points, num_cb_points,
    num_cr_points and ar_coeff_lag from the film grain table file and used
    them as loop bounds for fscanf() into the fixed size arrays of
    aom_film_grain_t, so a table with larger values wrote past the
    heap-allocated entry.

    Reject counts larger than the arrays and an ar_coeff_lag outside 0..3
    as soon as each value is read, and free the table when the read fails
    so that a partially read entry is never used.

    Bug: 573427857
    Change-Id: I0f666dfdd5bcd2a1c1c1b75f4abee0e4cfe16bf7

diff --git a/aom_dsp/grain_table.c b/aom_dsp/grain_table.c
index 97d2465bb8..a08b991514 100644
--- a/aom_dsp/grain_table.c
+++ b/aom_dsp/grain_table.c
@@ -64,10 +64,19 @@ static void grain_table_entry_read(FILE *file,
                          "Unable to read entry params. Read %d != 12",
                          num_read);
     }
+    if (pars->ar_coeff_lag < 0 || pars->ar_coeff_lag > 3) {
+      aom_internal_error(error_info, AOM_CODEC_ERROR, "Invalid ar_coeff_lag %d",
+                         pars->ar_coeff_lag);
+    }
     if (1 != fscanf(file, "\tsY %d ", &pars->num_y_points)) {
       aom_internal_error(error_info, AOM_CODEC_ERROR,
                          "Unable to read num y points");
     }
+    if (pars->num_y_points < 0 ||
+        pars->num_y_points > NELEMENTS(pars->scaling_points_y)) {
+      aom_internal_error(error_info, AOM_CODEC_ERROR,
+                         "Invalid number of y points %d", pars->num_y_points);
+    }
     for (int i = 0; i < pars->num_y_points; ++i) {
       if (2 != fscanf(file, "%d %d", &pars->scaling_points_y[i][0],
                       &pars->scaling_points_y[i][1])) {
@@ -79,6 +88,11 @@ static void grain_table_entry_read(FILE *file,
       aom_internal_error(error_info, AOM_CODEC_ERROR,
                          "Unable to read num cb points");
     }
+    if (pars->num_cb_points < 0 ||
+        pars->num_cb_points > NELEMENTS(pars->scaling_points_cb)) {
+      aom_internal_error(error_info, AOM_CODEC_ERROR,
+                         "Invalid number of cb points %d", pars->num_cb_points);
+    }
     for (int i = 0; i < pars->num_cb_points; ++i) {
       if (2 != fscanf(file, "%d %d", &pars->scaling_points_cb[i][0],
                       &pars->scaling_points_cb[i][1])) {
@@ -90,6 +104,11 @@ static void grain_table_entry_read(FILE *file,
       aom_internal_error(error_info, AOM_CODEC_ERROR,
                          "Unable to read num cr points");
     }
+    if (pars->num_cr_points < 0 ||
+        pars->num_cr_points > NELEMENTS(pars->scaling_points_cr)) {
+      aom_internal_error(error_info, AOM_CODEC_ERROR,
+                         "Invalid number of cr points %d", pars->num_cr_points);
+    }
     for (int i = 0; i < pars->num_cr_points; ++i) {
       if (2 != fscanf(file, "%d %d", &pars->scaling_points_cr[i][0],
                       &pars->scaling_points_cr[i][1])) {
@@ -305,6 +324,9 @@ aom_codec_err_t aom_film_grain_table_read(

       grain_table_entry_read(file, error_info, entry);
     }
+  } else {
+    // Do not leave a partially read entry in the table.
+    aom_film_grain_table_free(t);
   }

   fclose(file);
diff --git a/test/film_grain_table_test.cc b/test/film_grain_table_test.cc
index 33a709785f..d97742da68 100644
--- a/test/film_grain_table_test.cc
+++ b/test/film_grain_table_test.cc
@@ -209,6 +209,54 @@ TEST_F(FilmGrainTableIOTest, ReadTruncatedFile) {
   EXPECT_EQ(0, remove(grain_file.c_str()));
 }

+// Counts read from the file must be checked against the sizes of the arrays
+// they index, and a table that fails to read must be left empty.
+TEST_F(FilmGrainTableIOTest, ReadCountLimits) {
+  const auto repeat = [](const char *s, int n) {
+    std::string out;
+    for (int i = 0; i < n; ++i) out += s;
+    return out;
+  };
+  const auto make_entry = [&](int lag, int num_y, int num_cb, int num_cr) {
+    const int num_pos = 2 * lag * (lag + 1);
+    return "E 0 1000 1 7391 1\np " + std::to_string(lag) +
+           " 7 0 11 0 1 128 192 256 128 192 256\n\tsY " +
+           std::to_string(num_y) + repeat(" 64 64", num_y) + "\n\tsCb " +
+           std::to_string(num_cb) + repeat(" 64 64", num_cb) + "\n\tsCr " +
+           std::to_string(num_cr) + repeat(" 64 64", num_cr) + "\n\tcY" +
+           repeat(" 0", num_pos) + "\n\tcCb" + repeat(" 0", num_pos + 1) +
+           "\n\tcCr" + repeat(" 0", num_pos + 1) + "\n";
+  };
+  const struct {
+    int lag, num_y, num_cb, num_cr;
+    bool valid;
+  } kCases[] = {
+    { 3, 14, 10, 10, true }, { 4, 0, 0, 0, false },   { 20, 0, 0, 0, false },
+    { -1, 0, 0, 0, false },  { 0, 15, 0, 0, false },  { 0, 200, 0, 0, false },
+    { 0, 0, 11, 0, false },  { 0, 0, 200, 0, false }, { 0, 0, 0, 11, false },
+    { 0, 0, 0, 200, false },
+  };
+  for (const auto &c : kCases) {
+    aom_film_grain_table_t table;
+    memset(&table, 0, sizeof(table));
+    libaom_test::TempOutFile grain_file;
+    ASSERT_NE(grain_file.file(), nullptr);
+    const std::string contents =
+        "filmgrn1\n" + make_entry(c.lag, c.num_y, c.num_cb, c.num_cr);
+    fwrite(contents.data(), contents.size(), 1, grain_file.file());
+    fflush(grain_file.file());
+    EXPECT_EQ(c.valid ? AOM_CODEC_OK : AOM_CODEC_ERROR,
+              aom_film_grain_table_read(&table, grain_file.file_name().c_str(),
+                                        &error_))
+        << "lag " << c.lag << " points " << c.num_y << " " << c.num_cb << " "
+        << c.num_cr;
+    if (!c.valid) {
+      EXPECT_EQ(table.head, nullptr);
+    }
+    aom_film_grain_table_free(&table);
+  }
+}
+
 TEST_F(FilmGrainTableIOTest, RoundTripReadWrite) {
   aom_film_grain_table_t table;
   memset(&table, 0, sizeof(table));