mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-16 05:34:00 -04:00
search: filter out non-printable characters
Don’t allow non-printable characters in the search input string.
This commit is contained in:
parent
e7b8f95af7
commit
cbf90d67a2
1 changed files with 16 additions and 4 deletions
20
search.c
20
search.c
|
|
@ -354,14 +354,28 @@ search_find_next(struct terminal *term)
|
||||||
void
|
void
|
||||||
search_add_chars(struct terminal *term, const char *src, size_t count)
|
search_add_chars(struct terminal *term, const char *src, size_t count)
|
||||||
{
|
{
|
||||||
|
const char *_src = src;
|
||||||
mbstate_t ps = {0};
|
mbstate_t ps = {0};
|
||||||
size_t wchars = mbsnrtowcs(NULL, &src, count, 0, &ps);
|
size_t wchars = mbsnrtowcs(NULL, &_src, count, 0, &ps);
|
||||||
|
|
||||||
if (wchars == -1) {
|
if (wchars == -1) {
|
||||||
LOG_ERRNO("failed to convert %.*s to wchars", (int)count, src);
|
LOG_ERRNO("failed to convert %.*s to wchars", (int)count, src);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_src = src;
|
||||||
|
ps = (mbstate_t){0};
|
||||||
|
wchar_t wcs[wchars + 1];
|
||||||
|
mbsnrtowcs(wcs, &_src, count, wchars, &ps);
|
||||||
|
|
||||||
|
/* Strip non-printable characters */
|
||||||
|
for (size_t i = 0, j = 0, orig_wchars = wchars; i < orig_wchars; i++) {
|
||||||
|
if (iswprint(wcs[i]))
|
||||||
|
wcs[j++] = wcs[i];
|
||||||
|
else
|
||||||
|
wchars--;
|
||||||
|
}
|
||||||
|
|
||||||
if (!search_ensure_size(term, term->search.len + wchars))
|
if (!search_ensure_size(term, term->search.len + wchars))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
@ -371,9 +385,7 @@ search_add_chars(struct terminal *term, const char *src, size_t count)
|
||||||
&term->search.buf[term->search.cursor],
|
&term->search.buf[term->search.cursor],
|
||||||
(term->search.len - term->search.cursor) * sizeof(wchar_t));
|
(term->search.len - term->search.cursor) * sizeof(wchar_t));
|
||||||
|
|
||||||
memset(&ps, 0, sizeof(ps));
|
memcpy(&term->search.buf[term->search.cursor], wcs, wchars * sizeof(wchar_t));
|
||||||
mbsnrtowcs(&term->search.buf[term->search.cursor], &src, count,
|
|
||||||
wchars, &ps);
|
|
||||||
|
|
||||||
term->search.len += wchars;
|
term->search.len += wchars;
|
||||||
term->search.cursor += wchars;
|
term->search.cursor += wchars;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue