Move string validation into own helper function.

This commit is contained in:
Martin Dosch 2022-11-05 12:40:40 +01:00
parent 679740d516
commit dd9566eaee
No known key found for this signature in database
GPG Key ID: 52A57CFCE13D657D
3 changed files with 14 additions and 13 deletions

View File

@ -10,8 +10,19 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"
)
func validUTF8(s string) string {
// Remove invalid code points.
s = strings.ToValidUTF8(s, "")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
s = reg.ReplaceAllString(s, "")
return s
}
func readFile(path string) (*bytes.Buffer, error) {
file, err := os.Open(path)
if err != nil {

11
main.go
View File

@ -12,7 +12,6 @@ import (
"log"
"net"
"os"
"regexp"
"strings"
"time"
@ -290,11 +289,7 @@ func main() {
}
}
// Remove invalid code points.
message = strings.ToValidUTF8(message, "")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
message = reg.ReplaceAllString(message, "")
message = validUTF8(message)
// Exit if message is empty.
if message == "" && !*flagInteractive && !*flagListen && *flagHTTPUpload == "" {
os.Exit(0)
@ -428,9 +423,7 @@ func main() {
}
// Remove invalid code points.
message = strings.ToValidUTF8(message, "")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
message = reg.ReplaceAllString(message, "")
message = validUTF8(message)
if message == "" {
continue
}

5
ox.go
View File

@ -9,7 +9,6 @@ import (
"errors"
"log"
"os"
"regexp"
"runtime"
"strings"
"time"
@ -99,9 +98,7 @@ func oxDecrypt(m xmpp.Chat, client *xmpp.Client, iqc chan xmpp.IQ,
return "error", time.Now(), err
}
// Remove invalid code points.
message := strings.ToValidUTF8(string(decryptMsg.Data), "")
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
message = reg.ReplaceAllString(message, "")
message := validUTF8(string(decryptMsg.Data))
doc := etree.NewDocument()
err = doc.ReadFromString(message)
if err != nil {