[binutils-gdb] [gdb/expr] Handle iconv EINVAL better in wchar_iterator::iterate

Tom de Vries vries@sourceware.org
Mon Nov 9 15:09:23 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=146c864ff545cbd12052cf025334a1c1d11f73a7

commit 146c864ff545cbd12052cf025334a1c1d11f73a7
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Nov 9 16:09:19 2026 +0100

    [gdb/expr] Handle iconv EINVAL better in wchar_iterator::iterate
    
    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.
    
    Approved-By: Tom Tromey <tom@tromey.com>
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34976

Diff:
---
 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..57fc9f44ff7 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 PR exp/34976.
+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"


More information about the Gdb-cvs mailing list