Skip to content
Myr

parse_theme: ignore whitespace around KV pairs and skip malformed lines

Before adoption: this commit came in with the history the project adopted, so no landing records it.

strtok_r(3) returns NULL if no tokens are found. Hence, we can't safely dereference key or value without first checking that they're not NULL, such as in the call to strcasecmp(3) later. However, as we need to do more than simply tokenization when we iterate through line, don't use strtok_r(3) such that we don't iterate through the same section of the string multiple times. The freedesktop icon specification states that the format for .theme files is mostly similar to that of the desktop file specification[1]: > Both the icon theme description file and the icon data files are > ini-style text files, as described in the desktop file specification. > They don't have any encoding field. Instead, they must always > be stored in UTF-8 encoding. The desktop entry specification has support for comments[2]: > Lines beginning with a # and blank lines are considered comments and > will be ignored, however they should be preserved across reads and > writes of the desktop entry file. This patch will also account for those. While we're at it, handle whitespace surrounding KV pairs properly. The desktop entry specification explicitly states that space space before and after the equals sign is to be ignored[3]: > Space before and after the equals sign should be ignored; the = sign > is the actual delimiter. Although it also bounds the character set that a key may be comprised: > Only the characters A-Za-z0-9- may be used in key names. don't bother validating key names: we only lookup known good keys, and as such malformed key names would be ignored anyway. [1] https://specifications.freedesktop.org/icon-theme/latest/#file_formats [2] https://specifications.freedesktop.org/desktop-entry/latest/basic-format.html#comments [3] https://specifications.freedesktop.org/desktop-entry/latest/basic-format.html#entries

authored by Rahul Sandhu · committed by Daniel Eklöf · parent 24aa49f · Browse files · 2 files+52 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Rahul Sandhu <nvraxn@posteo.uk>

Diff stats

2 files+52 added−6 removed

  • other:+52 added−6 removed

2 files+52 added−6 removed

modified:CHANGELOG.md+9 added-3 removed
37 unmodified lines
38### Deprecated39### Removed40### Fixed4142 * Removed statement that Cairo is required for rounded corners.43 This is no longer true.42* Removed statement that Cairo is required for rounded corners. This43 is no longer true.44* Segfault when attempting to parse a comment in an icon `index.theme` file45 and correctly interpreting KV pairs by ignoring whitespace ([#727][727]).4647[727]: https://codeberg.org/dnkl/fuzzel/pulls/727484950### Security51### Contributors5248 * tranzystorekk53* tranzystorekk54* Rahul Sandhu555657## 1.14.058
1116 unmodified lines
modified:icon.c+43 added-3 removed
1#include "icon.h"23#include <ctype.h>4#include <stdlib.h>5#include <stdio.h>6#include <string.h>7#include <stdbool.h>
118 unmodified lines
126 free(line);127 continue;128 }129129 char *tok_ctx = NULL;130 char *p = line;131 while (isspace((unsigned char)*p))132 p++;133 /* skip over comments and blank lines comprised of only whitespace */134 if (*p == '#' || *p == '\0') {135 free(line);136 continue;137 }138131 const char *key = strtok_r(line, "=", &tok_ctx);132 char *value = strtok_r(NULL, "=", &tok_ctx);139 const char *key_start = p;140 /* look for the first space or '=', i.e. the end of the key name */141 while (*p != '\0' && !isspace((unsigned char)*p) && *p != '=')142 p++;143 if (p == key_start) {144 /* empty key name */145 free(line);146 continue;147 }148149 char *key_end = p;150151 while (isspace((unsigned char)*p))152 p++;153154 /* keys may not contain whitespace */155 if (*p != '=') {156 free(line);157 continue;158 }159 /* consume the '=' */160 p++;161162 /*163 * While values themselves may contain whitespace, the spec tells us to164 * ignore any leading whitespace:165 * "Space before and after the equals sign should be ignored"166 */167 while (isspace((unsigned char)*p))168 p++;169170 *key_end = '\0';171 const char *key = key_start;172 char *value = p;173174 if (strcasecmp(key, "inherits") == 0) {175 char *ctx = NULL;176 for (const char *theme_name = strtok_r(value, ",", &ctx);177 theme_name != NULL; theme_name = strtok_r(NULL, ",", &ctx))
762 unmodified lines