Protect canonical operand prefixes from aliases

This commit is contained in:
2026-10-20 00:20:40 +00:00
parent c225a66c97
commit ccf8acd861
3 changed files with 48 additions and 7 deletions

View File

@@ -326,7 +326,7 @@ func traceCanonicalPath(roots map[string]*canonicalNode, path commandpath.Path)
continue
}
operandCount := len(path) - index
if current != nil && acceptsOperandCount(current.command, operandCount) {
if current != nil && acceptsOperandPrefix(current.command, operandCount) {
return commandAliasTrace{operandOwner: current}
}
return commandAliasTrace{}
@@ -334,14 +334,15 @@ func traceCanonicalPath(roots map[string]*canonicalNode, path commandpath.Path)
return commandAliasTrace{exact: current}
}
func acceptsOperandCount(command Command, count int) bool {
// acceptsOperandPrefix reports whether more operands could complete this prefix.
// A source need not already satisfy the command's minimum to intercept it.
func acceptsOperandPrefix(command Command, count int) bool {
if command.record == nil || command.record.payload == nil || !command.record.payload.hasRun() {
return false
}
arguments := command.record.payload.bridgeArguments()
minimum, maximum := 0, 0
maximum := 0
for _, argument := range arguments {
minimum += argument.Min
if maximum >= 0 {
if argument.Max < 0 {
maximum = -1
@@ -350,5 +351,5 @@ func acceptsOperandCount(command Command, count int) bool {
}
}
}
return count >= minimum && (maximum < 0 || count <= maximum)
return maximum < 0 || count <= maximum
}