FT32: Remove recursion in ft32_opcode

Message ID 2BB0A51F073B384698CACFD1D5A30FCCD7FC3906@glaexch3
State New
Headers show
Series
  • FT32: Remove recursion in ft32_opcode
Related show

Commit Message

James Bowman (FTDI-UK) Aug. 24, 2021, 2:16 a.m.
The function ft32_opcode used recursion. This could cause a stack
overflow. Replaced with a pair of non-recursive functions.

https://sourceware.org/bugzilla/show_bug.cgi?id=28169


OK to apply?

James.


opcodes/ChangeLog:

2021-08-23  James Bowman  <james.bowman@ftdichip.com>

        * opcodes/ft32-dis.c (ft32_opcode): Split into two non-recursive functions

---

Comments

Cooper Qu via Binutils Aug. 24, 2021, 11:25 a.m. | #1
On Tue, Aug 24, 2021 at 02:16:56AM +0000, James Bowman (FTDI-UK) wrote:
> The function ft32_opcode used recursion. This could cause a stack

> overflow. Replaced with a pair of non-recursive functions.

> 

> https://sourceware.org/bugzilla/show_bug.cgi?id=28169


Thanks, I applied this for you along with some formatting fixes.

James, would you like to be listed in binutils/MAINTAINERS for ft32?
If so, please add yourself to the file.  You'll then be able to commit
ft32 fixes without asking permission, and of course might occasionally
be annoyed by users wanting fixes.  On the latter subject, please
also create a bugzilla account for yourself at
https://sourceware.org/bugzilla/.

-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/opcodes/ft32-dis.c b/opcodes/ft32-dis.c
index cefc99d..07ece1f 100644
--- a/opcodes/ft32-dis.c
+++ b/opcodes/ft32-dis.c
@@ -40,9 +40,8 @@  sign_extend(int bit, int value)
 }
 
 static void
-ft32_opcode(bfd_vma addr ATTRIBUTE_UNUSED,
-            unsigned int iword,
-            struct disassemble_info *info)
+ft32_opcode1(unsigned int iword,
+             struct disassemble_info *info)
 {
   const ft32_opc_info_t *oo;
 
@@ -50,14 +49,6 @@  ft32_opcode(bfd_vma addr ATTRIBUTE_UNUSED,
     if ((iword & oo->mask) == oo->bits)
       break;
 
-  unsigned int sc[2];
-  if (ft32_decode_shortcode((unsigned int)addr, iword, sc))
-    {
-      ft32_opcode(addr, sc[0], info);
-      fpr (stream, " ; ");
-      ft32_opcode(addr, sc[1], info);
-    }
-
   if (oo->name)
     {
       int f = oo->fields;
@@ -174,6 +165,24 @@  ft32_opcode(bfd_vma addr ATTRIBUTE_UNUSED,
     }
 }
 
+static void
+ft32_opcode(bfd_vma addr ATTRIBUTE_UNUSED,
+            unsigned int iword,
+            struct disassemble_info *info)
+{
+  unsigned int sc[2];
+  if (ft32_decode_shortcode((unsigned int)addr, iword, sc))
+    {
+      ft32_opcode1(sc[0], info);
+      fpr (stream, " ; ");
+      ft32_opcode1(sc[1], info);
+    }
+  else
+    {
+       ft32_opcode1(iword, info);
+    }
+}
+
 int
 print_insn_ft32 (bfd_vma addr, struct disassemble_info *info)
 {