From: Alan Modra Date: Fri, 3 Oct 2025 23:14:58 +0000 (+0930) Subject: gdb: PR 33384 invalid disassembler option message X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=58a722afdb64a2c4b2ce606d4628214b890705d3;p=thirdparty%2Fbinutils-gdb.git gdb: PR 33384 invalid disassembler option message This is the gdb part of fixing PR33384, where it is noted that an error in a disassembler option prints the rest of the comma separated option string rather than just the option in error. Removing FOR_EACH_DISASSEMBLER_OPTION seemed a good idea to me, as we then expose the strchr there which is useful in zero terminating the option, and in the case of arm-tdep.c, to replace strcspn. Also, if the option is zero terminated we don't need disassembler_options_cmp. Alternatively, you could do similarly to arm-tdep.c in disasm.c by changing the error message to use %.*s with a length found by strcspn. I rejected that smaller patch on the grounds that it makes for churn in message translation. I also prefer to see code using the standard string functions. Regression tested on x86_64-linux. Message behaviour tested on powerpc64le-linux and arm-linux-eabi. * arm-tdep.c (show_disassembly_style_sfunc): Don't use FOR_EACH_DISASSEMBLER_OPTION. Use strchr needed for loop control to size option len. * disasm.c (set_disassembler_options): Don't use FOR_EACH_DISASSEMBLER_OPTION. Overwrite comma in options with a zero. Replace disassembler_options_cmp with strcmp. --- diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c index 940d05f75e6..f4c2e0413f1 100644 --- a/gdb/arm-tdep.c +++ b/gdb/arm-tdep.c @@ -9641,13 +9641,20 @@ show_disassembly_style_sfunc (struct ui_file *file, int from_tty, const char *options = get_disassembler_options (gdbarch); const char *style = ""; int len = 0; - const char *opt; + const char *opt = options; - FOR_EACH_DISASSEMBLER_OPTION (opt, options) - if (startswith (opt, "reg-names-")) + if (opt) + while (1) { - style = &opt[strlen ("reg-names-")]; - len = strcspn (style, ","); + const char *opt_end = strchr (opt, ','); + if (startswith (opt, "reg-names-")) + { + style = &opt[strlen ("reg-names-")]; + len = opt_end ? opt_end - style : 99; + } + if (!opt_end) + break; + opt = opt_end + 1; } gdb_printf (file, "The disassembly style is \"%.*s\".\n", len, style); diff --git a/gdb/disasm.c b/gdb/disasm.c index c8e830ed471..73b094b1173 100644 --- a/gdb/disasm.c +++ b/gdb/disasm.c @@ -1269,7 +1269,6 @@ set_disassembler_options (const char *prospective_options) = make_unique_xstrdup (prospective_options); char *options = remove_whitespace_and_extra_commas (prospective_options_local.get ()); - const char *opt; /* Allow all architectures, even ones that do not support 'set disassembler', to reset their disassembler options to NULL. */ @@ -1291,9 +1290,13 @@ set_disassembler_options (const char *prospective_options) valid_options = &valid_options_and_args->options; /* Verify we have valid disassembler options. */ - FOR_EACH_DISASSEMBLER_OPTION (opt, options) + char *opt = options; + while (1) { size_t i; + char *opt_end = strchr (opt, ','); + if (opt_end) + *opt_end = 0; for (i = 0; valid_options->name[i] != NULL; i++) if (valid_options->arg != NULL && valid_options->arg[i] != NULL) { @@ -1308,8 +1311,7 @@ set_disassembler_options (const char *prospective_options) if (valid_options->arg[i]->values == NULL) break; for (j = 0; valid_options->arg[i]->values[j] != NULL; j++) - if (disassembler_options_cmp - (arg, valid_options->arg[i]->values[j]) == 0) + if (strcmp (arg, valid_options->arg[i]->values[j]) == 0) { found = true; break; @@ -1317,7 +1319,7 @@ set_disassembler_options (const char *prospective_options) if (found) break; } - else if (disassembler_options_cmp (opt, valid_options->name[i]) == 0) + else if (strcmp (opt, valid_options->name[i]) == 0) break; if (valid_options->name[i] == NULL) { @@ -1326,6 +1328,10 @@ set_disassembler_options (const char *prospective_options) opt); return; } + if (!opt_end) + break; + *opt_end = ','; + opt = opt_end + 1; } *disassembler_options = options;