diff --git a/src/detection/wmtheme/wmtheme_linux.c b/src/detection/wmtheme/wmtheme_linux.c
index 70b6d5d9c..1142fc1e6 100644
--- a/src/detection/wmtheme/wmtheme_linux.c
+++ b/src/detection/wmtheme/wmtheme_linux.c
@@ -1,4 +1,5 @@
#include "wmtheme.h"
+#include "common/io/io.h"
#include "common/properties.h"
#include "common/parsing.h"
#include "common/settings.h"
@@ -142,50 +143,45 @@ static bool detectOpenbox(const FFstrbuf* dePrettyName, FFstrbuf* themeOrError)
else
ffStrbufAppendS(&absolutePath, ".config/openbox/rc.xml");
- char* line = NULL;
- size_t len = 0;
-
- FILE* file = fopen(absolutePath.chars, "r");
- if(file == NULL)
+ FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
+ if (!ffReadFileBuffer(absolutePath.chars, &content))
{
- ffStrbufAppendF(themeOrError, "Couldn't open \"%s\"", absolutePath.chars);
-
+ ffStrbufAppendF(themeOrError, "Couldn't read \"%s\"", absolutePath.chars);
return false;
}
- while(getline(&line, &len, file) != -1)
- {
- if(strstr(line, "") != 0)
- break;
- }
+ const char *themeStart = strstr(content.chars, "");
+ if (themeStart == NULL)
+ goto theme_not_found;
- while(getline(&line, &len, file) != -1)
- {
- if(strstr(line, "") != 0)
- {
- ffStrbufAppendS(themeOrError, line);
- ffStrbufRemoveStrings(themeOrError, 2, (const char*[]) { "", "" });
- ffStrbufTrimRight(themeOrError, '\n');
- ffStrbufTrim(themeOrError, ' ');
- break;
- }
+ const char *themeEnd = strstr(themeStart, "");
+ if (__builtin_expect(themeEnd == NULL, false)) // very rare case
+ goto theme_not_found;
- if(strstr(line, "") != 0) // sanity check
- break;
- }
+ const char *nameStart = strstr(themeStart, "");
+ if (nameStart == NULL)
+ goto name_not_found;
- if(line != NULL)
- free(line);
+ const char *nameEnd = strstr(nameStart, "");
+ if (nameEnd == NULL || nameEnd > themeEnd) // (nameEnd > themeEnd) means name is not a theme's child
+ goto name_not_found;
- fclose(file);
+ nameStart += strlen("");
+ ffStrbufAppendNS(themeOrError, (uint32_t)(nameEnd - nameStart), nameStart);
+ ffStrbufTrim(themeOrError, ' ');
if(themeOrError->length == 0)
- {
- ffStrbufAppendF(themeOrError, "Couldn't find theme name in \"%s\"", absolutePath.chars);
- return false;
- }
+ goto name_not_found;
return true;
+
+theme_not_found:
+ ffStrbufAppendF(themeOrError, "Couldn't find theme node in \"%s\"", absolutePath.chars);
+ return false;
+
+name_not_found:
+ ffStrbufAppendF(themeOrError, "Couldn't find theme name in \"%s\"", absolutePath.chars);
+ return false;
}
bool ffDetectWmTheme(FFstrbuf* themeOrError)