8000 gh-111719: Add extra check for alias command (#111720) · python/cpython@853b4b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 853b4b5

Browse files
gh-111719: Add extra check for alias command (#111720)
1 parent a6c1c04 commit 853b4b5

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

Doc/library/pdb.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ can be overridden by the local file.
580580

581581
Create an alias called *name* that executes *command*. The *command* must
582582
*not* be enclosed in quotes. Replaceable parameters can be indicated by
583-
``%1``, ``%2``, and so on, while ``%*`` is replaced by all the parameters.
583+
``%1``, ``%2``, ... and ``%9``, while ``%*`` is replaced by all the parameters.
584584
If *command* is omitted, the current alias for *name* is shown. If no
585585
arguments are given, all aliases are listed.
586586

Lib/pdb.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,20 @@ def precmd(self, line):
597597
args = line.split()
598598
while args[0] in self.aliases:
599599
line = self.aliases[args[0]]
600-
ii = 1
601-
for tmpArg in args[1:]:
602-
line = line.replace("%" + str(ii),
603-
tmpArg)
604-
ii += 1
600+
for idx in range(1, 10):
601+
if f'%{idx}' in line:
602+
if idx >= len(args):
603+
self.error(f"Not enough arguments for alias '{args[0]}'")
604+
# This is a no-op
605+
return "!"
606+
line = line.replace(f'%{idx}', args[idx])
607+
elif '%*' not in line:
608+
if idx < len(args):
609+
self.error(f"Too many arguments for alias '{args[0]}'")
610+
# This is a no-op
611+
return "!"
612+
break
613+
605614
line = line.replace("%*", ' '.join(args[1:]))
606615
args = line.split()
607616
# split into ';;' separated commands
@@ -616,6 +625,7 @@ def precmd(self, line):
616625

617626
# Replace all the convenience variables
618627
line = re.sub(r'\$([a-zA-Z_][a-zA-Z0-9_]*)', r'__pdb_convenience_variables["\1"]', line)
628+
619629
return line
620630

621631
def onecmd(self, line):
@@ -1797,7 +1807,18 @@ def do_alias(self, arg):
17971807
else:
17981808
self.error(f"Unknown alias '{args[0]}'")
17991809
else:
1800-
self.aliases[args[0]] = ' '.join(args[1:])
1810+
# Do a validation check to make sure no replaceable parameters
1811+
# are skipped if %* is not used.
1812+
alias = ' '.join(args[1:])
1813+
if '%*' not in alias:
1814+
consecutive = True
1815+
for idx in range(1, 10):
1816+
if f'%{idx}' not in alias:
1817+
consecutive = False
1818+
if f'%{idx}' in alias and not consecutive:
1819+
self.error("Replaceable parameters must be consecutive")
1820+
return
1821+
self.aliases[args[0]] = alias
18011822

18021823
def do_unalias(self, arg):
18031824
"""unalias name

Lib/test/test_pdb.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,14 @@ def test_pdb_alias_command():
671671
... 'pi o',
672672
... 's',
673673
... 'ps',
674+
... 'alias myp p %2',
675+
... 'alias myp',
676+
... 'alias myp p %1',
677+
... 'myp',
678+
... 'myp 1',
679+
... 'myp 1 2',
680+
... 'alias repeat_second_arg p "%* %2"',
681+
... 'repeat_second_arg 1 2 3',
674682
... 'continue',
675683
... ]):
676684
... test_function()
@@ -692,6 +700,20 @@ def test_pdb_alias_command():
692700
(Pdb) ps
693701
self.attr1 = 10
694702
self.attr2 = str
703+
(Pdb) alias myp p %2
704+
*** Replaceable parameters must be consecutive
705+
(Pdb) alias myp
706+
*** Unknown alias 'myp'
707+
(Pdb) alias myp p %1
708+
(Pdb) myp
709+
*** Not enough arguments for alias 'myp'
710+
(Pdb) myp 1
711+
1
712+
(Pdb) myp 1 2
713+
*** Too many arguments for alias 'myp'
714+
(Pdb) alias repeat_second_arg p "%* %2"
715+
(Pdb) repeat_second_arg 1 2 3
716+
'1 2 3 2'
695717
(Pdb) continue
696718
"""
697719

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add extra argument validation for ``alias`` command in :mod:`pdb`

0 commit comments

Comments
 (0)
0