diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2022-11-01 16:05:38 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2022-11-01 16:05:38 -0400 |
commit | 7d63af86b012b14c3a711e0a16439fb5d55e5079 (patch) | |
tree | bc3511c21eb26c048e56098332fc36da4d01e9ea | |
parent | e102ac1a08542e402664694b1bf52e2184a25895 (diff) | |
download | bfs-7d63af86b012b14c3a711e0a16439fb5d55e5079.tar.xz |
color: Avoid dynamic allocation in get_ext_color()
-rw-r--r-- | src/color.c | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/color.c b/src/color.c index f96813f..6240b9c 100644 --- a/src/color.c +++ b/src/color.c @@ -131,8 +131,7 @@ static int set_color(struct colors *colors, const char *name, char *value) { /** * Transform a file extension for fast lookups, by reversing and lowercasing it. */ -static void extxfrm(char *ext) { - size_t len = strlen(ext); +static void extxfrm(char *ext, size_t len) { for (size_t i = 0; i < len - i; ++i) { char a = ext[i]; char b = ext[len - i - 1]; @@ -153,11 +152,19 @@ static void extxfrm(char *ext) { } } +/** Maximum supported extension length. */ +#define EXT_MAX 255 + /** * Set the color for an extension. */ -static int set_ext_color(struct colors *colors, char *key, const char *value) { - extxfrm(key); +static int set_ext_color(struct colors *colors, char *key, char *value) { + size_t len = dstrlen(key); + if (len > EXT_MAX) { + return -1; + } + + extxfrm(key, len); // A later *.x should override any earlier *.x, *.y.x, etc. struct trie_leaf *match; @@ -168,7 +175,7 @@ static int set_ext_color(struct colors *colors, char *key, const char *value) { struct trie_leaf *leaf = trie_insert_str(&colors->ext_colors, key); if (leaf) { - leaf->value = (char *)value; + leaf->value = value; return 0; } else { return -1; @@ -179,14 +186,15 @@ static int set_ext_color(struct colors *colors, char *key, const char *value) { * Find a color by an extension. */ static const char *get_ext_color(const struct colors *colors, const char *filename) { - char *xfrm = strdup(filename); - if (!xfrm) { - return NULL; - } - extxfrm(xfrm); + size_t name_len = strlen(filename); + size_t ext_len = name_len < EXT_MAX ? name_len : EXT_MAX; + const char *ext = filename + name_len - ext_len; + + char xfrm[ext_len + 1]; + memcpy(xfrm, ext, sizeof(xfrm)); + extxfrm(xfrm, ext_len); const struct trie_leaf *leaf = trie_find_prefix(&colors->ext_colors, xfrm); - free(xfrm); if (leaf) { return leaf->value; } else { |