From: Tom Tromey Date: Sun, 16 Nov 2025 17:29:40 +0000 (-0700) Subject: Avoid crash with "NSString" literals X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4062d79d63aef884ca69b92bd293c997fae142e7;p=thirdparty%2Fbinutils-gdb.git 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 --- 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" }