Skip to content

Commit

Permalink
tab-complete: figure out the size before copying
Browse files Browse the repository at this point in the history
we already need to know the string length since `cursor` needs to be
adjusted.

so just calculate the length beforehand and use `memcpy` to copy exactly
as much as needed (as opposed to `strncpy` which always writes `n`
bytes).
  • Loading branch information
N-R-K authored and hiltjo committed Sep 2, 2022
1 parent 32db2b1 commit 528d39b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,9 @@ keypress(XKeyEvent *ev)
case XK_Tab:
if (!sel)
return;
strncpy(text, sel->text, sizeof text - 1);
cursor = strnlen(sel->text, sizeof text - 1);
memcpy(text, sel->text, cursor);
text[sizeof text - 1] = '\0';
cursor = strlen(text);
match();
break;
}
Expand Down

0 comments on commit 528d39b

Please sign in to comment.