babl: avoid using half initialized LUTs when used from threads
authorØyvind Kolås <pippin@gimp.org>
Sun, 23 Jan 2022 22:35:34 +0000 (23:35 +0100)
committerØyvind Kolås <pippin@gimp.org>
Sun, 23 Jan 2022 22:35:34 +0000 (23:35 +0100)
babl/babl-fish-path.c

index e372d4f43dd81f023a809c909a6d8b472d0bb174..7d9e825803b032fa963c30c5056d7820c977aba1 100644 (file)
@@ -721,8 +721,7 @@ babl_fish_path_process (const Babl *babl,
      ((Babl*)babl)->fish.pixels += n;
      if (!lut && babl->fish.pixels > 256 * 128)
      {
-       ((Babl*)babl)->fish_path.u8_lut = malloc (256 * 256 * 256 * 4);
-       lut = (uint32_t*)babl->fish_path.u8_lut;
+       lut = malloc (256 * 256 * 256 * 4);
        if (babl->fish_path.source_bpp == 8)
        {
           uint64_t *lut_in = malloc (256 * 256  * 256 * 8);
@@ -758,6 +757,18 @@ babl_fish_path_process (const Babl *babl,
                                 babl->fish_path.dest_bpp,
                                 256*256*256);
        }
+       // XXX : there is still a micro race, if lost we should only
+       // leak a LUT not produce wrong results.
+       if (babl->fish_path.u8_lut == NULL)
+       {
+         (((Babl*)babl)->fish_path.u8_lut) = (uint8_t*)lut;
+
+       }
+       else
+       {
+         free (lut);
+         lut = (uint32_t*)babl->fish_path.u8_lut;
+       }
      }
      if (lut)
      {