Skip to content

gh-111719: Add extra check for alias command #111720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/pdb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ can be overridden by the local file.

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

Expand Down
33 changes: 27 additions & 6 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,20 @@ def precmd(self, line):
args = line.split()
while args[0] in self.aliases:
line = self.aliases[args[0]]
ii = 1
for tmpArg in args[1:]:
line = line.replace("%" + str(ii),
tmpArg)
ii += 1
for idx in range(1, 10):
if f'%{idx}' in line:
if idx >= len(args):
self.error(f"Not enough arguments for alias '{args[0]}'")
# This is a no-op
return "!"
line = line.replace(f'%{idx}', args[idx])
elif '%*' not in line:
if idx < len(args):
self.error(f"Too many arguments for alias '{args[0]}'")
# This is a no-op
return "!"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this return needed?

Copy link
Member Author

@gaogaotiantian gaogaotiantian Nov 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because onecmd expects a command, and returning a False-like value would be equivalent to returning an empty line - and cmd considers an empty line as "repeat last command". If you do "enter" in pdb, that's repeat last command.

break

line = line.replace("%*", ' '.join(args[1:]))
args = line.split()
# split into ';;' separated commands
Expand All @@ -616,6 +625,7 @@ def precmd(self, line):

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

return line

def onecmd(self, line):
Expand Down Expand Up @@ -1797,7 +1807,18 @@ def do_alias(self, arg):
else:
self.error(f"Unknown alias '{args[0]}'")
else:
self.aliases[args[0]] = ' '.join(args[1:])
# Do a validation check to make sure no replaceable parameters
# are skipped if %* is not used.
alias = ' '.join(args[1:])
if '%*' not in alias:
consecutive = True
for idx in range(1, 10):
if f'%{idx}' not in alias:
consecutive = False
if f'%{idx}' in alias and not consecutive:
self.error("Replaceable parameters must be consecutive")
return
self.aliases[args[0]] = alias

def do_unalias(self, arg):
"""unalias name
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,14 @@ def test_pdb_alias_command():
... 'pi o',
... 's',
... 'ps',
... 'alias myp p %2',
... 'alias myp',
... 'alias myp p %1',
... 'myp',
... 'myp 1',
... 'myp 1 2',
... 'alias repeat_second_arg p "%* %2"',
... 'repeat_second_arg 1 2 3',
... 'continue',
... ]):
... test_function()
Expand All @@ -692,6 +700,20 @@ def test_pdb_alias_command():
(Pdb) ps
self.attr1 = 10
self.attr2 = str
(Pdb) alias myp p %2
*** Replaceable parameters must be consecutive
(Pdb) alias myp
*** Unknown alias 'myp'
(Pdb) alias myp p %1
(Pdb) myp
*** Not enough arguments for alias 'myp'
(Pdb) myp 1
1
(Pdb) myp 1 2
*** Too many arguments for alias 'myp'
(Pdb) alias repeat_second_arg p "%* %2"
(Pdb) repeat_second_arg 1 2 3
'1 2 3 2'
(Pdb) continue
"""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add extra argument validation for ``alias`` command in :mod:`pdb`