Skip to content

Commit

Permalink
It's not safe to unmarshal strings using unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest committed Sep 4, 2024
1 parent 7330d63 commit c5a098c
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions types/tstring/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (b Builder) MarshalCodeTemplate(_ *uint64) string {

// UnmarshalCodeTemplate returns code template unmarshaling the data.
func (b Builder) UnmarshalCodeTemplate(_ *uint64) string {
unsafe := b.tm.Import("unsafe")
code := `{
var l uint64
`
Expand All @@ -88,9 +87,9 @@ func (b Builder) UnmarshalCodeTemplate(_ *uint64) string {
code += ` if l > 0 {
`
if b.fieldType.Name() == "string" {
code += fmt.Sprintf(" {{ . }} = %[1]s.String((*byte)(%[1]s.Pointer(&b[o])), l)\n", unsafe)
code += " {{ . }} = string(b[o:o+l])\n"
} else {
code += fmt.Sprintf(" {{ . }} = %[2]s(%[1]s.String((*byte)(%[1]s.Pointer(&b[o])), l))\n", unsafe,
code += fmt.Sprintf(" {{ . }} = %[1]s(b[o:o+l])\n",
b.tm.TypeName(b.msgType, b.fieldType))
}
code += ` o += l
Expand Down

0 comments on commit c5a098c

Please sign in to comment.