0014-avoid-fixed-inf.patch revision 4d3acf4ec42bf6e838f9060103aff98fbf170794
1diff --git a/third_party/lcms2-2.6/src/cmsopt.c b/third_party/lcms2-2.6/src/cmsopt.c
2index 684910d..76de015 100644
3--- a/third_party/lcms2-2.6/src/cmsopt.c
4+++ b/third_party/lcms2-2.6/src/cmsopt.c
5@@ -1443,7 +1443,7 @@ void MatShaperEval16(register const cmsUInt16Number In[],
6 
7 // This table converts from 8 bits to 1.14 after applying the curve
8 static
9-void FillFirstShaper(cmsS1Fixed14Number* Table, cmsToneCurve* Curve)
10+cmsBool FillFirstShaper(cmsS1Fixed14Number* Table, cmsToneCurve* Curve)
11 {
12     int i;
13     cmsFloat32Number R, y;
14@@ -1452,14 +1452,17 @@ void FillFirstShaper(cmsS1Fixed14Number* Table, cmsToneCurve* Curve)
15 
16         R   = (cmsFloat32Number) (i / 255.0);
17         y   = cmsEvalToneCurveFloat(Curve, R);
18+        if (isinf(y))
19+            return FALSE;
20 
21         Table[i] = DOUBLE_TO_1FIXED14(y);
22     }
23+    return TRUE;
24 }
25 
26 // This table converts form 1.14 (being 0x4000 the last entry) to 8 bits after applying the curve
27 static
28-void FillSecondShaper(cmsUInt16Number* Table, cmsToneCurve* Curve, cmsBool Is8BitsOutput)
29+cmsBool FillSecondShaper(cmsUInt16Number* Table, cmsToneCurve* Curve, cmsBool Is8BitsOutput)
30 {
31     int i;
32     cmsFloat32Number R, Val;
33@@ -1468,6 +1471,8 @@ void FillSecondShaper(cmsUInt16Number* Table, cmsToneCurve* Curve, cmsBool Is8Bi
34 
35         R   = (cmsFloat32Number) (i / 16384.0);
36         Val = cmsEvalToneCurveFloat(Curve, R);    // Val comes 0..1.0
37+        if (isinf(Val))
38+            return FALSE;
39 
40         if (Is8BitsOutput) {
41 
42@@ -1482,6 +1487,7 @@ void FillSecondShaper(cmsUInt16Number* Table, cmsToneCurve* Curve, cmsBool Is8Bi
43         }
44         else Table[i]  = _cmsQuickSaturateWord(Val * 65535.0);
45     }
46+    return TRUE;
47 }
48 
49 // Compute the matrix-shaper structure
50@@ -1499,13 +1505,19 @@ cmsBool SetMatShaper(cmsPipeline* Dest, cmsToneCurve* Curve1[3], cmsMAT3* Mat, c
51     p -> ContextID = Dest -> ContextID;
52 
53     // Precompute tables
54-    FillFirstShaper(p ->Shaper1R, Curve1[0]);
55-    FillFirstShaper(p ->Shaper1G, Curve1[1]);
56-    FillFirstShaper(p ->Shaper1B, Curve1[2]);
57+    if (!FillFirstShaper(p ->Shaper1R, Curve1[0]))
58+        goto Error;
59+    if (!FillFirstShaper(p ->Shaper1G, Curve1[1]))
60+        goto Error;
61+    if (!FillFirstShaper(p ->Shaper1B, Curve1[2]))
62+        goto Error;
63 
64-    FillSecondShaper(p ->Shaper2R, Curve2[0], Is8Bits);
65-    FillSecondShaper(p ->Shaper2G, Curve2[1], Is8Bits);
66-    FillSecondShaper(p ->Shaper2B, Curve2[2], Is8Bits);
67+    if (!FillSecondShaper(p ->Shaper2R, Curve2[0], Is8Bits))
68+        goto Error;
69+    if (!FillSecondShaper(p ->Shaper2G, Curve2[1], Is8Bits))
70+        goto Error;
71+    if (!FillSecondShaper(p ->Shaper2B, Curve2[2], Is8Bits))
72+        goto Error;
73 
74     // Convert matrix to nFixed14. Note that those values may take more than 16 bits as
75     for (i=0; i < 3; i++) {
76@@ -1531,6 +1543,9 @@ cmsBool SetMatShaper(cmsPipeline* Dest, cmsToneCurve* Curve1[3], cmsMAT3* Mat, c
77     // Fill function pointers
78     _cmsPipelineSetOptimizationParameters(Dest, MatShaperEval16, (void*) p, FreeMatShaper, DupMatShaper);
79     return TRUE;
80+Error:
81+    _cmsFree(Dest->ContextID, p);
82+    return FALSE;
83 }
84 
85 //  8 bits on input allows matrix-shaper boot up to 25 Mpixels per second on RGB. That's fast!
86@@ -1606,7 +1621,8 @@ cmsBool OptimizeMatrixShaper(cmsPipeline** Lut, cmsUInt32Number Intent, cmsUInt3
87         *dwFlags |= cmsFLAGS_NOCACHE;
88 
89         // Setup the optimizarion routines
90-        SetMatShaper(Dest, mpeC1 ->TheCurves, &res, (cmsVEC3*) Data2 ->Offset, mpeC2->TheCurves, OutputFormat);
91+        if (!SetMatShaper(Dest, mpeC1 ->TheCurves, &res, (cmsVEC3*) Data2 ->Offset, mpeC2->TheCurves, OutputFormat))
92+            goto Error;
93     }
94 
95     cmsPipelineFree(Src);
96