Skip to contentSkip to conversation

gdbstub: Make generated compound literal array size explicit

PATCH In review Philippe Mathieu-Daudé 3 participants 3 messages Feb 16 – Feb 17

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gdbstub: Make generated compound literal array size explicit

GDBFeature::num_regs holds the number of registers, but when using the compound literal construction, if the last array entry is not set, the array will be shorter. Prevent array overrun by making the array length explicit,

---
 scripts/feature_to_c.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
scripts/feature_to_c.py +5 -2
index 807af0e685c..3aa62fb895f 100644
@@ -90,13 +90,16 @@ def writeliteral(indent, bytes):
     writeliteral(8, read)
     sys.stdout.write(',\n')
     writeliteral(8, bytes(feature_name, 'utf-8'))
-    sys.stdout.write(',\n        (const char * const []) {\n')
+    sys.stdout.write(',\n')
+    sys.stdout.write(f'        (const char * const [{num_regs}]) {{\n')
 
     for index, regname in enumerate(regnames):
         sys.stdout.write(f'            [{regnums[index] - base_reg}] =\n')
         writeliteral(16, bytes(regname, 'utf-8'))
         sys.stdout.write(',\n')
 
-    sys.stdout.write(f'        }},\n        {num_regs},\n    }},\n')
+    sys.stdout.write( '        },\n')
+    sys.stdout.write(f'        {num_regs},\n')
+    sys.stdout.write( '    },\n')
 
 sys.stdout.write('    { NULL }\n};\n')
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Philippe Mathieu-Daudé

GDBFeature::num_regs holds the number of registers, but when using the compound literal construction, if the last array entry is not set, the array will be shorter. Prevent array overrun by making the array length explicit,

---
  scripts/feature_to_c.py | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)

r~

Quoted 24 lines
scripts/feature_to_c.py +5 -2
index 807af0e685c..3aa62fb895f 100644
@@ -90,13 +90,16 @@ def writeliteral(indent, bytes):
      writeliteral(8, read)
      sys.stdout.write(',\n')
      writeliteral(8, bytes(feature_name, 'utf-8'))
-    sys.stdout.write(',\n        (const char * const []) {\n')
+    sys.stdout.write(',\n')
+    sys.stdout.write(f'        (const char * const [{num_regs}]) {{\n')
 
      for index, regname in enumerate(regnames):
          sys.stdout.write(f'            [{regnums[index] - base_reg}] =\n')
          writeliteral(16, bytes(regname, 'utf-8'))
          sys.stdout.write(',\n')
 
-    sys.stdout.write(f'        }},\n        {num_regs},\n    }},\n')
+    sys.stdout.write( '        },\n')
+    sys.stdout.write(f'        {num_regs},\n')
+    sys.stdout.write( '    },\n')
 
  sys.stdout.write('    { NULL }\n};\n')