Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util/text/validate.go: Safe is not safe #1129

Open
3052 opened this issue Oct 24, 2023 · 0 comments
Open

util/text/validate.go: Safe is not safe #1129

3052 opened this issue Oct 24, 2023 · 0 comments

Comments

@3052
Copy link

3052 commented Oct 24, 2023

// Safe will tell if a character in the string is considered unsafe
// Currently trigger on unicode control character except \n, \t and \r
func Safe(s string) bool {
for _, r := range s {
switch r {
case '\t', '\r', '\n':
continue
}
if unicode.IsControl(r) {
return false
}
}
return true
}

doesn't check for invalid UTF-8, so this string would be returned as safe:

"\xA0\xA1"

also doesn't catch all control characters, so this would also be returned as safe:

"\u200E"

improved code:

package unicode

import (
   "unicode"
   "unicode/utf8"
)

func binary(src []byte) bool {
   for len(src) >= 1 {
      r, size := utf8.DecodeRune(src)
      if r == utf8.RuneError {
         if size == 1 {
            return true
         }
      }
      if unicode.Is(unicode.C, r) {
         return true
      }
      src = src[size:]
   }
   return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant