From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 6/7] Avoid crash with "NSString" literals
Date: Sun, 16 Nov 2025 18:27:05 -0700 [thread overview]
Message-ID: <20251117012752.2657208-7-tom@tromey.com> (raw)
In-Reply-To: <20251117012752.2657208-1-tom@tromey.com>
Evaluating an Objective-C "NSString" literal will cause gdb to crash.
This patch fixes the crash.
I think the result here still isn't correct -- I see a warning from
the runtime ("autorelease called without pool for object") with the
new code.
However, not crashing is an improvement on its own.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20501
---
gdb/c-exp.y | 10 +++++-----
gdb/objc-lang.c | 3 ++-
gdb/testsuite/gdb.objc/print.exp | 4 ++++
3 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index 6dd2b0800dd..fadd735c517 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -223,7 +223,7 @@ static void c_print_token (FILE *file, int type, YYSTYPE value);
nonterminal "name", which matches either NAME or TYPENAME. */
%token <tsval> STRING
-%token <sval> NSSTRING /* ObjC Foundation "NSString" literal */
+%token <tsval> NSSTRING /* ObjC Foundation "NSString" literal */
%token SELECTOR /* ObjC "@selector" pseudo-operator */
%token <tsval> CHAR
%token <ssym> NAME /* BLOCKNAME defined below to give it higher precedence. */
@@ -1030,12 +1030,12 @@ exp : string_exp
}
;
-exp : NSSTRING /* ObjC NextStep NSString constant
- * of the form '@' '"' string '"'.
- */
+exp : NSSTRING
{
+ /* ObjC NextStep NSString constant of the
+ form '@' '"' string '"'. */
pstate->push_new<objc_nsstring_operation>
- (copy_name ($1));
+ (std::string ($1.ptr, $1.length));
}
;
diff --git a/gdb/objc-lang.c b/gdb/objc-lang.c
index a6abcd60089..9804f2c3963 100644
--- a/gdb/objc-lang.c
+++ b/gdb/objc-lang.c
@@ -169,7 +169,8 @@ objc_nsstring_operation::evaluate (struct type *expect_type,
struct type *type;
if (!target_has_execution ())
- return 0; /* Can't call into inferior to create NSString. */
+ error (_("evaluation of this expression "
+ "requires the target program to be active"));
stringValue[2] = value_string (str.c_str (), str.size () + 1, char_type);
stringValue[2] = value_coerce_array (stringValue[2]);
diff --git a/gdb/testsuite/gdb.objc/print.exp b/gdb/testsuite/gdb.objc/print.exp
index e3f4177d997..2e9fee3b869 100644
--- a/gdb/testsuite/gdb.objc/print.exp
+++ b/gdb/testsuite/gdb.objc/print.exp
@@ -62,6 +62,10 @@ clean_restart
if { [set_lang_objc] } {
test_float_accepted
test_float_rejected
+
+ gdb_test {print @"hi"} \
+ "evaluation of this expression requires the target program to be active" \
+ "simple nsstring"
} else {
warning "Objective-c print tests suppressed"
}
--
2.49.0
next prev parent reply other threads:[~2025-11-17 1:30 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-17 1:26 [PATCH 0/7] Objective-C fixes Tom Tromey
2025-11-17 1:27 ` [PATCH 1/7] Reformat gdb.objc tests Tom Tromey
2025-11-17 1:27 ` [PATCH 2/7] Make the gdb.objc tests compile Tom Tromey
2025-11-17 10:09 ` Matt Rice
2025-11-18 0:51 ` Tom Tromey
2025-11-18 1:12 ` Matt Rice
2025-12-02 16:20 ` Tom Tromey
2025-12-03 15:24 ` Matt Rice
2025-11-17 1:27 ` [PATCH 3/7] Minor fixes to make gdb.objc tests pass Tom Tromey
2025-11-17 1:27 ` [PATCH 4/7] Rename lookup_struct_typedef Tom Tromey
2025-11-17 1:27 ` [PATCH 5/7] Remove a couple Objective-C expression helpers Tom Tromey
2025-11-17 1:27 ` Tom Tromey [this message]
2025-11-17 1:27 ` [PATCH 7/7] Rewrite the @selector code 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=20251117012752.2657208-7-tom@tromey.com \
--to=tom@tromey.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