Skip to content

Commit

Permalink
Revert "It's not safe to unmarshal strings using unsafe (#25)" (#26)
Browse files Browse the repository at this point in the history
This reverts commit 795b894.
  • Loading branch information
outofforest authored Sep 4, 2024
1 parent 795b894 commit 4d25ac2
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions types/tstring/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ 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 @@ -87,9 +88,9 @@ func (b Builder) UnmarshalCodeTemplate(_ *uint64) string {
code += ` if l > 0 {
`
if b.fieldType.Name() == "string" {
code += " {{ . }} = string(b[o:o+l])\n"
code += fmt.Sprintf(" {{ . }} = %[1]s.String((*byte)(%[1]s.Pointer(&b[o])), l)\n", unsafe)
} else {
code += fmt.Sprintf(" {{ . }} = %[1]s(b[o:o+l])\n",
code += fmt.Sprintf(" {{ . }} = %[2]s(%[1]s.String((*byte)(%[1]s.Pointer(&b[o])), l))\n", unsafe,
b.tm.TypeName(b.msgType, b.fieldType))
}
code += ` o += l
Expand Down

0 comments on commit 4d25ac2

Please sign in to comment.