From 76995688e80b6e1b23a39c86325c09de446bc392 Mon Sep 17 00:00:00 2001 From: Andrew Cagney Date: Mon, 20 Nov 2000 02:06:19 +0000 Subject: [PATCH] Replace asprintf() / vasprintf() with xasprintf() xvasprintf(). --- gdb/ChangeLog | 12 ++++++ gdb/TODO | 7 ---- gdb/defs.h | 5 +++ gdb/mi/ChangeLog | 5 +++ gdb/mi/mi-cmd-var.c | 2 +- gdb/mi/mi-main.c | 97 +++++++++++++++++++++------------------------ gdb/remote.c | 12 +++--- gdb/utils.c | 43 ++++++++++++++------ 8 files changed, 106 insertions(+), 77 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 63b60817309..f8f8416f8d2 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,15 @@ +Fri Nov 17 16:07:23 2000 Andrew Cagney + + * utils.c (xvasprintf, xasprintf): New functions. + * defs.h (xvasprintf, xasprintf): Add declarations. + + * remote.c (add_packet_config_cmd): Use function xasprintf instead + of asprintf. + * utils.c (vfprintf_maybe_filtered, vfprintf_unfiltered): Use + function xvasprintf instead of vasprintf. + + * TODO (xasprintf): Update. + Mon Nov 20 12:22:32 2000 Andrew Cagney * TODO: Mention ``extern'' and ``STREQ'' cleanups. diff --git a/gdb/TODO b/gdb/TODO index 8d0785da85a..9c7acdb7162 100644 --- a/gdb/TODO +++ b/gdb/TODO @@ -366,13 +366,6 @@ needed. -- -Replace asprintf() calls with xasprintf() calls. - -As with things like strdup() most calls to asprintf() don't check the -return value. - --- - Replace strsave() + mstrsave() with libiberty:xstrdup(). -- diff --git a/gdb/defs.h b/gdb/defs.h index dd788e62662..63f8345dea4 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -835,6 +835,11 @@ extern PTR xmmalloc (PTR, long); extern PTR xmrealloc (PTR, PTR, long); #endif +/* Like asprintf/vasprintf but get an internal_error if the call + fails. */ +extern void xasprintf (char **ret, const char *format, ...) ATTR_FORMAT (printf, 2, 3); +extern void xvasprintf (char **ret, const char *format, va_list ap); + extern int parse_escape (char **); /* Message to be printed before the error message, when an error occurs. */ diff --git a/gdb/mi/ChangeLog b/gdb/mi/ChangeLog index 15a1ddea773..c3fb691dd3a 100644 --- a/gdb/mi/ChangeLog +++ b/gdb/mi/ChangeLog @@ -1,3 +1,8 @@ +Fri Nov 17 16:07:23 2000 Andrew Cagney + + * mi-main.c: Replace asprintf with xasprintf. + * mi-cmd-var.c (mi_cmd_var_create): Ditto. + 2000-10-16 Eli Zaretskii * gdbmi.texinfo (GDB/MI Variable Objects): Dimensions of diff --git a/gdb/mi/mi-cmd-var.c b/gdb/mi/mi-cmd-var.c index 771d8ef37dc..a5caa2ac8e0 100644 --- a/gdb/mi/mi-cmd-var.c +++ b/gdb/mi/mi-cmd-var.c @@ -52,7 +52,7 @@ mi_cmd_var_create (char *command, char **argv, int argc) if (argc != 3) { - /* asprintf (&mi_error_message, + /* xasprintf (&mi_error_message, "mi_cmd_var_create: Usage: ."); return MI_CMD_ERROR; */ error ("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."); diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 12be2b7f061..75fd435d965 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -196,7 +196,8 @@ mi_cmd_exec_interrupt (char *args, int from_tty) #ifdef UI_OUT if (!target_executing) { - asprintf (&mi_error_message, "mi_cmd_exec_interrupt: Inferior not executing."); + xasprintf (&mi_error_message, + "mi_cmd_exec_interrupt: Inferior not executing."); return MI_CMD_ERROR; } interrupt_target_command_wrapper (args, from_tty); @@ -222,8 +223,8 @@ mi_cmd_thread_select (char *command, char **argv, int argc) if (argc != 1) { - asprintf (&mi_error_message, - "mi_cmd_thread_select: USAGE: threadnum."); + xasprintf (&mi_error_message, + "mi_cmd_thread_select: USAGE: threadnum."); return MI_CMD_ERROR; } else @@ -242,8 +243,8 @@ mi_cmd_thread_list_ids (char *command, char **argv, int argc) if (argc != 0) { - asprintf (&mi_error_message, - "mi_cmd_thread_list_ids: No arguments required."); + xasprintf (&mi_error_message, + "mi_cmd_thread_list_ids: No arguments required."); return MI_CMD_ERROR; } else @@ -299,7 +300,7 @@ mi_cmd_data_list_register_names (char *command, char **argv, int argc) ui_out_field_string (uiout, NULL, REGISTER_NAME (regnum)); else { - asprintf (&mi_error_message, "bad register number"); + xasprintf (&mi_error_message, "bad register number"); return MI_CMD_ERROR; } } @@ -335,8 +336,8 @@ mi_cmd_data_list_changed_registers (char *command, char **argv, int argc) changed = register_changed_p (regnum); if (changed < 0) { - asprintf (&mi_error_message, - "mi_cmd_data_list_changed_registers: Unable to read register contents."); + xasprintf (&mi_error_message, + "mi_cmd_data_list_changed_registers: Unable to read register contents."); return MI_CMD_ERROR; } else if (changed) @@ -357,8 +358,8 @@ mi_cmd_data_list_changed_registers (char *command, char **argv, int argc) changed = register_changed_p (regnum); if (changed < 0) { - asprintf (&mi_error_message, - "mi_cmd_data_list_register_change: Unable to read register contents."); + xasprintf (&mi_error_message, + "mi_cmd_data_list_register_change: Unable to read register contents."); return MI_CMD_ERROR; } else if (changed) @@ -366,7 +367,7 @@ mi_cmd_data_list_changed_registers (char *command, char **argv, int argc) } else { - asprintf (&mi_error_message, "bad register number"); + xasprintf (&mi_error_message, "bad register number"); return MI_CMD_ERROR; } } @@ -417,8 +418,8 @@ mi_cmd_data_list_register_values (char *command, char **argv, int argc) if (argc == 0) { - asprintf (&mi_error_message, - "mi_cmd_data_list_register_values: Usage: -data-list-register-values [...]"); + xasprintf (&mi_error_message, + "mi_cmd_data_list_register_values: Usage: -data-list-register-values [...]"); return MI_CMD_ERROR; } @@ -426,7 +427,8 @@ mi_cmd_data_list_register_values (char *command, char **argv, int argc) if (!target_has_registers) { - asprintf (&mi_error_message, "mi_cmd_data_list_register_values: No registers."); + xasprintf (&mi_error_message, + "mi_cmd_data_list_register_values: No registers."); return MI_CMD_ERROR; } @@ -469,7 +471,7 @@ mi_cmd_data_list_register_values (char *command, char **argv, int argc) } else { - asprintf (&mi_error_message, "bad register number"); + xasprintf (&mi_error_message, "bad register number"); return MI_CMD_ERROR; } } @@ -499,7 +501,7 @@ get_register (int regnum, int format) regnum, (enum lval_type *) NULL); if (optim) { - asprintf (&mi_error_message, "Optimized out"); + xasprintf (&mi_error_message, "Optimized out"); return -1; } @@ -563,8 +565,8 @@ mi_cmd_data_write_register_values (char *command, char **argv, int argc) if (argc == 0) { - asprintf (&mi_error_message, - "mi_cmd_data_write_register_values: Usage: -data-write-register-values [ ... ]"); + xasprintf (&mi_error_message, + "mi_cmd_data_write_register_values: Usage: -data-write-register-values [ ... ]"); return MI_CMD_ERROR; } @@ -572,19 +574,22 @@ mi_cmd_data_write_register_values (char *command, char **argv, int argc) if (!target_has_registers) { - asprintf (&mi_error_message, "mi_cmd_data_write_register_values: No registers."); + xasprintf (&mi_error_message, + "mi_cmd_data_write_register_values: No registers."); return MI_CMD_ERROR; } if (!(argc - 1)) { - asprintf (&mi_error_message, "mi_cmd_data_write_register_values: No regs and values specified."); + xasprintf (&mi_error_message, + "mi_cmd_data_write_register_values: No regs and values specified."); return MI_CMD_ERROR; } if ((argc - 1) % 2) { - asprintf (&mi_error_message, "mi_cmd_data_write_register_values: Regs and vals are not in pairs."); + xasprintf (&mi_error_message, + "mi_cmd_data_write_register_values: Regs and vals are not in pairs."); return MI_CMD_ERROR; } @@ -608,7 +613,7 @@ mi_cmd_data_write_register_values (char *command, char **argv, int argc) } else { - asprintf (&mi_error_message, "bad register number"); + xasprintf (&mi_error_message, "bad register number"); return MI_CMD_ERROR; } } @@ -630,8 +635,8 @@ mi_cmd_data_assign (char *command, char **argv, int argc) if (argc != 1) { - asprintf (&mi_error_message, - "mi_cmd_data_assign: Usage: -data-assign expression"); + xasprintf (&mi_error_message, + "mi_cmd_data_assign: Usage: -data-assign expression"); return MI_CMD_ERROR; } @@ -661,8 +666,8 @@ mi_cmd_data_evaluate_expression (char *command, char **argv, int argc) if (argc != 1) { - asprintf (&mi_error_message, - "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression"); + xasprintf (&mi_error_message, + "mi_cmd_data_evaluate_expression: Usage: -data-evaluate-expression expression"); return MI_CMD_ERROR; } @@ -691,9 +696,7 @@ mi_cmd_target_download (char *args, int from_tty) char *run; struct cleanup *old_cleanups = NULL; - asprintf (&run, "load %s", args); - if (run == 0) - internal_error ("mi_cmd_target_download: no memory"); + xasprintf (&run, "load %s", args); old_cleanups = make_cleanup (free, run); execute_command (run, from_tty); @@ -708,9 +711,7 @@ mi_cmd_target_select (char *args, int from_tty) char *run; struct cleanup *old_cleanups = NULL; - asprintf (&run, "target %s", args); - if (run == 0) - internal_error ("mi_cmd_target_select: no memory"); + xasprintf (&run, "target %s", args); old_cleanups = make_cleanup (free, run); /* target-select is always synchronous. once the call has returned @@ -798,8 +799,8 @@ mi_cmd_data_read_memory (char *command, char **argv, int argc) if (argc < 5 || argc > 6) { - asprintf (&mi_error_message, - "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR]."); + xasprintf (&mi_error_message, + "mi_cmd_data_read_memory: Usage: ADDR WORD-FORMAT WORD-SIZE NR-ROWS NR-COLS [ASCHAR]."); return MI_CMD_ERROR; } @@ -838,16 +839,16 @@ mi_cmd_data_read_memory (char *command, char **argv, int argc) nr_rows = atol (argv[3]); if (nr_rows <= 0) { - asprintf (&mi_error_message, - "mi_cmd_data_read_memory: invalid number of rows."); + xasprintf (&mi_error_message, + "mi_cmd_data_read_memory: invalid number of rows."); return MI_CMD_ERROR; } /* number of bytes per row. */ nr_cols = atol (argv[4]); if (nr_cols <= 0) { - asprintf (&mi_error_message, - "mi_cmd_data_read_memory: invalid number of columns."); + xasprintf (&mi_error_message, + "mi_cmd_data_read_memory: invalid number of columns."); } /* The un-printable character when printing ascii. */ if (argc == 6) @@ -861,8 +862,8 @@ mi_cmd_data_read_memory (char *command, char **argv, int argc) make_cleanup (free, mbuf); if (mbuf == NULL) { - asprintf (&mi_error_message, - "mi_cmd_data_read_memory: out of memory."); + xasprintf (&mi_error_message, + "mi_cmd_data_read_memory: out of memory."); return MI_CMD_ERROR; } nr_bytes = 0; @@ -1004,8 +1005,8 @@ mi_cmd_data_write_memory (char *command, char **argv, int argc) if (argc != 4) { - asprintf (&mi_error_message, - "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE."); + xasprintf (&mi_error_message, + "mi_cmd_data_write_memory: Usage: [-o COLUMN_OFFSET] ADDR FORMAT WORD-SIZE VALUE."); return MI_CMD_ERROR; } @@ -1242,13 +1243,11 @@ mi_execute_cli_command (const char *cli, char *args) { struct cleanup *old_cleanups; char *run; - asprintf (&run, cli, args); + xasprintf (&run, cli, args); if (mi_debug_p) /* FIXME: gdb_???? */ fprintf_unfiltered (gdb_stdout, "cli=%s run=%s\n", cli, run); - if (run == 0) - abort (); old_cleanups = make_cleanup (free, run); execute_command ( /*ui */ run, 0 /*from_tty */ ); do_cleanups (old_cleanups); @@ -1269,17 +1268,13 @@ mi_execute_async_cli_command (char *mi, char *args, int from_tty) make_exec_cleanup (free, async_args); strcpy (async_args, args); strcat (async_args, "&"); - asprintf (&run, "%s %s", mi, async_args); - if (run == 0) - internal_error ("mi_execute_async_cli_command: no memory"); + xasprintf (&run, "%s %s", mi, async_args); make_exec_cleanup (free, run); add_continuation (mi_exec_async_cli_cmd_continuation, NULL); } else { - asprintf (&run, "%s %s", mi, args); - if (run == 0) - internal_error ("mi_execute_async_cli_command: no memory"); + xasprintf (&run, "%s %s", mi, args); old_cleanups = make_cleanup (free, run); } diff --git a/gdb/remote.c b/gdb/remote.c index 9764e583ff2..2b8279cb031 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -574,12 +574,12 @@ add_packet_config_cmd (struct packet_config *config, config->title = title; config->detect = CMD_AUTO_BOOLEAN_AUTO; config->support = PACKET_SUPPORT_UNKNOWN; - asprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet", - name, title); - asprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet", - name, title); + xasprintf (&set_doc, "Set use of remote protocol `%s' (%s) packet", + name, title); + xasprintf (&show_doc, "Show current use of remote protocol `%s' (%s) packet", + name, title); /* set/show TITLE-packet {auto,on,off} */ - asprintf (&cmd_name, "%s-packet", title); + xasprintf (&cmd_name, "%s-packet", title); set_cmd = add_set_auto_boolean_cmd (cmd_name, class_obscure, &config->detect, set_doc, set_remote_list); @@ -590,7 +590,7 @@ add_packet_config_cmd (struct packet_config *config, if (legacy) { char *legacy_name; - asprintf (&legacy_name, "%s-packet", name); + xasprintf (&legacy_name, "%s-packet", name); add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, set_remote_list); add_alias_cmd (legacy_name, cmd_name, class_obscure, 0, diff --git a/gdb/utils.c b/gdb/utils.c index 64c26aef9be..46d0eb1e6f4 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -1079,6 +1079,35 @@ xrealloc (PTR ptr, size_t size) } +/* Like asprintf/vasprintf but get an internal_error if the call + fails. */ + +void +xasprintf (char **ret, const char *format, ...) +{ + va_list args; + va_start (args, format); + xvasprintf (ret, format, args); + va_end (args); +} + +void +xvasprintf (char **ret, const char *format, va_list ap) +{ + int status = vasprintf (ret, format, ap); + /* NULL could be returned due to a memory allocation problem; a + badly format string; or something else. */ + if ((*ret) == NULL) + internal_error ("%s:%d: vasprintf returned NULL buffer (errno %d)", + __FILE__, __LINE__, errno); + /* A negative status with a non-NULL buffer shouldn't never + happen. But to be sure. */ + if (status < 0) + internal_error ("%s:%d: vasprintf call failed (errno %d)", + __FILE__, __LINE__, errno); +} + + /* My replacement for the read system call. Used like `read' but keeps going if `read' returns too soon. */ @@ -1952,12 +1981,7 @@ vfprintf_maybe_filtered (struct ui_file *stream, const char *format, char *linebuffer; struct cleanup *old_cleanups; - vasprintf (&linebuffer, format, args); - if (linebuffer == NULL) - { - fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr); - exit (1); - } + xvasprintf (&linebuffer, format, args); old_cleanups = make_cleanup (free, linebuffer); fputs_maybe_filtered (linebuffer, stream, filter); do_cleanups (old_cleanups); @@ -1976,12 +2000,7 @@ vfprintf_unfiltered (struct ui_file *stream, const char *format, va_list args) char *linebuffer; struct cleanup *old_cleanups; - vasprintf (&linebuffer, format, args); - if (linebuffer == NULL) - { - fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr); - exit (1); - } + xvasprintf (&linebuffer, format, args); old_cleanups = make_cleanup (free, linebuffer); fputs_unfiltered (linebuffer, stream); do_cleanups (old_cleanups); -- 2.30.2