Skip to contentSkip to conversation

pygrub: fix IndexError when editing grub lines

PATCH Applied Ryan Harper 2 participants 2 messages Nov 7, 2006 – Nov 9, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pygrub: fix IndexError when editing grub lines

pygrub will attempt to pop out-of-range values from a list if one sets the cursor past the end of the last character and hits backspace while in edit mode.

The attached patch fixes the stack trace below by not allowing the cursor to move beyond the end of the line and also checks that the current position is within the range of the line before popping.

Please apply.

Traceback (most recent call last):
File "/usr/bin/pygrub", line 491, in ?
curses.wrapper(run_main)
File "/usr/lib64/python2.4/curses/wrapper.py", line 44, in wrapper
return func(stdscr, *args, **kwds)
File "/usr/bin/pygrub", line 447, in run_main

    sel = g.run()
  File "/usr/bin/pygrub", line 353, in run
    self.run_main(timeout)
  File "/usr/bin/pygrub", line 405, in run_main
    self.edit_entry(img)
  File "/usr/bin/pygrub", line 234, in edit_entry
    l = self.edit_line(img.lines[curline])
  File "/usr/bin/pygrub", line 266, in edit_line
    ret = t.edit()

File "/usr/bin/pygrub", line 135, in edit
r = curses.textpad.Textbox.edit(self)
File "/usr/lib64/python2.4/curses/textpad.py", line 157, in edit
if not self.do_command(ch):
File "/usr/bin/pygrub", line 120, in do_command
self.line.pop(self.pos)
IndexError: pop index out of range

Signature · 36 lines
Ryan Harper
Software Engineer; Linux Technology Center
IBM Corp., Austin, Tx
(512) 838-9253   T/L: 678-9253
[email protected]


diffstat output:
 pygrub |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)

Signed-off-by: Ryan Harper <[email protected]>
---
diff -r 5a41ea282c86 tools/pygrub/src/pygrub
--- a/tools/pygrub/src/pygrub	Tue Nov 07 11:54:52 2006 +0000
+++ b/tools/pygrub/src/pygrub	Tue Nov 07 14:20:11 2006 -0600
@@ -117,13 +117,17 @@ class GrubLineEditor(curses.textpad.Text
         elif ch in (curses.ascii.BS,curses.KEY_BACKSPACE):
             if self.pos > 0:
                 self.pos -= 1
-                self.line.pop(self.pos)
+                # only pop char if pos is within line
+                if self.pos <= len(self.line):
+                    self.line.pop(self.pos)
         elif ch == curses.ascii.EOT:                           # ^d
             self.line.pop(self.pos)
         elif ch == curses.ascii.ENQ:                           # ^e
             self.pos = len(self.line)
         elif ch in (curses.ascii.ACK, curses.KEY_RIGHT):
-            self.pos +=1
+            # only allow cursor to move to end of the line
+            if self.pos+1 <= len(self.line):
+                self.pos +=1
         elif ch == curses.ascii.VT:                            # ^k
             self.line = self.line[:self.pos]
         else:
Applied
#1 lore raw
Quoted 9 lines · Ryan Harper

pygrub will attempt to pop out-of-range values from a list if one sets the cursor past the end of the last character and hits backspace while in edit mode.

The attached patch fixes the stack trace below by not allowing the cursor to move beyond the end of the line and also checks that the current position is within the range of the line before popping.

Please apply.

Applied, thank you (plus a few other bounds checks).

Cheers,

Tim.