From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH] [gdb/expr] Handle iconv EINVAL better in wchar_iterator::iterate
Date: Sun, 8 Nov 2026 12:25:05 +0100 [thread overview]
Message-ID: <20261108112505.1651089-1-tdevries@suse.de> (raw)
PR exp/34976 reports the following problem.
Given this char* variable:
...
char *value = "abc\xe0";
...
printing it gets us:
...
$1 = 0x402004 "ab", <incomplete sequence \340>
...
We get 'a' and 'b' but not 'c'.
Fix this in wchar_iterator::iterate by handling EINVAL the same as EILSEQ and
E2BIG: if iconv converted some of the input sequence, use it.
This gets us instead:
...
$1 = 0x402004 "abc", <incomplete sequence \340>
...
Tested on aarch64-linux.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34976
---
gdb/charset.c | 9 +++++++--
gdb/testsuite/gdb.base/charset.exp | 4 ++++
gdb/testsuite/gdb.guile/scm-pretty-print.exp | 3 ++-
gdb/testsuite/gdb.python/py-prettyprint.exp | 3 ++-
4 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/gdb/charset.c b/gdb/charset.c
index 66199407a1f..4558625abf6 100644
--- a/gdb/charset.c
+++ b/gdb/charset.c
@@ -663,8 +663,13 @@ wchar_iterator::iterate (enum wchar_iterate_result *out_result,
continue;
case EINVAL:
- /* Incomplete input sequence. Let the caller know, and
- arrange for future calls to see EOF. */
+ /* Incomplete input sequence. We still might have converted a
+ character; if so, return it. */
+ if (out_avail < out_request * sizeof (gdb_wchar_t))
+ break;
+
+ /* Otherwise, let the caller know and arrange for future calls
+ to see EOF. */
*out_result = wchar_iterate_incomplete;
*ptr = m_input;
*len = m_bytes;
diff --git a/gdb/testsuite/gdb.base/charset.exp b/gdb/testsuite/gdb.base/charset.exp
index 9995021dabc..376b7ad88a2 100644
--- a/gdb/testsuite/gdb.base/charset.exp
+++ b/gdb/testsuite/gdb.base/charset.exp
@@ -519,6 +519,10 @@ foreach val {0 1 2 3 4 5 6 7 8 9 a b c d e f} {
gdb_test "print \"\\0\" \"${val}\"" " = \"\\\\000${val}\""
}
+# Regression test for PR34976.
+gdb_test {print "abc\xe0"} \
+ [string_to_regexp { = "abc", <incomplete sequence \340>}]
+
# Tests for wide- or unicode- strings. L is the prefix letter to use,
# either "L" (for wide strings), "u" (for UTF-16), or "U" (for UTF-32).
# NAME is used in the test names and should be related to the prefix
diff --git a/gdb/testsuite/gdb.guile/scm-pretty-print.exp b/gdb/testsuite/gdb.guile/scm-pretty-print.exp
index 585e2b80643..c0b4adf1e23 100644
--- a/gdb/testsuite/gdb.guile/scm-pretty-print.exp
+++ b/gdb/testsuite/gdb.guile/scm-pretty-print.exp
@@ -93,7 +93,8 @@ proc run_lang_tests {exefile lang} {
gdb_test "print estring" " = \"embedded x\\\\201\\\\202\\\\203\\\\204\""
gdb_test_no_output "guile (set! *pp-ls-encoding* \"UTF-8\")"
- gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>"
+ gdb_test "print estring2" \
+ [string_to_regexp {"embedded x", <incomplete sequence \302>}]
gdb_test_no_output "set guile print-stack full"
gdb_test "print hint_error" "ERROR: Invalid display hint: 42\r\nhint_error_val"
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.exp b/gdb/testsuite/gdb.python/py-prettyprint.exp
index ab31928fa77..780edf5b968 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.exp
+++ b/gdb/testsuite/gdb.python/py-prettyprint.exp
@@ -105,7 +105,8 @@ proc run_lang_tests {exefile lang} {
}
gdb_test_no_output "python pp_ls_encoding = 'UTF-8'"
- gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>"
+ gdb_test "print estring2" \
+ [string_to_regexp {"embedded x", <incomplete sequence \302>}]
gdb_test_no_output "set python print-stack full"
gdb_test "print hint_error" "Exception: hint failed\r\nhint_error_val"
base-commit: 819dfa7b5325656ccb58ea5f65761982bb7844e3
--
2.51.0
next reply other threads:[~2026-11-08 11:25 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-08 11:25 Tom de Vries [this message]
2026-11-09 14:44 ` Tom Tromey
2026-11-09 15:11 ` Tom de Vries
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261108112505.1651089-1-tdevries@suse.de \
--to=tdevries@suse.de \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox