* [PATCH] use xstrdup and friends more
@ 2016-03-29 10:34 tbsaunde+binutils
2016-03-29 11:27 ` Alan Modra
0 siblings, 1 reply; 4+ messages in thread
From: tbsaunde+binutils @ 2016-03-29 10:34 UTC (permalink / raw)
To: binutils; +Cc: Trevor Saunders
From: Trevor Saunders <tbsaunde+binutils@tbsaunde.org>
Hi,
$subject, the diffstat should speak for it self :)
built and tested crosses to hppa-linux, mips-elf, tic4x-coff, tic54x and
xtensa-elf, ok?
Trev
gas/ChangeLog:
2016-03-29 Trevor Saunders <tbsaunde+binutils@tbsaunde.org>
* config/tc-hppa.c (pa_space): Use xstrdup where appropriate.
(pa_subspace): Likewise.
(create_new_space): Likewise.
(create_new_subspace): Likewise.
* config/tc-mips.c (mips_lookup_insn): Likewise.
* config/tc-tic4x.c (tic4x_asg): Likewise.
* config/tc-tic54x.c (tic54x_eval): Likewise.
(stag_add_field): Likewise.
(tic54x_usect): Likewise.
(tic54x_clink): Likewise.
(tic54x_set_default_include): Likewise.
(tic54x_include): Likewise.
(tic54x_message): Likewise.
(tic54x_sblock): Likewise.
(tic54x_var): Likewise.
(subsym_ismember): Likewise.
(subsym_substitute): Likewise.
* config/tc-xtensa.c (xg_replace_opname): Likewise.
(xg_translate_sysreg_op): Likewise.
(xg_translate_idioms): Likewise.
(md_assemble): Likewise.
(cache_literal_section): Likewise.
---
gas/config/tc-hppa.c | 12 ++++--------
gas/config/tc-mips.c | 4 +---
gas/config/tc-tic4x.c | 9 ++-------
gas/config/tc-tic54x.c | 34 +++++++++++++---------------------
gas/config/tc-xtensa.c | 32 +++++++++-----------------------
5 files changed, 29 insertions(+), 62 deletions(-)
diff --git a/gas/config/tc-hppa.c b/gas/config/tc-hppa.c
index c3e1d75..15e32e1 100644
--- a/gas/config/tc-hppa.c
+++ b/gas/config/tc-hppa.c
@@ -7300,8 +7300,7 @@ pa_space (int unused ATTRIBUTE_UNUSED)
print_errors = 1;
input_line_pointer = save_s;
c = get_symbol_name (&name);
- space_name = xmalloc (strlen (name) + 1);
- strcpy (space_name, name);
+ space_name = xstrdup (name);
(void) restore_line_pointer (c);
sd_chain = pa_parse_space_stmt (space_name, 1);
@@ -7365,8 +7364,7 @@ pa_subspace (int create_new)
else
{
c = get_symbol_name (&name);
- ss_name = xmalloc (strlen (name) + 1);
- strcpy (ss_name, name);
+ ss_name = xstrdup (name);
(void) restore_line_pointer (c);
/* Load default values. */
@@ -7725,8 +7723,7 @@ create_new_space (char *name,
as_fatal (_("Out of memory: could not allocate new space chain entry: %s\n"),
name);
- SPACE_NAME (chain_entry) = xmalloc (strlen (name) + 1);
- strcpy (SPACE_NAME (chain_entry), name);
+ SPACE_NAME (chain_entry) = xstrdup (name);
SPACE_DEFINED (chain_entry) = defined;
SPACE_USER_DEFINED (chain_entry) = user_defined;
SPACE_SPNUM (chain_entry) = spnum;
@@ -7812,8 +7809,7 @@ create_new_subspace (sd_chain_struct *space,
if (!chain_entry)
as_fatal (_("Out of memory: could not allocate new subspace chain entry: %s\n"), name);
- SUBSPACE_NAME (chain_entry) = xmalloc (strlen (name) + 1);
- strcpy (SUBSPACE_NAME (chain_entry), name);
+ SUBSPACE_NAME (chain_entry) = xstrdup (name);
/* Initialize subspace_defined. When we hit a .subspace directive
we'll set it to 1 which "locks-in" the subspace attributes. */
diff --git a/gas/config/tc-mips.c b/gas/config/tc-mips.c
index d089362..7db13b1 100644
--- a/gas/config/tc-mips.c
+++ b/gas/config/tc-mips.c
@@ -13531,9 +13531,7 @@ mips_lookup_insn (struct hash_control *hash, const char *start,
struct mips_opcode *insn;
/* Make a copy of the instruction so that we can fiddle with it. */
- name = xmalloc (length + 1);
- memcpy (name, start, length);
- name[length] = '\0';
+ name = xstrndup (start, length);
/* Look up the instruction as-is. */
insn = (struct mips_opcode *) hash_find (hash, name);
diff --git a/gas/config/tc-tic4x.c b/gas/config/tc-tic4x.c
index 21e6e36..048b184b 100644
--- a/gas/config/tc-tic4x.c
+++ b/gas/config/tc-tic4x.c
@@ -713,7 +713,6 @@ tic4x_asg (int x ATTRIBUTE_UNUSED)
char c;
char *name;
char *str;
- char *tmp;
SKIP_WHITESPACE ();
str = input_line_pointer;
@@ -728,12 +727,8 @@ tic4x_asg (int x ATTRIBUTE_UNUSED)
}
*input_line_pointer++ = '\0';
c = get_symbol_name (&name); /* Get terminator. */
- tmp = xmalloc (strlen (str) + 1);
- strcpy (tmp, str);
- str = tmp;
- tmp = xmalloc (strlen (name) + 1);
- strcpy (tmp, name);
- name = tmp;
+ str = xstrdup (str);
+ name = xstrdup (name);
if (hash_find (tic4x_asg_hash, name))
hash_replace (tic4x_asg_hash, name, (void *) str);
else
diff --git a/gas/config/tc-tic54x.c b/gas/config/tc-tic54x.c
index d718a33..a473289 100644
--- a/gas/config/tc-tic54x.c
+++ b/gas/config/tc-tic54x.c
@@ -407,8 +407,7 @@ tic54x_eval (int x ATTRIBUTE_UNUSED)
return;
}
c = get_symbol_name (&name); /* Get terminator. */
- tmp = xmalloc (strlen (name) + 1);
- name = strcpy (tmp, name);
+ name = xstrdup (name);
(void) restore_line_pointer (c);
if (!ISALPHA (*name))
@@ -426,8 +425,7 @@ tic54x_eval (int x ATTRIBUTE_UNUSED)
But since there's not written rule as to when, don't even bother trying
to match their behavior. */
sprintf (valuestr, "%d", value);
- tmp = xmalloc (strlen (valuestr) + 1);
- strcpy (tmp, valuestr);
+ tmp = xstrdup (valuestr);
subsym_create_or_replace (name, tmp);
demand_empty_rest_of_line ();
@@ -598,7 +596,7 @@ stag_add_field (struct stag *parent,
struct stag_field *sfield = xmalloc (sizeof (struct stag_field));
memset (sfield, 0, sizeof (*sfield));
- sfield->name = strcpy (xmalloc (strlen (name) + 1), name);
+ sfield->name = xstrdup (name);
sfield->offset = offset;
sfield->bitfield_offset = parent->current_bitfield_offset;
sfield->stag = stag;
@@ -1361,8 +1359,7 @@ tic54x_usect (int x ATTRIBUTE_UNUSED)
current_subseg = now_subseg; /* Save current subseg. */
c = get_symbol_name (§ion_name); /* Get terminator. */
- name = xmalloc (input_line_pointer - section_name + 1);
- strcpy (name, section_name);
+ name = xstrdup (section_name);
c = restore_line_pointer (c);
if (c == ',')
@@ -1831,8 +1828,7 @@ tic54x_clink (int ignored ATTRIBUTE_UNUSED)
;
know (input_line_pointer[-1] == '\"');
input_line_pointer[-1] = 0;
- name = xmalloc (input_line_pointer - section_name + 1);
- strcpy (name, section_name);
+ name = xstrdup (section_name);
seg = bfd_get_section_by_name (stdoutput, name);
if (seg == NULL)
@@ -1874,7 +1870,7 @@ tic54x_set_default_include (int dot)
unsigned lineno;
curfile = as_where (&lineno);
- dir = strcpy (xmalloc (strlen (curfile) + 1), curfile);
+ dir = xstrdup (curfile);
tmp = strrchr (dir, '/');
}
if (tmp != NULL)
@@ -1931,7 +1927,7 @@ tic54x_include (int ignored ATTRIBUTE_UNUSED)
++input_line_pointer;
c = *input_line_pointer;
*input_line_pointer = '\0';
- filename = strcpy (xmalloc (strlen (filename) + 1), filename);
+ filename = xstrdup (filename);
*input_line_pointer = c;
demand_empty_rest_of_line ();
}
@@ -1968,7 +1964,7 @@ tic54x_message (int type)
++input_line_pointer;
c = *input_line_pointer;
*input_line_pointer = 0;
- msg = strcpy (xmalloc (strlen (msg) + 1), msg);
+ msg = xstrdup (msg);
*input_line_pointer = c;
}
@@ -2135,8 +2131,7 @@ tic54x_sblock (int ignore ATTRIBUTE_UNUSED)
char *section_name;
c = get_symbol_name (§ion_name);
- name = xmalloc (strlen (section_name) + 1);
- strcpy (name, section_name);
+ name = xstrdup (section_name);
(void) restore_line_pointer (c);
}
@@ -2249,7 +2244,7 @@ tic54x_var (int ignore ATTRIBUTE_UNUSED)
}
c = get_symbol_name (&name);
/* .var symbols start out with a null string. */
- name = strcpy (xmalloc (strlen (name) + 1), name);
+ name = xstrdup (name);
hash_insert (subsym_hash[macro_level], name, empty);
c = restore_line_pointer (c);
if (c == ',')
@@ -2617,8 +2612,7 @@ subsym_ismember (char *sym, char *list)
return 0;
}
- ptr = elem = xmalloc (strlen (listv) + 1);
- strcpy (elem, listv);
+ ptr = elem = xstrdup (listv);
while (*ptr && *ptr != ',')
++ptr;
*ptr++ = 0;
@@ -4411,8 +4405,7 @@ subsym_substitute (char *line, int forced)
char *tmp;
/* Work with a copy of the input line. */
- replacement = xmalloc (strlen (line) + 1);
- strcpy (replacement, line);
+ replacement = xstrdup (line);
ptr = head = replacement;
@@ -4667,8 +4660,7 @@ subsym_substitute (char *line, int forced)
kinda indicates that forced substitution is not
supposed to be recursive, but I'm not sure. */
unsigned beg, len = 1; /* default to a single char */
- char *newval = strcpy (xmalloc (strlen (value) + 1),
- value);
+ char *newval = xstrdup (value);
savedp = input_line_pointer;
input_line_pointer = tail + 1;
diff --git a/gas/config/tc-xtensa.c b/gas/config/tc-xtensa.c
index 180181c..30631b7 100644
--- a/gas/config/tc-xtensa.c
+++ b/gas/config/tc-xtensa.c
@@ -2272,8 +2272,7 @@ static void
xg_replace_opname (char **popname, const char *newop)
{
free (*popname);
- *popname = (char *) xmalloc (strlen (newop) + 1);
- strcpy (*popname, newop);
+ *popname = xstrdup (newop);
}
@@ -2368,8 +2367,7 @@ xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings)
/* Another special case for "WSR.INTSET".... */
if (is_write && !is_user && !strcasecmp ("interrupt", sr_name))
sr_name = "intset";
- new_opname = (char *) xmalloc (strlen (sr_name) + 6);
- sprintf (new_opname, "%s.%s", *popname, sr_name);
+ new_opname = concat (*popname, ".", sr_name, NULL);
free (*popname);
*popname = new_opname;
@@ -2493,8 +2491,7 @@ xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings)
if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
return -1;
xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
- arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
- strcpy (arg_strings[2], arg_strings[1]);
+ arg_strings[2] = xstrdup (arg_strings[1]);
*pnum_args = 3;
}
return 0;
@@ -2534,12 +2531,9 @@ xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings)
if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
return -1;
xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
- arg_strings[0] = (char *) xmalloc (3);
- arg_strings[1] = (char *) xmalloc (3);
- arg_strings[2] = (char *) xmalloc (3);
- strcpy (arg_strings[0], "a1");
- strcpy (arg_strings[1], "a1");
- strcpy (arg_strings[2], "a1");
+ arg_strings[0] = xstrdup ("a1");
+ arg_strings[1] = xstrdup ("a1");
+ arg_strings[2] = xstrdup ("a1");
*pnum_args = 3;
}
return 0;
@@ -5499,9 +5493,7 @@ md_assemble (char *str)
/* Split off the opcode. */
opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
- opname = xmalloc (opnamelen + 1);
- memcpy (opname, str, opnamelen);
- opname[opnamelen] = '\0';
+ opname = xstrndup (str, opnamelen);
num_args = tokenize_arguments (arg_strings, str + opnamelen);
if (num_args == -1)
@@ -11557,19 +11549,13 @@ cache_literal_section (bfd_boolean use_abs_literals)
base_name = use_abs_literals ? ".lit4" : ".literal";
if (group_name)
{
- name = xmalloc (strlen (base_name) + strlen (group_name) + 2);
- sprintf (name, "%s.%s", base_name, group_name);
+ name = concat (base_name, ".", group_name, NULL);
}
else if (strncmp (text_name, ".gnu.linkonce.", linkonce_len) == 0)
{
suffix = strchr (text_name + linkonce_len, '.');
- name = xmalloc (linkonce_len + strlen (base_name) + 1
- + (suffix ? strlen (suffix) : 0));
- strcpy (name, ".gnu.linkonce");
- strcat (name, base_name);
- if (suffix)
- strcat (name, suffix);
+ name = concat (".gnu.linkonce", base_name, suffix ? suffix : "", NULL);
linkonce = TRUE;
}
else
--
2.1.4
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] use xstrdup and friends more
2016-03-29 10:34 [PATCH] use xstrdup and friends more tbsaunde+binutils
@ 2016-03-29 11:27 ` Alan Modra
0 siblings, 0 replies; 4+ messages in thread
From: Alan Modra @ 2016-03-29 11:27 UTC (permalink / raw)
To: tbsaunde+binutils; +Cc: binutils
On Tue, Mar 29, 2016 at 06:37:37AM -0400, tbsaunde+binutils@tbsaunde.org wrote:
> * config/tc-hppa.c (pa_space): Use xstrdup where appropriate.
> (pa_subspace): Likewise.
> (create_new_space): Likewise.
> (create_new_subspace): Likewise.
> * config/tc-mips.c (mips_lookup_insn): Likewise.
> * config/tc-tic4x.c (tic4x_asg): Likewise.
> * config/tc-tic54x.c (tic54x_eval): Likewise.
> (stag_add_field): Likewise.
> (tic54x_usect): Likewise.
> (tic54x_clink): Likewise.
> (tic54x_set_default_include): Likewise.
> (tic54x_include): Likewise.
> (tic54x_message): Likewise.
> (tic54x_sblock): Likewise.
> (tic54x_var): Likewise.
> (subsym_ismember): Likewise.
> (subsym_substitute): Likewise.
> * config/tc-xtensa.c (xg_replace_opname): Likewise.
> (xg_translate_sysreg_op): Likewise.
> (xg_translate_idioms): Likewise.
> (md_assemble): Likewise.
> (cache_literal_section): Likewise.
OK, but
> - new_opname = (char *) xmalloc (strlen (sr_name) + 6);
> - sprintf (new_opname, "%s.%s", *popname, sr_name);
> + new_opname = concat (*popname, ".", sr_name, NULL);
here and other places you use concat, please pass (const char *) NULL
as the last argument. The reason being that we are passing an
argument corresponding to the ellipsis in the prototype, and if NULL
is simply defined as 0 it may be that ints are passed differently to
char*.
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] use xstrdup and friends more
2016-04-02 18:43 tbsaunde+binutils
@ 2016-04-03 22:56 ` Alan Modra
0 siblings, 0 replies; 4+ messages in thread
From: Alan Modra @ 2016-04-03 22:56 UTC (permalink / raw)
To: tbsaunde+binutils; +Cc: binutils
On Sat, Apr 02, 2016 at 02:48:16PM -0400, tbsaunde+binutils@tbsaunde.org wrote:
> * config/obj-elf.c (obj_elf_vendor_attribute): Use xstrdup.
> * config/tc-ppc.c (ppc_frob_file_before_adjust): Likewise.
> (ppc_znop): Likewise.
> (ppc_pe_section): Likewise.
> (ppc_frob_symbol): Likewise.
> * config/tc-tic30.c (tic30_operand): Likewise.
> * config/tc-tic4x.c (tic4x_sect): Likewise.
> (tic4x_usect): Likewise.
OK.
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] use xstrdup and friends more
@ 2016-04-02 18:43 tbsaunde+binutils
2016-04-03 22:56 ` Alan Modra
0 siblings, 1 reply; 4+ messages in thread
From: tbsaunde+binutils @ 2016-04-02 18:43 UTC (permalink / raw)
To: binutils; +Cc: Trevor Saunders
From: Trevor Saunders <tbsaunde+binutils@tbsaunde.org>
Hi,
$subject, its a lot simpler.
built crosses and regtested ppc-elf, tic30-none-coff, and tic4x-coff, ok?
Trev
gas/ChangeLog:
2016-04-02 Trevor Saunders <tbsaunde+binutils@tbsaunde.org>
* config/obj-elf.c (obj_elf_vendor_attribute): Use xstrdup.
* config/tc-ppc.c (ppc_frob_file_before_adjust): Likewise.
(ppc_znop): Likewise.
(ppc_pe_section): Likewise.
(ppc_frob_symbol): Likewise.
* config/tc-tic30.c (tic30_operand): Likewise.
* config/tc-tic4x.c (tic4x_sect): Likewise.
(tic4x_usect): Likewise.
---
gas/config/obj-elf.c | 4 +---
gas/config/tc-ppc.c | 16 ++++------------
gas/config/tc-tic30.c | 8 ++------
gas/config/tc-tic4x.c | 6 ++----
4 files changed, 9 insertions(+), 25 deletions(-)
diff --git a/gas/config/obj-elf.c b/gas/config/obj-elf.c
index 1d07d4b..68290e2 100644
--- a/gas/config/obj-elf.c
+++ b/gas/config/obj-elf.c
@@ -1609,9 +1609,7 @@ obj_elf_vendor_attribute (int vendor)
if (i == 0)
goto bad;
- name = xmalloc (i + 1);
- memcpy (name, s, i);
- name[i] = '\0';
+ name = xstrndup (s, i);
#ifndef CONVERT_SYMBOLIC_ATTRIBUTE
#define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
diff --git a/gas/config/tc-ppc.c b/gas/config/tc-ppc.c
index cf4f96e..23a84a7 100644
--- a/gas/config/tc-ppc.c
+++ b/gas/config/tc-ppc.c
@@ -2390,7 +2390,6 @@ ppc_frob_file_before_adjust (void)
const char *name;
char *dotname;
symbolS *dotsym;
- size_t len;
name = S_GET_NAME (symp);
if (name[0] == '.')
@@ -2400,10 +2399,7 @@ ppc_frob_file_before_adjust (void)
|| S_IS_DEFINED (symp))
continue;
- len = strlen (name) + 1;
- dotname = xmalloc (len + 1);
- dotname[0] = '.';
- memcpy (dotname + 1, name, len);
+ dotname = concat (".", name, (char *) NULL);
dotsym = symbol_find_noref (dotname, 1);
free (dotname);
if (dotsym != NULL && (symbol_used_p (dotsym)
@@ -5195,8 +5191,7 @@ ppc_znop (int ignore ATTRIBUTE_UNUSED)
/* Strip out the symbol name. */
c = get_symbol_name (&symbol_name);
- name = xmalloc (input_line_pointer - symbol_name + 1);
- strcpy (name, symbol_name);
+ name = xstrdup (symbol_name);
sym = symbol_find_or_make (name);
@@ -5370,8 +5365,7 @@ ppc_pe_section (int ignore ATTRIBUTE_UNUSED)
c = get_symbol_name (§ion_name);
- name = xmalloc (input_line_pointer - section_name + 1);
- strcpy (name, section_name);
+ name = xstrdup (section_name);
*input_line_pointer = c;
@@ -5768,9 +5762,7 @@ ppc_frob_symbol (symbolS *sym)
char *snew;
len = s - name;
- snew = xmalloc (len + 1);
- memcpy (snew, name, len);
- snew[len] = '\0';
+ snew = xstrndup (name, len);
S_SET_NAME (sym, snew);
}
diff --git a/gas/config/tc-tic30.c b/gas/config/tc-tic30.c
index 71c6249..10653ed 100644
--- a/gas/config/tc-tic30.c
+++ b/gas/config/tc-tic30.c
@@ -604,9 +604,7 @@ tic30_operand (char *token)
segT retval;
debug ("Probably a label: %s\n", token);
- current_op->immediate.label = malloc (strlen (token) + 1);
- strcpy (current_op->immediate.label, token);
- current_op->immediate.label[strlen (token)] = '\0';
+ current_op->immediate.label = xstrdup (token);
save_input_line_pointer = input_line_pointer;
input_line_pointer = token;
@@ -634,9 +632,7 @@ tic30_operand (char *token)
for (count = 0; count < strlen (token); count++)
if (*(token + count) == '.')
current_op->immediate.decimal_found = 1;
- current_op->immediate.label = malloc (strlen (token) + 1);
- strcpy (current_op->immediate.label, token);
- current_op->immediate.label[strlen (token)] = '\0';
+ current_op->immediate.label = xstrdup (token);
current_op->immediate.f_number = (float) atof (token);
current_op->immediate.s_number = (int) atoi (token);
current_op->immediate.u_number = (unsigned int) atoi (token);
diff --git a/gas/config/tc-tic4x.c b/gas/config/tc-tic4x.c
index c44201d..ba0aa38 100644
--- a/gas/config/tc-tic4x.c
+++ b/gas/config/tc-tic4x.c
@@ -964,8 +964,7 @@ tic4x_sect (int x ATTRIBUTE_UNUSED)
if (c == '"')
c = * ++ input_line_pointer;
input_line_pointer++; /* Skip null symbol terminator. */
- name = xmalloc (input_line_pointer - section_name + 1);
- strcpy (name, section_name);
+ name = xstrdup (section_name);
/* TI C from version 5.0 allows a section name to contain a
subsection name as well. The subsection name is separated by a
@@ -1075,8 +1074,7 @@ tic4x_usect (int x ATTRIBUTE_UNUSED)
if (c == '"')
c = * ++ input_line_pointer;
input_line_pointer++; /* Skip null symbol terminator. */
- name = xmalloc (input_line_pointer - section_name + 1);
- strcpy (name, section_name);
+ name = xstrdup (section_name);
if (c == ',')
input_line_pointer =
--
2.1.4
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-04-03 22:56 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-29 10:34 [PATCH] use xstrdup and friends more tbsaunde+binutils
2016-03-29 11:27 ` Alan Modra
2016-04-02 18:43 tbsaunde+binutils
2016-04-03 22:56 ` Alan Modra
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).