From 4062d79d63aef884ca69b92bd293c997fae142e7 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sun, 16 Nov 2025 10:29:40 -0700 Subject: [PATCH] Avoid crash with "NSString" literals 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 STRING -%token NSSTRING /* ObjC Foundation "NSString" literal */ +%token NSSTRING /* ObjC Foundation "NSString" literal */ %token SELECTOR /* ObjC "@selector" pseudo-operator */ %token CHAR %token 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 - (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.47.3