gas/arc: Fix array overrun when checking opcode array
authorAndrew Burgess <andrew.burgess@embecosm.com>
Wed, 4 May 2016 12:57:10 +0000 (13:57 +0100)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Wed, 18 May 2016 21:23:40 +0000 (22:23 +0100)
The opcode array iterator mechanism can, in some situations, result in
reading memory outside of the opcode array.  When using the
iterator-next mechanism to find the next possible arc_opcode, if we find
an opcode where the name field is NULL, or the name does not match, then
the cached opcode pointer is not set to NULL.  The result is that
another call to iterator-next will again increment the opcode
pointer (which might now point outside the opcode array) and attempt to
access the name field of this undefined opcode.

Fixed in this commit by clearing the cached opcode pointer.

I've added a test case, which currently shows the bug, however, this
will only expose this bug while the opcode used (dsp_fp_cmp) is the last
opcode in the table.

gas/ChangeLog:

* config/tc-arc.c (arc_opcode_hash_entry_iterator_next): Set
cached opcode to NULL when we reach a non-matching opcode.
* testsuite/gas/arc/asm-errors-2.d: New file.
* testsuite/gas/arc/asm-errors-2.err: New file.
* testsuite/gas/arc/asm-errors-2.s: New file.

gas/ChangeLog
gas/config/tc-arc.c
gas/testsuite/gas/arc/asm-errors-2.d [new file with mode: 0644]
gas/testsuite/gas/arc/asm-errors-2.err [new file with mode: 0644]
gas/testsuite/gas/arc/asm-errors-2.s [new file with mode: 0644]

index bd529cd895444573d8461925e6c3e276f47f27d9..ac921abf22f91e448558765bb661fae652f2e49f 100644 (file)
@@ -1,3 +1,11 @@
+2016-05-18  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * config/tc-arc.c (arc_opcode_hash_entry_iterator_next): Set
+       cached opcode to NULL when we reach a non-matching opcode.
+       * testsuite/gas/arc/asm-errors-2.d: New file.
+       * testsuite/gas/arc/asm-errors-2.err: New file.
+       * testsuite/gas/arc/asm-errors-2.s: New file.
+
 2016-05-18  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * config/tc-arc.c (tokenize_arguments): Add checks for array
index ca94b1f6d9b9e8d4cc2e897c78efedded94394f6..2f43be5ce39105e42721f2da7b4127376dd9bc10 100644 (file)
@@ -674,8 +674,8 @@ arc_opcode_hash_entry_iterator_next (const struct arc_opcode_hash_entry *entry,
       const char *old_name = iter->opcode->name;
 
       iter->opcode++;
-      if (iter->opcode->name
-         && (strcmp (old_name, iter->opcode->name) != 0))
+      if (iter->opcode->name == NULL
+         || strcmp (old_name, iter->opcode->name) != 0)
        {
          iter->index++;
          if (iter->index == entry->count)
diff --git a/gas/testsuite/gas/arc/asm-errors-2.d b/gas/testsuite/gas/arc/asm-errors-2.d
new file mode 100644 (file)
index 0000000..fd3c09a
--- /dev/null
@@ -0,0 +1,2 @@
+#as: -mcpu=arcem
+#error-output: asm-errors-2.err
diff --git a/gas/testsuite/gas/arc/asm-errors-2.err b/gas/testsuite/gas/arc/asm-errors-2.err
new file mode 100644 (file)
index 0000000..64fdc9a
--- /dev/null
@@ -0,0 +1,2 @@
+[^:]*: Assembler messages:
+[^:]*:2: Error: inappropriate arguments for opcode 'dsp_fp_cmp'
diff --git a/gas/testsuite/gas/arc/asm-errors-2.s b/gas/testsuite/gas/arc/asm-errors-2.s
new file mode 100644 (file)
index 0000000..f5bf8da
--- /dev/null
@@ -0,0 +1,2 @@
+        .text
+        dsp_fp_cmp      r0