Fixed a crash bug in the post preparser.

Moved tryStepForward out of PreparseMessage()
Added another test for PreparseMessage()
This commit is contained in:
Azareal 2018-06-30 14:34:07 +10:00
parent bf40d61474
commit c640a73afd
2 changed files with 20 additions and 9 deletions

View File

@ -171,6 +171,14 @@ type TagToAction struct {
PartialMode bool PartialMode bool
} }
func tryStepForward(i int, step int, runes []rune) (int, bool) {
i += step
if i < len(runes) {
return i, true
}
return i - step, false
}
// TODO: Preparse Markdown and normalize it into HTML? // TODO: Preparse Markdown and normalize it into HTML?
func PreparseMessage(msg string) string { func PreparseMessage(msg string) string {
//fmt.Println("initial msg: ", msg) //fmt.Println("initial msg: ", msg)
@ -196,13 +204,6 @@ func PreparseMessage(msg string) string {
//fmt.Println("before []byte(msg): ", []byte(msg)) //fmt.Println("before []byte(msg): ", []byte(msg))
var runes = []rune(msg) var runes = []rune(msg)
msg = "" msg = ""
var stepForward = func(i int, step int, runes []rune) int {
i += step
if i < len(runes) {
return i
}
return i - step
}
// TODO: We can maybe reduce the size of this by using an offset? // TODO: We can maybe reduce the size of this by using an offset?
// TODO: Move some of these closures out of this function to make things a little more efficient // TODO: Move some of these closures out of this function to make things a little more efficient
@ -262,7 +263,12 @@ func PreparseMessage(msg string) string {
char := runes[i] char := runes[i]
if char == '&' && peekMatch(i, "lt;", runes) { if char == '&' && peekMatch(i, "lt;", runes) {
//fmt.Println("found less than") //fmt.Println("found less than")
i = stepForward(i, 4, runes) var ok bool
i, ok = tryStepForward(i, 4, runes)
if !ok {
msg += "&lt;"
break
}
char := runes[i] char := runes[i]
//fmt.Println("char: ", char) //fmt.Println("char: ", char)
//fmt.Println("string(char): ", string(char)) //fmt.Println("string(char): ", string(char))
@ -276,7 +282,11 @@ func PreparseMessage(msg string) string {
var closeTag bool var closeTag bool
if char == '/' { if char == '/' {
//fmt.Println("found close tag") //fmt.Println("found close tag")
i = stepForward(i, 1, runes) i, ok = tryStepForward(i, 1, runes)
if !ok {
msg += "&lt;/"
break
}
char = runes[i] char = runes[i]
closeTag = true closeTag = true
} }

View File

@ -993,6 +993,7 @@ func TestPreparser(t *testing.T) {
msgList = addMETri(msgList, ">", "&gt;") msgList = addMETri(msgList, ">", "&gt;")
msgList = addMETri(msgList, "<meow>", "&lt;meow&gt;") msgList = addMETri(msgList, "<meow>", "&lt;meow&gt;")
msgList = addMETri(msgList, "&lt;", "&amp;lt;") msgList = addMETri(msgList, "&lt;", "&amp;lt;")
msgList = addMETri(msgList, "&", "&amp;")
// Note: strings.TrimSpace strips newlines, if there's nothing before or after them // Note: strings.TrimSpace strips newlines, if there's nothing before or after them
msgList = addMETri(msgList, "<br>", "") msgList = addMETri(msgList, "<br>", "")