Skip to content

Commit

Permalink
Safely convert strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
notbdu committed Feb 25, 2019
1 parent 739dd82 commit 77b61c4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions parser/json/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"unicode/utf8"

"github.com/xichen2020/eventdb/parser/json/value"
"github.com/xichen2020/eventdb/x/unsafe"
"github.com/xichen2020/eventdb/x/safe"
)

const (
Expand Down Expand Up @@ -130,7 +130,7 @@ func (p *parser) Parse(str string) (*value.Value, error) {
}

func (p *parser) ParseBytes(b []byte) (*value.Value, error) {
return p.Parse(unsafe.ToString(b))
return p.Parse(safe.ToString(b))
}

func (p *parser) reset() {
Expand Down Expand Up @@ -401,7 +401,7 @@ func (p *parser) parseStringAsRaw() (string, error) {
case '"':
p.pos += i + 1
escapedBytes = append(escapedBytes, data[prev:i]...)
return unsafe.ToString(escapedBytes), nil
return safe.ToString(escapedBytes), nil

case '\\':
escapedBytes = append(escapedBytes, data[prev:i]...)
Expand Down
3 changes: 2 additions & 1 deletion server/http/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/xichen2020/eventdb/parser/json/value"
"github.com/xichen2020/eventdb/query"
"github.com/xichen2020/eventdb/storage"
"github.com/xichen2020/eventdb/x/safe"
"github.com/xichen2020/eventdb/x/unsafe"

"github.com/m3db/m3x/clock"
Expand Down Expand Up @@ -318,7 +319,7 @@ func (s *service) writeBatch(data []byte) error {
if err != nil {
return fmt.Errorf("cannot parse document from %s: %v", docBytes, err)
}
nsStr := unsafe.ToString(ns)
nsStr := safe.ToString(ns)
docsByNamespace[nsStr] = append(docsByNamespace[nsStr], doc)
start = end + 1
batchSize++
Expand Down
4 changes: 2 additions & 2 deletions storage/mutable_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/xichen2020/eventdb/persist"
"github.com/xichen2020/eventdb/query"
"github.com/xichen2020/eventdb/x/hash"
"github.com/xichen2020/eventdb/x/unsafe"
"github.com/xichen2020/eventdb/x/safe"

"github.com/m3db/m3x/context"
)
Expand Down Expand Up @@ -568,7 +568,7 @@ func (s *mutableSeg) writeTimestampFieldWithLock(docID int32, val int64) {
func (s *mutableSeg) writeRawDocSourceFieldWithLock(docID int32, val []byte) {
v := field.ValueUnion{
Type: field.StringType,
StringVal: unsafe.ToString(val),
StringVal: safe.ToString(val),
}
s.rawDocSourceField.Add(docID, v)
}
Expand Down
8 changes: 8 additions & 0 deletions x/safe/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package safe

// ToString safely converts a byte slice to a string.
// TODO: This method should be removed when we move over to handling bytes in the
// database hot path instead of strings.
func ToString(b []byte) string {
return string(b)
}

0 comments on commit 77b61c4

Please sign in to comment.