1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-10 14:00:21 +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:
Ludovic Courtès 2011-04-27 20:34:08 +02:00
parent d84783a80c
commit a42d79711b
2 changed files with 23 additions and 5 deletions

View file

@ -1174,6 +1174,10 @@ get_codepoint (SCM port, scm_t_wchar *codepoint,
output_size = sizeof (utf8_buf) - output_left;
}
if (SCM_UNLIKELY (output_size == 0))
/* An unterminated sequence. */
err = EILSEQ;
if (SCM_UNLIKELY (err != 0))
{
/* Reset the `iconv' state. */
@ -1190,11 +1194,11 @@ get_codepoint (SCM port, scm_t_wchar *codepoint,
input encoding errors.) */
}
else
/* 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);
{
/* Convert the UTF8_BUF sequence to a Unicode code point. */
*codepoint = utf8_to_codepoint (utf8_buf, output_size);
update_port_lf (*codepoint, port);
}
*len = bytes_consumed;

View file

@ -531,6 +531,20 @@
(read-char -> #\μ)
(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
(tests
;; `peek-char' should repeatedly raise an error.