c2b52547a4fec0ecdaa606f0cd1a3eff669c22cd
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / package / imlib2 / 0002-GIF-loader-Simplify-error-handling.patch
1 From 908a179726d010963f4fe1b57fb5f7bf590d7d64 Mon Sep 17 00:00:00 2001
2 From: Kim Woelders <kim@woelders.dk>
3 Date: Tue, 31 Dec 2013 18:13:45 +0100
4 Subject: [PATCH 2/5] GIF loader: Simplify error handling.
5
6 Also:
7 - Fix memory leak when image data allocation fails.
8 - Some aux data arrays may as well be const.
9 ---
10  src/modules/loaders/loader_gif.c | 80 ++++++++++++++++------------------------
11  1 file changed, 32 insertions(+), 48 deletions(-)
12
13 diff --git a/src/modules/loaders/loader_gif.c b/src/modules/loaders/loader_gif.c
14 index d1c2ae2..a39c860 100644
15 --- a/src/modules/loaders/loader_gif.c
16 +++ b/src/modules/loaders/loader_gif.c
17 @@ -8,6 +8,9 @@ char
18  load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
19       char immediate_load)
20  {
21 +   static const int    intoffset[] = { 0, 4, 2, 1 };
22 +   static const int    intjump[] = { 8, 8, 4, 2 };
23 +   int                 rc;
24     DATA32             *ptr;
25     GifFileType        *gif;
26     GifRowType         *rows;
27 @@ -16,8 +19,6 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
28     int                 i, j, done, bg, r, g, b, w = 0, h = 0;
29     float               per = 0.0, per_inc;
30     int                 last_per = 0, last_y = 0;
31 -   int                 intoffset[] = { 0, 4, 2, 1 };
32 -   int                 intjump[] = { 8, 8, 4, 2 };
33     int                 transp;
34     int                 fd;
35  
36 @@ -49,6 +50,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
37          return 0;
38       }
39  
40 +   rc = 0;                      /* Failure */
41 +
42     do
43       {
44          if (DGifGetRecordType(gif, &rec) == GIF_ERROR)
45 @@ -66,37 +69,19 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
46               w = gif->Image.Width;
47               h = gif->Image.Height;
48               if (!IMAGE_DIMENSIONS_OK(w, h))
49 -               {
50 -                  DGifCloseFile(gif);
51 -                  return 0;
52 -               }
53 -             rows = malloc(h * sizeof(GifRowType *));
54 +                goto quit2;
55 +
56 +             rows = calloc(h, sizeof(GifRowType *));
57               if (!rows)
58 -               {
59 -                  DGifCloseFile(gif);
60 -                  return 0;
61 -               }
62 -             for (i = 0; i < h; i++)
63 -               {
64 -                  rows[i] = NULL;
65 -               }
66 +                goto quit2;
67 +
68               for (i = 0; i < h; i++)
69                 {
70                    rows[i] = malloc(w * sizeof(GifPixelType));
71                    if (!rows[i])
72 -                    {
73 -                       DGifCloseFile(gif);
74 -                       for (i = 0; i < h; i++)
75 -                         {
76 -                            if (rows[i])
77 -                              {
78 -                                 free(rows[i]);
79 -                              }
80 -                         }
81 -                       free(rows);
82 -                       return 0;
83 -                    }
84 +                     goto quit;
85                 }
86 +
87               if (gif->Image.Interlace)
88                 {
89                    for (i = 0; i < 4; i++)
90 @@ -135,6 +120,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
91            }
92       }
93     while (rec != TERMINATE_RECORD_TYPE);
94 +
95     if (transp >= 0)
96       {
97          SET_FLAG(im->flags, F_HAS_ALPHA);
98 @@ -143,6 +129,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
99       {
100          UNSET_FLAG(im->flags, F_HAS_ALPHA);
101       }
102 +
103     /* set the format string member to the lower-case full extension */
104     /* name for the format - so example names would be: */
105     /* "png", "jpeg", "tiff", "ppm", "pgm", "pbm", "gif", "xpm" ... */
106 @@ -150,17 +137,15 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
107     im->h = h;
108     if (!im->format)
109        im->format = strdup("gif");
110 +
111     if (im->loader || immediate_load || progress)
112       {
113          bg = gif->SBackGroundColor;
114          cmap = (gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap);
115          im->data = (DATA32 *) malloc(sizeof(DATA32) * w * h);
116          if (!im->data)
117 -          {
118 -             DGifCloseFile(gif);
119 -             free(rows);
120 -             return 0;
121 -          }
122 +           goto quit;
123 +
124          ptr = im->data;
125          per_inc = 100.0 / (((float)w) * h);
126          for (i = 0; i < h; i++)
127 @@ -188,30 +173,29 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
128                         last_per = (int)per;
129                         if (!(progress(im, (int)per, 0, last_y, w, i)))
130                           {
131 -                            DGifCloseFile(gif);
132 -                            for (i = 0; i < h; i++)
133 -                              {
134 -                                 free(rows[i]);
135 -                              }
136 -                            free(rows);
137 -                            return 2;
138 +                            rc = 2;
139 +                            goto quit;
140                           }
141                         last_y = i;
142                      }
143                 }
144            }
145 +
146 +        if (progress)
147 +           progress(im, 100, 0, last_y, w, h);
148       }
149 -   if (progress)
150 -     {
151 -        progress(im, 100, 0, last_y, w, h);
152 -     }
153 -   DGifCloseFile(gif);
154 +
155 +   rc = 1;                      /* Success */
156 +
157 + quit:
158     for (i = 0; i < h; i++)
159 -     {
160 -        free(rows[i]);
161 -     }
162 +      free(rows[i]);
163     free(rows);
164 -   return 1;
165 +
166 + quit2:
167 +   DGifCloseFile(gif);
168 +
169 +   return rc;
170  }
171  
172  void
173 -- 
174 2.3.1
175