From 08850b56edd752c3792bfb9bfc59dd9a5577ac2d Mon Sep 17 00:00:00 2001 From: Pierre Muller Date: Tue, 13 Nov 2012 09:46:11 +0000 Subject: [PATCH] ARI fixes: Avoid sprintf function use rule. * charset.c (convert_between_encodings): Use xsnprintf. * cli-out.c (cli_field_int): Likewise. * cp-namespace.c (cp_lookup_nested_symbol): Likewise. * expprint.c (op_name_standard): Likewise. * frv-tdep.c (set_variant_num_gprs): Likewise. (set_variant_num_fprs): Likewise. * m68hc11-tdep.c (m68hc11_initialize_register_info): Likewise. * nto-tdep.c (nto_find_and_open_solib): Likewise. (nto_init_solib_absolute_prefix): Likewise. * source.c (init_source_path): Likewise. (print_source_lines_base): Likewise. * valprint.c (print_wchar): Likewise. * mi/mi-out.c (mi_field_int): Likewise. windows-nat.c (windows_pid_to_exec_file): Likewise. (windows_create_inferior): Likewise. (_initialize_check_for_gdb_ini): Likewise. --- gdb/ChangeLog | 20 ++++++++++++++++++++ gdb/charset.c | 2 +- gdb/cli-out.c | 2 +- gdb/cp-namespace.c | 7 ++++--- gdb/expprint.c | 2 +- gdb/frv-tdep.c | 4 ++-- gdb/m68hc11-tdep.c | 2 +- gdb/mi/mi-out.c | 2 +- gdb/nto-tdep.c | 24 +++++++++++++----------- gdb/source.c | 14 ++++++++------ gdb/valprint.c | 7 ++++--- gdb/windows-nat.c | 16 +++++++++------- 12 files changed, 65 insertions(+), 37 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 250a25599b..c988862c29 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,23 @@ +2012-11-13 Pierre Muller + + ARI fixes: Avoid sprintf function use rule. + * charset.c (convert_between_encodings): Use xsnprintf. + * cli-out.c (cli_field_int): Likewise. + * cp-namespace.c (cp_lookup_nested_symbol): Likewise. + * expprint.c (op_name_standard): Likewise. + * frv-tdep.c (set_variant_num_gprs): Likewise. + (set_variant_num_fprs): Likewise. + * m68hc11-tdep.c (m68hc11_initialize_register_info): Likewise. + * nto-tdep.c (nto_find_and_open_solib): Likewise. + (nto_init_solib_absolute_prefix): Likewise. + * source.c (init_source_path): Likewise. + (print_source_lines_base): Likewise. + * valprint.c (print_wchar): Likewise. + * mi/mi-out.c (mi_field_int): Likewise. + windows-nat.c (windows_pid_to_exec_file): Likewise. + (windows_create_inferior): Likewise. + (_initialize_check_for_gdb_ini): Likewise. + 2012-11-12 Joel Brobecker * frame.h (deprecated_frame_register_read): Renames diff --git a/gdb/charset.c b/gdb/charset.c index 89baf461f4..777d901e26 100644 --- a/gdb/charset.c +++ b/gdb/charset.c @@ -531,7 +531,7 @@ convert_between_encodings (const char *from, const char *to, { char octal[5]; - sprintf (octal, "\\%.3o", *inp & 0xff); + xsnprintf (octal, sizeof (octal), "\\%.3o", *inp & 0xff); obstack_grow_str (output, octal); ++inp; diff --git a/gdb/cli-out.c b/gdb/cli-out.c index 925206c535..540403609b 100644 --- a/gdb/cli-out.c +++ b/gdb/cli-out.c @@ -139,7 +139,7 @@ cli_field_int (struct ui_out *uiout, int fldno, int width, if (data->suppress_output) return; - sprintf (buffer, "%d", value); + xsnprintf (buffer, sizeof (buffer), "%d", value); /* Always go through the function pointer (virtual function call). We may have been extended. */ diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c index e2291a9938..1a694023b9 100644 --- a/gdb/cp-namespace.c +++ b/gdb/cp-namespace.c @@ -688,6 +688,7 @@ cp_lookup_nested_symbol (struct type *parent_type, just like members of namespaces; in particular, lookup_symbol_namespace works when looking them up. */ + int size; const char *parent_name = type_name_no_tag_or_error (saved_parent_type); struct symbol *sym = cp_lookup_symbol_in_namespace (parent_name, nested_name, @@ -703,9 +704,9 @@ cp_lookup_nested_symbol (struct type *parent_type, namespace seach is is already not C++ compliant and more assumptions could make it too magic. */ - concatenated_name = alloca (strlen (parent_name) + 2 - + strlen (nested_name) + 1); - sprintf (concatenated_name, "%s::%s", + size = strlen (parent_name) + 2 + strlen (nested_name) + 1; + concatenated_name = alloca (size); + xsnprintf (concatenated_name, size, "%s::%s", parent_name, nested_name); sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN); if (sym != NULL) diff --git a/gdb/expprint.c b/gdb/expprint.c index 129dfa70b0..c8eef5e9c9 100644 --- a/gdb/expprint.c +++ b/gdb/expprint.c @@ -670,7 +670,7 @@ op_name_standard (enum exp_opcode opcode) { static char buf[30]; - sprintf (buf, "", opcode); + xsnprintf (buf, sizeof (buf), "", opcode); return buf; } #define OP(name) \ diff --git a/gdb/frv-tdep.c b/gdb/frv-tdep.c index 9262b103aa..28151f658c 100644 --- a/gdb/frv-tdep.c +++ b/gdb/frv-tdep.c @@ -233,7 +233,7 @@ set_variant_num_gprs (struct gdbarch_tdep *var, int num_gprs) { char buf[20]; - sprintf (buf, "gr%d", r); + xsnprintf (buf, sizeof (buf), "gr%d", r); var->register_names[first_gpr_regnum + r] = xstrdup (buf); } } @@ -252,7 +252,7 @@ set_variant_num_fprs (struct gdbarch_tdep *var, int num_fprs) { char buf[20]; - sprintf (buf, "fr%d", r); + xsnprintf (buf, sizeof (buf), "fr%d", r); var->register_names[first_fpr_regnum + r] = xstrdup (buf); } } diff --git a/gdb/m68hc11-tdep.c b/gdb/m68hc11-tdep.c index ff4cb99895..0b01387279 100644 --- a/gdb/m68hc11-tdep.c +++ b/gdb/m68hc11-tdep.c @@ -247,7 +247,7 @@ m68hc11_initialize_register_info (void) { char buf[10]; - sprintf (buf, "_.d%d", i - SOFT_D1_REGNUM + 1); + xsnprintf (buf, sizeof (buf), "_.d%d", i - SOFT_D1_REGNUM + 1); m68hc11_get_register_info (&soft_regs[i], buf); } diff --git a/gdb/mi/mi-out.c b/gdb/mi/mi-out.c index b39e05a3f6..9f2205c452 100644 --- a/gdb/mi/mi-out.c +++ b/gdb/mi/mi-out.c @@ -198,7 +198,7 @@ mi_field_int (struct ui_out *uiout, int fldno, int width, if (data->suppress_output) return; - sprintf (buffer, "%d", value); + xsnprintf (buffer, sizeof (buffer), "%d", value); mi_field_string (uiout, fldno, width, alignment, fldname, buffer); } diff --git a/gdb/nto-tdep.c b/gdb/nto-tdep.c index 7c4894638c..dff87bbf96 100644 --- a/gdb/nto-tdep.c +++ b/gdb/nto-tdep.c @@ -89,7 +89,7 @@ nto_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname) char *buf, *arch_path, *nto_root, *endian; const char *base; const char *arch; - int ret; + int arch_len, len, ret; #define PATH_FMT \ "%s/lib:%s/usr/lib:%s/usr/photon/lib:%s/usr/photon/dll:%s/lib/dll" @@ -116,20 +116,22 @@ nto_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname) /* In case nto_root is short, add strlen(solib) so we can reuse arch_path below. */ - arch_path = - alloca (strlen (nto_root) + strlen (arch) + strlen (endian) + 2 + - strlen (solib)); - sprintf (arch_path, "%s/%s%s", nto_root, arch, endian); - buf = alloca (strlen (PATH_FMT) + strlen (arch_path) * 5 + 1); - sprintf (buf, PATH_FMT, arch_path, arch_path, arch_path, arch_path, - arch_path); + arch_len = (strlen (nto_root) + strlen (arch) + strlen (endian) + 2 + + strlen (solib)); + arch_path = alloca (arch_len); + xsnprintf (arch_path, arch_len, "%s/%s%s", nto_root, arch, endian); + + len = strlen (PATH_FMT) + strlen (arch_path) * 5 + 1; + buf = alloca (len); + xsnprintf (buf, len, PATH_FMT, arch_path, arch_path, arch_path, arch_path, + arch_path); base = lbasename (solib); ret = openp (buf, 1, base, o_flags, temp_pathname); if (ret < 0 && base != solib) { - sprintf (arch_path, "/%s", solib); + xsnprintf (arch_path, arch_len, "/%s", solib); ret = open (arch_path, o_flags, 0); if (temp_pathname) { @@ -170,9 +172,9 @@ nto_init_solib_absolute_prefix (void) == BFD_ENDIAN_BIG ? "be" : "le"; } - sprintf (arch_path, "%s/%s%s", nto_root, arch, endian); + xsnprintf (arch_path, sizeof (arch_path), "%s/%s%s", nto_root, arch, endian); - sprintf (buf, "set solib-absolute-prefix %s", arch_path); + xsnprintf (buf, sizeof (buf), "set solib-absolute-prefix %s", arch_path); execute_command (buf, 0); } diff --git a/gdb/source.c b/gdb/source.c index 9813c43faa..c05822e468 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -384,7 +384,7 @@ init_source_path (void) { char buf[20]; - sprintf (buf, "$cdir%c$cwd", DIRNAME_SEPARATOR); + xsnprintf (buf, sizeof (buf), "$cdir%c$cwd", DIRNAME_SEPARATOR); source_path = xstrdup (buf); forget_cached_source_info (); } @@ -1292,8 +1292,10 @@ print_source_lines_base (struct symtab *s, int line, int stopline, int noerror) if (!noerror) { - char *name = alloca (strlen (s->filename) + 100); - sprintf (name, "%d\t%s", line, s->filename); + int len = strlen (s->filename) + 100; + char *name = alloca (len); + + xsnprintf (name, len, "%d\t%s", line, s->filename); print_sys_errmsg (name, errno); } else @@ -1344,13 +1346,13 @@ print_source_lines_base (struct symtab *s, int line, int stopline, int noerror) if (c == EOF) break; last_line_listed = current_source_line; - sprintf (buf, "%d\t", current_source_line++); + xsnprintf (buf, sizeof (buf), "%d\t", current_source_line++); ui_out_text (uiout, buf); do { if (c < 040 && c != '\t' && c != '\n' && c != '\r') { - sprintf (buf, "^%c", c + 0100); + xsnprintf (buf, sizeof (buf), "^%c", c + 0100); ui_out_text (uiout, buf); } else if (c == 0177) @@ -1367,7 +1369,7 @@ print_source_lines_base (struct symtab *s, int line, int stopline, int noerror) } else { - sprintf (buf, "%c", c); + xsnprintf (buf, sizeof (buf), "%c", c); ui_out_text (uiout, buf); } } diff --git a/gdb/valprint.c b/gdb/valprint.c index 8600be03dc..8fe1224374 100644 --- a/gdb/valprint.c +++ b/gdb/valprint.c @@ -1974,9 +1974,10 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig, /* If the value fits in 3 octal digits, print it that way. Otherwise, print it as a hex escape. */ if (value <= 0777) - sprintf (octal, "\\%.3o", (int) (value & 0777)); + xsnprintf (octal, sizeof (octal), "\\%.3o", + (int) (value & 0777)); else - sprintf (octal, "\\x%lx", (long) value); + xsnprintf (octal, sizeof (octal), "\\x%lx", (long) value); append_string_as_wide (octal, output); } /* If we somehow have extra bytes, print them now. */ @@ -1984,7 +1985,7 @@ print_wchar (gdb_wint_t w, const gdb_byte *orig, { char octal[5]; - sprintf (octal, "\\%.3o", orig[i] & 0xff); + xsnprintf (octal, sizeof (octal), "\\%.3o", orig[i] & 0xff); append_string_as_wide (octal, output); ++i; } diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c index a8766cd550..75831d821c 100644 --- a/gdb/windows-nat.c +++ b/gdb/windows-nat.c @@ -1895,7 +1895,8 @@ windows_pid_to_exec_file (int pid) /* Try to find exe name as symlink target of /proc//exe. */ int nchars; char procexe[sizeof ("/proc/4294967295/exe")]; - sprintf (procexe, "/proc/%u/exe", pid); + + xsnprintf (procexe, sizeof (procexe), "/proc/%u/exe", pid); nchars = readlink (procexe, path, sizeof(path)); if (nchars > 0 && nchars < sizeof (path)) { @@ -2093,10 +2094,10 @@ windows_create_inferior (struct target_ops *ops, char *exec_file, cygallargs = (wchar_t *) alloca (len * sizeof (wchar_t)); swprintf (cygallargs, len, L" -c 'exec %s %s'", exec_file, allargs); #else - cygallargs = (char *) - alloca (sizeof (" -c 'exec '") + strlen (exec_file) - + strlen (allargs) + 2); - sprintf (cygallargs, " -c 'exec %s %s'", exec_file, allargs); + len = (sizeof (" -c 'exec '") + strlen (exec_file) + + strlen (allargs) + 2); + cygallargs = (char *) alloca (len); + xsnprintf (cygallargs, len, " -c 'exec %s %s'", exec_file, allargs); #endif toexec = shell; flags |= DEBUG_PROCESS; @@ -2724,8 +2725,9 @@ _initialize_check_for_gdb_ini (void) { int len = strlen (oldini); char *newini = alloca (len + 1); - sprintf (newini, "%.*s.gdbinit", - (int) (len - (sizeof ("gdb.ini") - 1)), oldini); + + xsnprintf (newini, len + 1, "%.*s.gdbinit", + (int) (len - (sizeof ("gdb.ini") - 1)), oldini); warning (_("obsolete '%s' found. Rename to '%s'."), oldini, newini); } } -- 2.34.1