Skip to content
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
16 changes: 15 additions & 1 deletion cmd/threadmark/process_args.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "strings"
import (
"regexp"
"strings"
)

func argvHasFlagValue(args []string, name, value string) bool {
for idx := 0; idx < len(args)-1; idx++ {
Expand All @@ -16,3 +19,14 @@ func argvHasFlagValue(args []string, name, value string) bool {
}
return false
}

func commandLineHasFlagValue(commandLine, name, value string) bool {
boundary := `(?:^|\s)`
end := `(?:\s|$)`
separate := regexp.MustCompile(boundary + regexp.QuoteMeta(name) + `\s+` + regexp.QuoteMeta(value) + end)
if separate.MatchString(commandLine) {
return true
}
equals := regexp.MustCompile(boundary + regexp.QuoteMeta(name+"="+value) + end)
return equals.MatchString(commandLine)
}
25 changes: 25 additions & 0 deletions cmd/threadmark/process_args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "testing"

func TestCommandLineHasFlagValueRequiresExactTokenBoundary(t *testing.T) {
commandLine := "threadmarkd -root /tmp/threadmark-other -socket=/tmp/threadmark/daemon.sock-extra"

if commandLineHasFlagValue(commandLine, "-root", "/tmp/threadmark") {
t.Fatal("matched root value prefix from longer flag value")
}
if commandLineHasFlagValue(commandLine, "-socket", "/tmp/threadmark/daemon.sock") {
t.Fatal("matched socket value prefix from longer flag value")
}
}

func TestCommandLineHasFlagValueMatchesSeparateAndEqualsForms(t *testing.T) {
commandLine := "threadmarkd -root /tmp/threadmark -socket=/tmp/threadmark/daemon.sock"

if !commandLineHasFlagValue(commandLine, "-root", "/tmp/threadmark") {
t.Fatal("did not match separate flag value")
}
if !commandLineHasFlagValue(commandLine, "-socket", "/tmp/threadmark/daemon.sock") {
t.Fatal("did not match equals flag value")
}
}
5 changes: 0 additions & 5 deletions cmd/threadmark/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,3 @@ func verifyDaemonProcess(pid int, root, socketPath string) error {
}
return nil
}

func commandLineHasFlagValue(commandLine, name, value string) bool {
return strings.Contains(commandLine, name+" "+value) ||
strings.Contains(commandLine, name+"="+value)
}