Skip to content

Commit a0259d5

Browse files
committed
Fix various potential OOM crashes.
There were several potential OOM crashes in __imlib_ListFilters(), __imlib_ListLoaders() and __imlib_TrimLoaderList(). The fix of __imlib_TrimLoaderList() is from patch by Yuriy M. Kaminskiy <yumkam@gmail.com>.
1 parent 5b54980 commit a0259d5

File tree

6 files changed

+93
-125
lines changed

6 files changed

+93
-125
lines changed

src/lib/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ image.c \
4747
image.h \
4848
line.c \
4949
loaderpath.h \
50+
modules.c \
5051
polygon.c \
5152
rectangle.c \
5253
rend.c \

src/lib/dynamic_filters.c

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
#include "dynamic_filters.h"
1616
#include "file.h"
1717
#include "image.h"
18-
#include "loaderpath.h"
1918
#include "script.h"
2019

21-
static char **__imlib_ListFilters(int *num_ret);
22-
2320
static ImlibExternalFilter *filters = NULL;
2421
static int dyn_initialised = 0;
2522

@@ -101,7 +98,7 @@ __imlib_dynamic_filters_init()
10198
#ifdef FDEBUG
10299
printf("DEBUG: Loading Filters\n");
103100
#endif
104-
list = __imlib_ListFilters(&num_filters);
101+
list = __imlib_ListModules("filters", &num_filters);
105102
for (i = num_filters - 1; i >= 0; i--)
106103
{
107104
tptr = NULL;
@@ -150,40 +147,3 @@ __imlib_get_dynamic_filter(char *name)
150147
}
151148
return NULL;
152149
}
153-
154-
/* loader dir */
155-
static char **
156-
__imlib_ListFilters(int *num_ret)
157-
{
158-
char **list = NULL, **l, *s;
159-
int num, i, pi = 0;
160-
161-
*num_ret = 0;
162-
/* same for system loader path */
163-
s = (char *)malloc(sizeof(SYS_LOADERS_PATH) + 8 + 1);
164-
sprintf(s, SYS_LOADERS_PATH "/filters");
165-
l = __imlib_FileDir(s, &num);
166-
if (num > 0)
167-
{
168-
*num_ret += num;
169-
list = realloc(list, sizeof(char *) * *num_ret);
170-
for (i = 0; i < num; i++)
171-
{
172-
s = (char *)realloc(s,
173-
sizeof(SYS_LOADERS_PATH) + 9 + strlen(l[i]) +
174-
1);
175-
sprintf(s, SYS_LOADERS_PATH "/filters/%s", l[i]);
176-
list[pi + i] = strdup(s);
177-
}
178-
__imlib_FileFreeDirList(l, num);
179-
}
180-
free(s);
181-
182-
/* List currently contains *everything in there* we need to weed out
183-
* the .so, .la, .a versions of the same loader or whatever else.
184-
* dlopen can take an extension-less name and do the Right Thing
185-
* with it, so that's what we'll give it. */
186-
list = __imlib_TrimLoaderList(list, num_ret);
187-
188-
return list;
189-
}

src/lib/file.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ int __imlib_IsRealFile(const char *s);
2020

2121
int __imlib_ItemInList(char **list, int size, char *item);
2222

23+
char **__imlib_ListModules(const char *what, int *num_ret);
24+
2325
#endif

src/lib/image.c

Lines changed: 1 addition & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -594,88 +594,6 @@ __imlib_ProduceLoader(char *file)
594594
return l;
595595
}
596596

