]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Avoid crash with "NSString" literals
authorTom Tromey <tom@tromey.com>
Sun, 16 Nov 2025 17:29:40 +0000 (10:29 -0700)
committerTom Tromey <tom@tromey.com>
Tue, 2 Dec 2025 16:22:45 +0000 (09:22 -0700)
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
gdb/objc-lang.c
gdb/testsuite/gdb.objc/print.exp

index 6dd2b0800dda7abadc2952f76310eda9d90d7391..fadd735c51701f7b39c9ba35533db5c67f13e8da 100644 (file)
@@ -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));
                        }
        ;
 
index a6abcd600897e282d10a7bc38a0565da38e6d828..9804f2c3963a9c8ae6cc039db85983ab71eae20a 100644 (file)
@@ -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]);
index e3f4177d9972d2a20d4308bfe26b2bcc8ee930ed..2e9fee3b869adf82a8af228b182986443239766e 100644 (file)
@@ -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"
 }