[PATCH v2] gas/config/tc-tic4x.c: Let insn name full use its space and always zero terminated

Chen Gang gang.chen.5i5j@gmail.com
Tue Nov 25 14:19:00 GMT 2014


strncpy() can not be sure of insn name must be zero terminated, so need
set it explicitly. And also need use snprintf() instead of strcat() and
strncat().

At present, insn name is only useful in tic4x_insn_check() which only
test insn name within 9 chars explicitly. So it is harmless to use full
space of insn name for strncpy().

 - tic4x_insn_t is defined within this file, and "tc-tic4x.c" is not
   included by other files, so can only consider about tic4x_insn_t
   within "c-tic4x.c".

 - after search "tic4x_insn_t", can sure only tic4x_insn_check() use
   insn name. And 'the_insn' is only be referenced by 'insn'.

 - after search "insn", can sure insn name is set only in md_assemble().


2014-11-25  Chen Gang  <gang.chen.5i5j@gmail.com>

        * config/tc-tic4x.c (md_assemble): Let insn name full use its
        space and always zero terminated.
---
 gas/config/tc-tic4x.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/gas/config/tc-tic4x.c b/gas/config/tc-tic4x.c
index 12d8ec9..b57272c 100644
--- a/gas/config/tc-tic4x.c
+++ b/gas/config/tc-tic4x.c
@@ -2417,6 +2417,7 @@ md_assemble (char *str)
   char *s;
   int i;
   int parsed = 0;
+  size_t len;
   tic4x_inst_t *inst;		/* Instruction template.  */
   tic4x_inst_t *first_inst;
 
@@ -2455,8 +2456,8 @@ md_assemble (char *str)
 	s++;
       if (*s)			/* Null terminate for hash_find.  */
 	*s++ = '\0';		/* and skip past null.  */
-      strcat (insn->name, "_");
-      strncat (insn->name, str, TIC4X_NAME_MAX - 1 - strlen (insn->name));
+      len = strlen (insn->name);
+      snprintf (insn->name + len, TIC4X_NAME_MAX - len, "_%s", str);
 
       insn->operands[insn->num_operands++].mode = M_PARALLEL;
 
@@ -2518,7 +2519,8 @@ md_assemble (char *str)
 	s++;
       if (*s)			/* Null terminate for hash_find.  */
 	*s++ = '\0';		/* and skip past null.  */
-      strncpy (insn->name, str, TIC4X_NAME_MAX - 3);
+      strncpy (insn->name, str, TIC4X_NAME_MAX - 1);
+      insn->name[TIC4X_NAME_MAX - 1] = '\0';
 
       if ((i = tic4x_operands_parse (s, insn->operands, 0)) < 0)
 	{
-- 
1.9.3



More information about the Binutils mailing list