597-
/* list all the filenames of loaders in the system loaders dir and the user */
598-
/* loader dir */
599-
static char **
600-
__imlib_ListLoaders(int *num_ret)
601-
{
602-
char **list = NULL, **l, *s;
603-
int num, i, pi = 0;
604-
605-
*num_ret = 0;
606-
/* same for system loader path */
607-
s = (char *)malloc(sizeof(SYS_LOADERS_PATH) + 8 + 1);
608-
sprintf(s, SYS_LOADERS_PATH "/loaders");
609-
l = __imlib_FileDir(s, &num);
610-
if (num > 0)
611-
{
612-
*num_ret += num;
613-
list = realloc(list, sizeof(char *) * *num_ret);
614-
615-
for (i = 0; i < num; i++)
616-
{
617-
s = (char *)realloc(s,
618-
sizeof(SYS_LOADERS_PATH) + 9 + strlen(l[i]) +
619-
1);
620-
sprintf(s, SYS_LOADERS_PATH "/loaders/%s", l[i]);
621-
list[pi + i] = strdup(s);
622-
}
623-
__imlib_FileFreeDirList(l, num);
624-
}
625-
free(s);
626-
627-
/* List currently contains *everything in there* we need to weed out
628-
* the .so, .la, .a versions of the same loader or whatever else.
629-
* dlopen can take an extension-less name and do the Right Thing
630-
* with it, so that's what we'll give it. */
631-
list = __imlib_TrimLoaderList(list, num_ret);
632-
633-
return list;
634-
}
635-
636-
char **
637-
__imlib_TrimLoaderList(char **list, int *num)
638-
{
639-
int i, n, size = 0;
640-
641-
char **ret = NULL;
642-
643-
if (!list)
644-
return NULL;
645-
if (*num == 0)
646-
return list;
647-
648-
n = *num;
649-
650-
for (i = 0; i < n; i++)
651-
{
652-
char *ext;
653-
654-
if (!list[i])
655-
continue;
656-
ext = strrchr(list[i], '.');
657-
if ((ext) && (
658-
#ifdef __CYGWIN__
659-
(!strcasecmp(ext, ".dll")) ||
660-
#endif
661-
(!strcasecmp(ext, ".so"))))
662-
{
663-
/* Don't add the same loader multiple times... */
664-
if (!__imlib_ItemInList(ret, size, list[i]))
665-
{
666-
ret = realloc(ret, sizeof(char *) * (size + 1));
667-
668-
ret[size++] = strdup(list[i]);
669-
}
670-
}
671-
if (list[i])
672-
free(list[i]);
673-
}
674-
free(list);
675-
*num = size;
676-
return ret;
677-
}
678-
679597
/* fre the struct for a loader and close its dlopen'd handle */
680598
static void
681599
__imlib_ConsumeLoader(ImlibLoader * l)
@@ -753,7 +671,7 @@ __imlib_LoadAllLoaders(void)
753671
char **list;
754672

755673
/* list all the loaders imlib can find */
756-
list = __imlib_ListLoaders(&num);
674+
list = __imlib_ListModules("loaders", &num);
757675
/* no loaders? well don't load anything */
758676
if (!list)
759677
return;

src/lib/image.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ void __imlib_AddImagePixmapToCache(ImlibImagePixmap * ip);
135135
void __imlib_RemoveImagePixmapFromCache(ImlibImagePixmap * ip);
136136
void __imlib_CleanupImagePixmapCache(void);
137137
#endif
138-
char **__imlib_TrimLoaderList(char **list, int *num);
139138
void __imlib_RemoveAllLoaders(void);
140139
ImlibLoader *__imlib_FindBestLoaderForFile(const char *file,
141140
int for_save);

src/lib/modules.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "config.h"
2+
3+
#include <stdlib.h>
4+
5+
#include "file.h"
6+
#include "image.h"
7+
#include "loaderpath.h"
8+
9+
static char **
10+
__imlib_TrimLoaderList(char **list, int *num)
11+
{
12+
int i, n, size = 0;
13+
14+
if (!list)
15+
return NULL;
16+
17+
n = *num;
18+
19+
for (i = 0; i < n; i++)
20+
{
21+
char *ext;
22+
23+
if (!list[i])
24+
continue;
25+
ext = strrchr(list[i], '.');
26+
if ((ext) && (
27+
#ifdef __CYGWIN__
28+
(!strcasecmp(ext, ".dll")) ||
29+
#endif
30+
(!strcasecmp(ext, ".so"))))
31+
{
32+
/* Don't add the same loader multiple times... */
33+
if (!__imlib_ItemInList(list, size, list[i]))
34+
{
35+
list[size++] = list[i];
36+
continue;
37+
}
38+
}
39+
free(list[i]);
40+
}
41+
if (!size)
42+
{
43+
free(list);
44+
list = NULL;
45+
}
46+
else
47+
{
48+
list = realloc(list, size * sizeof(char *));
49+
}
50+
*num = size;
51+
return list;
52+
}
53+
54+
char **
55+
__imlib_ListModules(const char *what, int *num_ret)
56+
{
57+
char **list = NULL, **l;
58+
char path[1024];
59+
int num, i;
60+
61+
*num_ret = 0;
62+
63+
snprintf(path, sizeof(path), "%s/%s", SYS_LOADERS_PATH, what);
64+
l = __imlib_FileDir(path, &num);
65+
if (num <= 0)
66+
return NULL;
67+
68+
list = malloc(num * sizeof(char *));
69+
if (list)
70+
{
71+
for (i = 0; i < num; i++)
72+
{
73+
snprintf(path, sizeof(path), "%s/%s/%s",
74+
SYS_LOADERS_PATH, what, l[i]);
75+
list[i] = strdup(path);
76+
}
77+
*num_ret = num;
78+
}
79+
__imlib_FileFreeDirList(l, num);
80+
81+
/* List currently contains *everything in there* we need to weed out
82+
* the .so, .la, .a versions of the same loader or whatever else.
83+
* dlopen can take an extension-less name and do the Right Thing
84+
* with it, so that's what we'll give it. */
85+
list = __imlib_TrimLoaderList(list, num_ret);
86+
87+
return list;
88+
}

0 commit comments

Comments
 (0)