Skip to content

Commit

Permalink
💄 linting cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Dec 12, 2024
1 parent 4f5ad7d commit 75a3b27
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 129 deletions.
128 changes: 1 addition & 127 deletions pkg/cpe/cpeuri/cpeuri.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,133 +196,7 @@ func unbindValueFS(str string) (string, error) {
return "-", nil

}
return AddQuoting(str)
}

func AddQuoting(str string) (string, error) {
result := ""
idx := 0
embedded := false

for idx < len(str) {
c := str[idx]

// alphanum or _
if (c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
c == '_' {
result = result + string(c)
idx++
embedded = true
continue
}

// handle escaping
if c == '\\' {
// the 2.3 specification does not do this check, but should
if len(str)-idx > 1 {
result = fmt.Sprintf("%s%c%c", result, c, str[idx+1])
idx += 2
embedded = true
continue
} else {
return "", fmt.Errorf("escaping character length failure")
}
}

// wildcard must be at start or end
if c == '*' {
if idx == 0 || idx == len(str)-1 {
result = fmt.Sprintf("%s%c", result, c)
idx++
embedded = true
continue
} else {
return "",
fmt.Errorf("unquoted asterisk not at start or end of string")
}
}

// handle ? modifier
if c == '?' {
if ((idx == 0) || (idx == len(str)-1)) ||
(!embedded && str[idx-1] == '?') ||
(embedded && str[idx+1] == '?') {
result = fmt.Sprintf("%s%c", result, c)
idx++
embedded = false
continue
} else {
return "",
fmt.Errorf("unquoted ? must be at start or end of string")
}
}

// all others must be escaped
result = fmt.Sprintf("%s\\%c", result, c)
idx++
embedded = true
}
return result, nil
}

func bindValueFS(v string) (string, error) {
if v == "*" || v == "-" {
return v, nil
}
if v == "" {
return "*", nil
}
return processQuotedChars(v)
}

func processQuotedChars(s string) (string, error) {
result := ""
idx := 0

for idx < len(s) {
c := s[idx]

if c != '\\' {
result = fmt.Sprintf("%s%c", result, c)

} else {
if len(s)-idx <= 1 {
return "", fmt.Errorf("invalid escaping")
}

nextchr := s[idx+1]
switch nextchr {
case '.',
'-',
'_':
result = fmt.Sprintf("%s%c", result, nextchr)
idx += 2

default:
result = fmt.Sprintf("%s\\%c", result, nextchr)
idx += 2
}
continue
}
idx++
}
return result, nil
}

func getAndUnbindComponent(str string) (string, error) {
// Unescape colons
str = strings.ReplaceAll(str, "\\:", ":")

switch str {
case "", "*":
return "*", nil
case "-":
return "-", nil
default:
return addQuoting(str)
}
return addQuoting(str)
}

func addQuoting(str string) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ func IndexCPE(indexName string, cpe cpeutils.CPE, query string) ([]cpeutils.CPEV
func matchesCPE(line string, cpe cpeutils.CPE) bool {

if cpe.Vendor != "" {
if !strings.Contains(line, fmt.Sprintf(`%s`, strings.ToLower(cpe.Vendor))) {
if !strings.Contains(line, strings.ToLower(cpe.Vendor)) {
return false
}
}

if cpe.Product != "" {
if !strings.Contains(line, fmt.Sprintf(`%s`, strings.ToLower(cpe.Product))) {
if !strings.Contains(line, strings.ToLower(cpe.Product)) {
return false
}
}
Expand Down

0 comments on commit 75a3b27

Please sign in to comment.