Skip to content

Commit

Permalink
Replace UTF8 bounds check with utf8reader
Browse files Browse the repository at this point in the history
  • Loading branch information
yudai committed Aug 17, 2015
1 parent d0f2128 commit 87f8409
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 29 deletions.
4 changes: 4 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions Godeps/_workspace/src/github.com/yudai/utf8reader/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions Godeps/_workspace/src/github.com/yudai/utf8reader/utf8reader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 5 additions & 29 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"os/exec"
"strings"
"syscall"
"unicode/utf8"
"unsafe"

"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/websocket"
"github.com/kr/pty"
"github.com/yudai/utf8reader"
)

type App struct {
Expand Down Expand Up @@ -83,11 +83,10 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
defer func() { exit <- true }()

buf := make([]byte, 1024)
leftOver := 0
for {
size, err := fio.Read(buf[leftOver:])
size += leftOver
utf8f := utf8reader.New(fio)

for {
size, err := utf8f.Read(buf)
if err != nil {
log.Printf("command exited for: %s", r.RemoteAddr)
return
Expand All @@ -98,31 +97,8 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
return
}

// UTF-8 Boundary check
for leftOver = 0; leftOver < utf8.UTFMax; leftOver++ {
re, _ := utf8.DecodeLastRune(
buf[:size-leftOver],
)

if re != utf8.RuneError {
break
}
// Invalid UTF rune
}

if leftOver == utf8.UTFMax-1 {
re, _ := utf8.DecodeLastRune(buf[:size-leftOver])
if re == utf8.RuneError {
log.Fatal("UTF8 Boundary error.")
}
}

writer.Write(buf[:size-leftOver])
writer.Write(buf[:size])
writer.Close()

for i := 0; i < leftOver; i++ {
buf[i] = buf[size-leftOver+i]
}
}
}()

Expand Down

0 comments on commit 87f8409

Please sign in to comment.