mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-12 06:41:13 +02:00
Gracefully handle unterminated UTF-8 sequences instead of hitting an `assert'.
* libguile/ports.c (get_codepoint): Return EILSEQ when OUTPUT_SIZE == 0. * test-suite/tests/ports.test ("string ports"): Add 2 tests for unterminated sequences.
This commit is contained in:
parent
d84783a80c
commit
a42d79711b
2 changed files with 23 additions and 5 deletions
|
@ -1174,6 +1174,10 @@ get_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
output_size = sizeof (utf8_buf) - output_left;
|
output_size = sizeof (utf8_buf) - output_left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SCM_UNLIKELY (output_size == 0))
|
||||||
|
/* An unterminated sequence. */
|
||||||
|
err = EILSEQ;
|
||||||
|
|
||||||
if (SCM_UNLIKELY (err != 0))
|
if (SCM_UNLIKELY (err != 0))
|
||||||
{
|
{
|
||||||
/* Reset the `iconv' state. */
|
/* Reset the `iconv' state. */
|
||||||
|
@ -1190,11 +1194,11 @@ get_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
input encoding errors.) */
|
input encoding errors.) */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
/* Convert the UTF8_BUF sequence to a Unicode code point. */
|
{
|
||||||
*codepoint = utf8_to_codepoint (utf8_buf, output_size);
|
/* Convert the UTF8_BUF sequence to a Unicode code point. */
|
||||||
|
*codepoint = utf8_to_codepoint (utf8_buf, output_size);
|
||||||
if (SCM_LIKELY (err == 0))
|
update_port_lf (*codepoint, port);
|
||||||
update_port_lf (*codepoint, port);
|
}
|
||||||
|
|
||||||
*len = bytes_consumed;
|
*len = bytes_consumed;
|
||||||
|
|
||||||
|
|
|
@ -531,6 +531,20 @@
|
||||||
(read-char -> #\μ)
|
(read-char -> #\μ)
|
||||||
(read-char -> eof)))
|
(read-char -> eof)))
|
||||||
|
|
||||||
|
(test-decoding-error (206 187 206) "UTF-8" 'error
|
||||||
|
;; Unterminated sequence.
|
||||||
|
(tests
|
||||||
|
(read-char -> #\λ)
|
||||||
|
(read-char -> error)
|
||||||
|
(read-char -> eof)))
|
||||||
|
|
||||||
|
(test-decoding-error (206 187 206) "UTF-8" 'substitute
|
||||||
|
;; Unterminated sequence.
|
||||||
|
(tests
|
||||||
|
(read-char -> #\λ)
|
||||||
|
(read-char -> #\?)
|
||||||
|
(read-char -> eof)))
|
||||||
|
|
||||||
(test-decoding-error (255 65 66 67) "UTF-8" 'error
|
(test-decoding-error (255 65 66 67) "UTF-8" 'error
|
||||||
(tests
|
(tests
|
||||||
;; `peek-char' should repeatedly raise an error.
|
;; `peek-char' should repeatedly raise an error.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue