Skip to content

Commit 438083d

Browse files
committed
fix: meta character escaping
The cmdline processor treats each line of input as a string, not a regular rexpression. However, the assembler treats input as regular expressions. The output of the cmdline processor must, therefore, be properly escaped. The function responsible for handling escape sequences was missing the `+` character, causing literal `+` characters to end up being passed to the assembler.
1 parent 782b2a6 commit 438083d

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

regex/processors/cmdline.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,28 @@ func (c *CmdLine) regexpStr(input string) string {
150150
return result.String()
151151
}
152152

153-
// regexpChar ensures that some special characters are escaped
153+
// regexpChar ensures that some special characters are escaped.
154+
// Note that we do this so we don't have to treat the entries in command
155+
// lists as regular expressions, i.e., entries like `c++` don't need to be
156+
// escaped.
154157
func (c *CmdLine) regexpChar(char byte) string {
155158
logger.Trace().Msgf("regexpChar in: %v", char)
156159

157160
chars := ""
158161
switch char {
159162
case '.':
160-
chars = "\\."
163+
fallthrough
161164
case '-':
162-
chars = "\\-"
165+
fallthrough
166+
case '+':
167+
chars = "\\" + string(char)
168+
case ' ':
169+
chars = "\\s+"
163170
default:
164171
chars = string(char)
165172
}
166173
logger.Trace().Msgf("regexpChar out: %s", chars)
167-
return strings.ReplaceAll(chars, " ", "\\s+")
174+
return chars
168175
}
169176

170177
// Computes the evasion suffix based on the presence of `@` or `~` at

regex/processors/cmdline_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,40 @@ func (s *cmdLineTestSuite) TestCmdLine_ProcessLineFoo() {
9898
s.Equal(`f_av-u_o_av-u_o`, cmd.proc.lines[0])
9999
}
100100

101-
func (s *cmdLineTestSuite) TestCmdLine_ProcessLinePattern() {
101+
func (s *cmdLineTestSuite) TestCmdLine_ProcessLineWithDash() {
102102
cmd := NewCmdLine(s.ctx, CmdLineUnix)
103103

104-
err := cmd.ProcessLine(`gcc-10.`)
104+
err := cmd.ProcessLine(`gcc-10`)
105105
s.Require().NoError(err)
106106

107-
s.Equal(`g_av-u_c_av-u_c_av-u_\-_av-u_1_av-u_0_av-u_\.`, cmd.proc.lines[0])
107+
s.Equal(`g_av-u_c_av-u_c_av-u_\-_av-u_1_av-u_0`, cmd.proc.lines[0])
108+
}
109+
110+
func (s *cmdLineTestSuite) TestCmdLine_ProcessLineWithDot() {
111+
cmd := NewCmdLine(s.ctx, CmdLineUnix)
112+
113+
err := cmd.ProcessLine(`gcc10.`)
114+
s.Require().NoError(err)
115+
116+
s.Equal(`g_av-u_c_av-u_c_av-u_1_av-u_0_av-u_\.`, cmd.proc.lines[0])
117+
}
118+
119+
func (s *cmdLineTestSuite) TestCmdLine_ProcessLineWithPlus() {
120+
cmd := NewCmdLine(s.ctx, CmdLineUnix)
121+
122+
err := cmd.ProcessLine(`gcc10+`)
123+
s.Require().NoError(err)
124+
125+
s.Equal(`g_av-u_c_av-u_c_av-u_1_av-u_0_av-u_\+`, cmd.proc.lines[0])
126+
}
127+
128+
func (s *cmdLineTestSuite) TestCmdLine_ProcessLineWithSpace() {
129+
cmd := NewCmdLine(s.ctx, CmdLineUnix)
130+
131+
err := cmd.ProcessLine(`gcc 10`)
132+
s.Require().NoError(err)
133+
134+
s.Equal(`g_av-u_c_av-u_c_av-u_\s+_av-u_1_av-u_0`, cmd.proc.lines[0])
108135
}
109136

110137
func (s *cmdLineTestSuite) TestCmdLine_ProcessLineFooWindows() {

0 commit comments

Comments
 (0)