Handle more theme loop situations

This is a follow up for commit f64a8cc1a6
resulting from https://bugs.freedesktop.org/show_bug.cgi?id=3603

The current loop detection only works for direct self references but not
for transitive ones. Limiting the inheritance depth fixes this issue as
suggested by Keith Packard.

I avoided the introduction of a recursion function. Instead I modified
XcursorScanTheme to work iterative.

The current recursion code adds the "Inherits=..." line to heap and has
an iteration variable to go through all themes listed in that line per
recursion. This is covered with the newly introduced XcursorInherit
struct with its fields "line" and "theme". Since "theme" points into
"line", only "line" has to be freed eventually.

If a fixed inheritage limit of 32 is reached, the code stops processing
and returns NULL. It also returns NULL if it detects the initial theme
in one of the inheritages to break the loop early on.

Last but not least I removed the printf statement. The only situation in
which libXcursor writes to stdout is when it is explicitly requested.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
This commit is contained in:
Tobias Stoeckmann 2021-05-02 16:00:25 +02:00
parent 403bb32d5f
commit b47ca9858e

View file

@ -32,6 +32,11 @@
#define XCURSORPATH "~/.local/share/icons:~/.icons:/usr/share/icons:/usr/share/pixmaps:"ICONDIR #define XCURSORPATH "~/.local/share/icons:~/.icons:/usr/share/icons:/usr/share/pixmaps:"ICONDIR
#endif #endif
typedef struct XcursorInherit {
char *line;
const char *theme;
} XcursorInherit;
const char * const char *
XcursorLibraryPath (void) XcursorLibraryPath (void)
{ {
@ -206,16 +211,17 @@ _XcursorThemeInherits (const char *full)
} }
#define XCURSOR_SCAN_CORE ((FILE *) 1) #define XCURSOR_SCAN_CORE ((FILE *) 1)
#define MAX_INHERITS_DEPTH 32
static FILE * static FILE *
XcursorScanTheme (const char *theme, const char *name) XcursorScanTheme (const char *theme, const char *name)
{ {
FILE *f = NULL; FILE *f = NULL;
char *full; char *full;
char *dir; char *dir;
const char *path; const char *path;
char *inherits = NULL; XcursorInherit inherits[MAX_INHERITS_DEPTH + 1];
const char *i; int d;
if (!theme || !name) if (!theme || !name)
return NULL; return NULL;
@ -228,46 +234,77 @@ XcursorScanTheme (const char *theme, const char *name)
*/ */
if (!strcmp (theme, XCURSOR_CORE_THEME) && XcursorLibraryShape (name) >= 0) if (!strcmp (theme, XCURSOR_CORE_THEME) && XcursorLibraryShape (name) >= 0)
return XCURSOR_SCAN_CORE; return XCURSOR_SCAN_CORE;
/*
* Scan this theme memset (inherits, 0, sizeof (inherits));
*/
for (path = XcursorLibraryPath (); d = 0;
path && f == NULL; inherits[d].theme = theme;
path = _XcursorNextPath (path))
while (f == NULL && d >= 0 && inherits[d].theme != NULL)
{ {
dir = _XcursorBuildThemeDir (path, theme); /*
if (dir) * Scan this theme
*/
for (path = XcursorLibraryPath ();
path && f == NULL;
path = _XcursorNextPath (path))
{ {
full = _XcursorBuildFullname (dir, "cursors", name); dir = _XcursorBuildThemeDir (path, inherits[d].theme);
if (full) if (dir)
{ {
f = fopen (full, "r"); full = _XcursorBuildFullname (dir, "cursors", name);
free (full);
}
if (!f && !inherits)
{
full = _XcursorBuildFullname (dir, "", "index.theme");
if (full) if (full)
{ {
inherits = _XcursorThemeInherits (full); f = fopen (full, "r");
free (full); free (full);
} }
if (!f && inherits[d + 1].line == NULL)
{
if (d + 1 >= MAX_INHERITS_DEPTH)
{
free (dir);
goto cleanup;
}
full = _XcursorBuildFullname (dir, "", "index.theme");
if (full)
{
inherits[d + 1].line = _XcursorThemeInherits (full);
inherits[d + 1].theme = inherits[d + 1].line;
free (full);
}
}
free (dir);
} }
free (dir);
} }
if (inherits[d + 1].line == NULL)
{
if (d == 0)
inherits[d].theme = NULL;
else
{
inherits[d].theme = _XcursorNextPath (inherits[d].theme);
if (inherits[d].theme == NULL)
{
free (inherits[d].line);
inherits[d--].line = NULL;
}
}
}
else
d++;
/*
* Detect and break self reference loop early on.
*/
if (inherits[d].theme != NULL && strcmp (inherits[d].theme, theme) == 0)
break;
} }
/*
* Recurse to scan inherited themes cleanup:
*/ for (d = 1; d <= MAX_INHERITS_DEPTH; d++)
for (i = inherits; i && f == NULL; i = _XcursorNextPath (i)) free (inherits[d].line);
{
if (strcmp(i, theme) != 0)
f = XcursorScanTheme (i, name);
else
printf("Not calling XcursorScanTheme because of circular dependency: %s. %s", i, name);
}
if (inherits != NULL)
free (inherits);
return f; return f;
} }