]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111719: Add extra check for alias command (#111720)
authorTian Gao <gaogaotiantian@hotmail.com>
Sat, 4 Nov 2023 23:05:22 +0000 (16:05 -0700)
committerGitHub <noreply@github.com>
Sat, 4 Nov 2023 23:05:22 +0000 (23:05 +0000)
Doc/library/pdb.rst
Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS.d/next/Library/2023-11-04-01-20-23.gh-issue-111719.fUiKBD.rst [new file with mode: 0644]

index 002eeef4c09b5d29ee02c1d4f01cdceb6f6ccda9..bbc6aacc62aafa7b67b0ffcd2be4e02b75756ea9 100644 (file)
@@ -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.
 
index dbb7f556555bccc3539263953faafdde1051ac55..a4b02e010a6be6a47ef5575848b9c71f51b08e07 100755 (executable)
@@ -597,11 +597,20 @@ class Pdb(bdb.Bdb, cmd.Cmd):
         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 "!"
+                    break
+
             line = line.replace("%*", ' '.join(args[1:]))
             args = line.split()
         # split into ';;' separated commands
@@ -616,6 +625,7 @@ class Pdb(bdb.Bdb, cmd.Cmd):
 
         # Replace all the convenience variables
         line = re.sub(r'\$([a-zA-Z_][a-zA-Z0-9_]*)', r'__pdb_convenience_variables["\1"]', line)
+
         return line
 
     def onecmd(self, line):
@@ -1797,7 +1807,18 @@ class Pdb(bdb.Bdb, cmd.Cmd):
             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
index ff9e7c2142fe331576b99ceb1daed549479cc750..5508f7bff3799405c155f659e8e5f4f6b7e95946 100644 (file)
@@ -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()
@@ -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
     """
 
diff --git a/Misc/NEWS.d/next/Library/2023-11-04-01-20-23.gh-issue-111719.fUiKBD.rst b/Misc/NEWS.d/next/Library/2023-11-04-01-20-23.gh-issue-111719.fUiKBD.rst
new file mode 100644 (file)
index 0000000..1d3b948
--- /dev/null
@@ -0,0 +1 @@
+Add extra argument validation for ``alias`` command in :mod:`pdb`