Skip to content

Commit

Permalink
Use strchr(3) instead of memccpy(3) in collect_hl_styles()
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbarnes committed Dec 26, 2024
1 parent 30b44e3 commit bd2eb62
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/completion.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,22 @@ void collect_bound_normal_keys(EditorState *e, PointerArray *a, const char *pref

void collect_hl_styles(EditorState *e, PointerArray *a, const char *prefix)
{
char filetype[FILETYPE_NAME_MAX + 1];
char *end = memccpy(filetype, prefix, '.', sizeof(filetype));

if (!end || end <= filetype + 1) {
const char *dot = strchr(prefix, '.');
if (!dot || dot == prefix || (dot - prefix) > FILETYPE_NAME_MAX) {
// No dot found in prefix, or found at offset 0, or buffer too small;
// just collect matching highlight names added by the `hi` command
collect_builtin_styles(a, prefix);
collect_hashmap_keys(&e->styles.other, a, prefix);
return;
}

// Null terminate filetype[] by overwriting the dot
end[-1] = '\0';
// Copy and null-terminate the filetype part of `prefix` (before the dot)
char filetype[FILETYPE_NAME_MAX + 1];
size_t ftlen = dot - prefix;
memcpy(filetype, prefix, ftlen);
filetype[ftlen] = '\0';

// Find or load the Syntax matching the string before the dot
// Find or load the Syntax for `filetype`
const Syntax *syn = find_syntax(&e->syntaxes, filetype);
if (!syn) {
syn = load_syntax_by_filetype(e, filetype);
Expand All @@ -214,7 +215,7 @@ void collect_hl_styles(EditorState *e, PointerArray *a, const char *prefix)

// Collect all emit names from `syn` that start with the string after
// the dot
collect_syntax_emit_names(syn, a, prefix + (end - filetype));
collect_syntax_emit_names(syn, a, dot + 1);
}

void collect_compilers(EditorState *e, PointerArray *a, const char *prefix)
Expand Down

0 comments on commit bd2eb62

Please sign in to comment.