From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Correctly handle L'\\'
Date: Thu, 3 Jul 2025 12:55:25 -0600 [thread overview]
Message-ID: <20250703185525.1309719-1-tromey@adacore.com> (raw)
Hannes filed a bug that pointed out that:
print L'\\'
... did not work correctly. The bug is in convert_escape, which
simply transcribes the backslash character, rather than convert it
between encodings.
This patch fixes the error. I also turned a macro into a lambda to
clean up this code a little.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33124
---
gdb/c-lang.c | 26 +++++++++++++++-----------
gdb/testsuite/gdb.base/wchar.exp | 3 +++
2 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/gdb/c-lang.c b/gdb/c-lang.c
index f052401b8fd..9fccc1f7614 100644
--- a/gdb/c-lang.c
+++ b/gdb/c-lang.c
@@ -484,13 +484,6 @@ convert_hex (struct type *type, const char *p,
return p;
}
-#define ADVANCE \
- do { \
- ++p; \
- if (p == limit) \
- error (_("Malformed escape sequence")); \
- } while (0)
-
/* Convert an escape sequence to a target format. TYPE is the target
character type to use, and DEST_CHARSET is the name of the target
character set. The backslash of the escape sequence is at *P, and
@@ -502,18 +495,29 @@ static const char *
convert_escape (struct type *type, const char *dest_charset,
const char *p, const char *limit, struct obstack *output)
{
+ auto advance = [&] ()
+ {
+ ++p;
+ if (p == limit)
+ error (_("Malformed escape sequence"));
+ };
+
/* Skip the backslash. */
- ADVANCE;
+ advance ();
switch (*p)
{
case '\\':
- obstack_1grow (output, '\\');
+ /* Convert the backslash itself. This is probably overkill but
+ it doesn't hurt to do the full conversion. */
+ convert_between_encodings (host_charset (), dest_charset,
+ (const gdb_byte *) p, 1, 1,
+ output, translit_none);
++p;
break;
case 'x':
- ADVANCE;
+ advance ();
if (!ISXDIGIT (*p))
error (_("\\x used with no following hex digits."));
p = convert_hex (type, p, limit, output);
@@ -535,7 +539,7 @@ convert_escape (struct type *type, const char *dest_charset,
{
int length = *p == 'u' ? 4 : 8;
- ADVANCE;
+ advance ();
if (!ISXDIGIT (*p))
error (_("\\u used with no following hex digits"));
p = convert_ucn (p, limit, dest_charset, output, length);
diff --git a/gdb/testsuite/gdb.base/wchar.exp b/gdb/testsuite/gdb.base/wchar.exp
index 70f738cb7fc..9a0d79ada77 100644
--- a/gdb/testsuite/gdb.base/wchar.exp
+++ b/gdb/testsuite/gdb.base/wchar.exp
@@ -72,3 +72,6 @@ gdb_test "print repeat_p" "= $hex L\"A$cent$cent\"\.\.\." \
# From PR cli/14977, but here because it requires wchar_t.
gdb_test "printf \"%ls\\n\", 0" "\\(null\\)"
+
+# From PR exp/33124 - a bug when converting escapes.
+gdb_test "print L'\\\\'" " = $decimal L'\\\\\\\\'"
base-commit: 27e5f9c97599fa335f61340a726337cb4ce62804
--
2.50.0
next reply other threads:[~2025-07-03 18:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-03 18:55 Tom Tromey [this message]
2025-07-04 18:27 ` Hannes Domani
2025-07-05 5:55 ` Tom de Vries
2025-07-07 20:04 ` Tom de Vries
2025-07-07 20:30 ` Tom Tromey
2025-07-08 14:58 ` Tom Tromey
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=20250703185525.1309719-1-tromey@adacore.com \
--to=tromey@adacore.com \
--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