summaryrefslogtreecommitdiffstats
path: root/package/imlib2/0002-GIF-loader-Simplify-error-handling.patch
blob: c2b52547a4fec0ecdaa606f0cd1a3eff669c22cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
From 908a179726d010963f4fe1b57fb5f7bf590d7d64 Mon Sep 17 00:00:00 2001
From: Kim Woelders <kim@woelders.dk>
Date: Tue, 31 Dec 2013 18:13:45 +0100
Subject: [PATCH 2/5] GIF loader: Simplify error handling.

Also:
- Fix memory leak when image data allocation fails.
- Some aux data arrays may as well be const.
---
 src/modules/loaders/loader_gif.c | 80 ++++++++++++++++------------------------
 1 file changed, 32 insertions(+), 48 deletions(-)

diff --git a/src/modules/loaders/loader_gif.c b/src/modules/loaders/loader_gif.c
index d1c2ae2..a39c860 100644
--- a/src/modules/loaders/loader_gif.c
+++ b/src/modules/loaders/loader_gif.c
@@ -8,6 +8,9 @@ char
 load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
      char immediate_load)
 {
+   static const int    intoffset[] = { 0, 4, 2, 1 };
+   static const int    intjump[] = { 8, 8, 4, 2 };
+   int                 rc;
    DATA32             *ptr;
    GifFileType        *gif;
    GifRowType         *rows;
@@ -16,8 +19,6 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
    int                 i, j, done, bg, r, g, b, w = 0, h = 0;
    float               per = 0.0, per_inc;
    int                 last_per = 0, last_y = 0;
-   int                 intoffset[] = { 0, 4, 2, 1 };
-   int                 intjump[] = { 8, 8, 4, 2 };
    int                 transp;
    int                 fd;
 
@@ -49,6 +50,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
         return 0;
      }
 
+   rc = 0;                      /* Failure */
+
    do
      {
         if (DGifGetRecordType(gif, &rec) == GIF_ERROR)
@@ -66,37 +69,19 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
              w = gif->Image.Width;
              h = gif->Image.Height;
              if (!IMAGE_DIMENSIONS_OK(w, h))
-               {
-                  DGifCloseFile(gif);
-                  return 0;
-               }
-             rows = malloc(h * sizeof(GifRowType *));
+                goto quit2;
+
+             rows = calloc(h, sizeof(GifRowType *));
              if (!rows)
-               {
-                  DGifCloseFile(gif);
-                  return 0;
-               }
-             for (i = 0; i < h; i++)
-               {
-                  rows[i] = NULL;
-               }
+                goto quit2;
+
              for (i = 0; i < h; i++)
                {
                   rows[i] = malloc(w * sizeof(GifPixelType));
                   if (!rows[i])
-                    {
-                       DGifCloseFile(gif);
-                       for (i = 0; i < h; i++)
-                         {
-                            if (rows[i])
-                              {
-                                 free(rows[i]);
-                              }
-                         }
-                       free(rows);
-                       return 0;
-                    }
+                     goto quit;
                }
+
              if (gif->Image.Interlace)
                {
                   for (i = 0; i < 4; i++)
@@ -135,6 +120,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
           }
      }
    while (rec != TERMINATE_RECORD_TYPE);
+
    if (transp >= 0)
      {
         SET_FLAG(im->flags, F_HAS_ALPHA);
@@ -143,6 +129,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
      {
         UNSET_FLAG(im->flags, F_HAS_ALPHA);
      }
+
    /* set the format string member to the lower-case full extension */
    /* name for the format - so example names would be: */
    /* "png", "jpeg", "tiff", "ppm", "pgm", "pbm", "gif", "xpm" ... */
@@ -150,17 +137,15 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
    im->h = h;
    if (!im->format)
       im->format = strdup("gif");
+
    if (im->loader || immediate_load || progress)
      {
         bg = gif->SBackGroundColor;
         cmap = (gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap);
         im->data = (DATA32 *) malloc(sizeof(DATA32) * w * h);
         if (!im->data)
-          {
-             DGifCloseFile(gif);
-             free(rows);
-             return 0;
-          }
+           goto quit;
+
         ptr = im->data;
         per_inc = 100.0 / (((float)w) * h);
         for (i = 0; i < h; i++)
@@ -188,30 +173,29 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity,
                        last_per = (int)per;
                        if (!(progress(im, (int)per, 0, last_y, w, i)))
                          {
-                            DGifCloseFile(gif);
-                            for (i = 0; i < h; i++)
-                              {
-                                 free(rows[i]);
-                              }
-                            free(rows);
-                            return 2;
+                            rc = 2;
+                            goto quit;
                          }
                        last_y = i;
                     }
                }
           }
+
+        if (progress)
+           progress(im, 100, 0, last_y, w, h);
      }
-   if (progress)
-     {
-        progress(im, 100, 0, last_y, w, h);
-     }
-   DGifCloseFile(gif);
+
+   rc = 1;                      /* Success */
+
+ quit:
    for (i = 0; i < h; i++)
-     {
-        free(rows[i]);
-     }
+      free(rows[i]);
    free(rows);
-   return 1;
+
+ quit2:
+   DGifCloseFile(gif);
+
+   return rc;
 }
 
 void
-- 
2.3.1

OpenPOWER on IntegriCloud