From 6a7e8af003d6096480a9ec07b15f729426f8de8c Mon Sep 17 00:00:00 2001 From: Corentin Dupret Date: Wed, 4 Oct 2023 18:14:46 +0200 Subject: [PATCH] Replace `unsafeBytesToString` by `unsafe.String` --- .../conformance/conformance_vtproto.pb.go | 25 ++++++------ .../test_messages_proto2_vtproto.pb.go | 40 +++++++++---------- .../test_messages_proto3_vtproto.pb.go | 29 +++++++------- features/unmarshal/unmarshal.go | 13 +----- testproto/pool/pool_vtproto.pb.go | 6 +-- testproto/pool/pool_with_oneof_vtproto.pb.go | 3 +- .../pool/pool_with_slice_reuse_vtproto.pb.go | 7 ++-- testproto/proto2/scalars_vtproto.pb.go | 10 ++--- testproto/proto3opt/opt_vtproto.pb.go | 6 +-- testproto/unsafe/unsafe_test.go | 15 +++---- testproto/unsafe/unsafe_vtproto.pb.go | 12 ++---- testproto/wkt/wkt_vtproto.pb.go | 5 --- types/known/anypb/any_vtproto.pb.go | 6 +-- types/known/durationpb/duration_vtproto.pb.go | 5 --- types/known/emptypb/empty_vtproto.pb.go | 5 --- .../fieldmaskpb/field_mask_vtproto.pb.go | 6 +-- .../known/timestamppb/timestamp_vtproto.pb.go | 5 --- types/known/wrapperspb/wrappers_vtproto.pb.go | 6 +-- 18 files changed, 72 insertions(+), 132 deletions(-) diff --git a/conformance/internal/conformance/conformance_vtproto.pb.go b/conformance/internal/conformance/conformance_vtproto.pb.go index 35244e1..837109b 100644 --- a/conformance/internal/conformance/conformance_vtproto.pb.go +++ b/conformance/internal/conformance/conformance_vtproto.pb.go @@ -9,6 +9,7 @@ import ( proto "google.golang.org/protobuf/proto" protoimpl "google.golang.org/protobuf/runtime/protoimpl" io "io" + unsafe "unsafe" ) const ( @@ -2371,7 +2372,7 @@ func (m *FailureSet) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Failure = append(m.Failure, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.Failure = append(m.Failure, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex default: iNdEx = preIndex @@ -2486,7 +2487,7 @@ func (m *ConformanceRequest) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Payload = &ConformanceRequest_JsonPayload{JsonPayload: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Payload = &ConformanceRequest_JsonPayload{JsonPayload: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 3: if wireType != 0 { @@ -2537,7 +2538,7 @@ func (m *ConformanceRequest) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.MessageType = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.MessageType = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex case 5: if wireType != 0 { @@ -2624,7 +2625,7 @@ func (m *ConformanceRequest) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Payload = &ConformanceRequest_JspbPayload{JspbPayload: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Payload = &ConformanceRequest_JspbPayload{JspbPayload: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 8: if wireType != 2 { @@ -2656,7 +2657,7 @@ func (m *ConformanceRequest) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Payload = &ConformanceRequest_TextPayload{TextPayload: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Payload = &ConformanceRequest_TextPayload{TextPayload: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 9: if wireType != 0 { @@ -2759,7 +2760,7 @@ func (m *ConformanceResponse) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Result = &ConformanceResponse_ParseError{ParseError: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Result = &ConformanceResponse_ParseError{ParseError: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 2: if wireType != 2 { @@ -2791,7 +2792,7 @@ func (m *ConformanceResponse) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Result = &ConformanceResponse_RuntimeError{RuntimeError: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Result = &ConformanceResponse_RuntimeError{RuntimeError: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 3: if wireType != 2 { @@ -2855,7 +2856,7 @@ func (m *ConformanceResponse) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Result = &ConformanceResponse_JsonPayload{JsonPayload: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Result = &ConformanceResponse_JsonPayload{JsonPayload: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 5: if wireType != 2 { @@ -2887,7 +2888,7 @@ func (m *ConformanceResponse) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Result = &ConformanceResponse_Skipped{Skipped: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Result = &ConformanceResponse_Skipped{Skipped: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 6: if wireType != 2 { @@ -2919,7 +2920,7 @@ func (m *ConformanceResponse) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Result = &ConformanceResponse_SerializeError{SerializeError: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Result = &ConformanceResponse_SerializeError{SerializeError: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 7: if wireType != 2 { @@ -2951,7 +2952,7 @@ func (m *ConformanceResponse) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Result = &ConformanceResponse_JspbPayload{JspbPayload: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Result = &ConformanceResponse_JspbPayload{JspbPayload: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 8: if wireType != 2 { @@ -2983,7 +2984,7 @@ func (m *ConformanceResponse) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Result = &ConformanceResponse_TextPayload{TextPayload: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Result = &ConformanceResponse_TextPayload{TextPayload: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex default: iNdEx = preIndex diff --git a/conformance/internal/conformance/test_messages_proto2_vtproto.pb.go b/conformance/internal/conformance/test_messages_proto2_vtproto.pb.go index e8e95d5..0cd390d 100644 --- a/conformance/internal/conformance/test_messages_proto2_vtproto.pb.go +++ b/conformance/internal/conformance/test_messages_proto2_vtproto.pb.go @@ -15093,7 +15093,7 @@ func (m *TestAllTypesProto2_MessageSetCorrectExtension1) UnmarshalVTUnsafe(dAtA if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.Str = &s iNdEx = postIndex default: @@ -15460,7 +15460,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.OptionalString = &s iNdEx = postIndex case 15: @@ -15636,7 +15636,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.OptionalStringPiece = &s iNdEx = postIndex case 25: @@ -15669,7 +15669,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.OptionalCord = &s iNdEx = postIndex case 27: @@ -16584,7 +16584,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RepeatedString = append(m.RepeatedString, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.RepeatedString = append(m.RepeatedString, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex case 45: if wireType != 2 { @@ -16853,7 +16853,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RepeatedStringPiece = append(m.RepeatedStringPiece, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.RepeatedStringPiece = append(m.RepeatedStringPiece, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex case 55: if wireType != 2 { @@ -16885,7 +16885,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RepeatedCord = append(m.RepeatedCord, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.RepeatedCord = append(m.RepeatedCord, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex case 56: if wireType != 2 { @@ -18183,7 +18183,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var stringLenmapvalue uint64 @@ -18212,7 +18212,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapvalue > l { return io.ErrUnexpectedEOF } - mapvalue = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapvalue]) + mapvalue = unsafe.String(&dAtA[iNdEx], intStringLenmapvalue) iNdEx = postStringIndexmapvalue } else { iNdEx = entryPreIndex @@ -18310,7 +18310,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var mapbyteLen uint64 @@ -18437,7 +18437,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var mapmsglen int @@ -18566,7 +18566,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var mapmsglen int @@ -18695,7 +18695,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { for shift := uint(0); ; shift += 7 { @@ -18808,7 +18808,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { for shift := uint(0); ; shift += 7 { @@ -20763,7 +20763,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.OneofField = &TestAllTypesProto2_OneofString{OneofString: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.OneofField = &TestAllTypesProto2_OneofString{OneofString: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 114: if wireType != 2 { @@ -21160,7 +21160,7 @@ func (m *TestAllTypesProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.DefaultString = &s iNdEx = postIndex case 255: @@ -21805,7 +21805,7 @@ func (m *UnknownToTestAllTypes) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.OptionalString = &s iNdEx = postIndex case 1003: @@ -22162,7 +22162,7 @@ func (m *OneStringProto2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.Data = &s iNdEx = postIndex default: @@ -22187,7 +22187,3 @@ func (m *OneStringProto2) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/conformance/internal/conformance/test_messages_proto3_vtproto.pb.go b/conformance/internal/conformance/test_messages_proto3_vtproto.pb.go index 97c49e0..112590c 100644 --- a/conformance/internal/conformance/test_messages_proto3_vtproto.pb.go +++ b/conformance/internal/conformance/test_messages_proto3_vtproto.pb.go @@ -22,6 +22,7 @@ import ( wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" io "io" math "math" + unsafe "unsafe" ) const ( @@ -15458,7 +15459,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.OptionalString = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.OptionalString = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex case 15: if wireType != 2 { @@ -15650,7 +15651,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.OptionalStringPiece = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.OptionalStringPiece = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex case 25: if wireType != 2 { @@ -15682,7 +15683,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.OptionalCord = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.OptionalCord = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex case 27: if wireType != 2 { @@ -16596,7 +16597,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RepeatedString = append(m.RepeatedString, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.RepeatedString = append(m.RepeatedString, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex case 45: if wireType != 2 { @@ -16865,7 +16866,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RepeatedStringPiece = append(m.RepeatedStringPiece, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.RepeatedStringPiece = append(m.RepeatedStringPiece, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex case 55: if wireType != 2 { @@ -16897,7 +16898,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RepeatedCord = append(m.RepeatedCord, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.RepeatedCord = append(m.RepeatedCord, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex case 56: if wireType != 2 { @@ -18195,7 +18196,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var stringLenmapvalue uint64 @@ -18224,7 +18225,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapvalue > l { return io.ErrUnexpectedEOF } - mapvalue = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapvalue]) + mapvalue = unsafe.String(&dAtA[iNdEx], intStringLenmapvalue) iNdEx = postStringIndexmapvalue } else { iNdEx = entryPreIndex @@ -18322,7 +18323,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var mapbyteLen uint64 @@ -18449,7 +18450,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var mapmsglen int @@ -18578,7 +18579,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var mapmsglen int @@ -18707,7 +18708,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { for shift := uint(0); ; shift += 7 { @@ -18820,7 +18821,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { for shift := uint(0); ; shift += 7 { @@ -20775,7 +20776,7 @@ func (m *TestAllTypesProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.OneofField = &TestAllTypesProto3_OneofString{OneofString: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.OneofField = &TestAllTypesProto3_OneofString{OneofString: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 114: if wireType != 2 { diff --git a/features/unmarshal/unmarshal.go b/features/unmarshal/unmarshal.go index ed09147..77d65dc 100644 --- a/features/unmarshal/unmarshal.go +++ b/features/unmarshal/unmarshal.go @@ -135,15 +135,6 @@ func (p *unmarshal) GenerateHelpers() { ErrUnexpectedEndOfGroup = `, p.Ident("fmt", "Errorf"), `("proto: unexpected end of group") )`) }) - - if p.unsafe { - p.Helper("unsafeBytesToString", func(p *generator.GeneratedFile) { - p.P(` - func unsafeBytesToString(b []byte) string { - return *(*string)(`, p.Ident("unsafe", `Pointer`), `(&b)) - }`) - }) - } } func (p *unmarshal) methodUnmarshal() string { @@ -296,7 +287,7 @@ func (p *unmarshal) mapField(varName string, field *protogen.Field) { p.P(`return `, p.Ident("io", `ErrUnexpectedEOF`)) p.P(`}`) if p.unsafe { - p.P(varName, ` = `, "unsafeBytesToString", `(dAtA[iNdEx:postStringIndex`, varName, `])`) + p.P(varName, ` = `, p.Ident("unsafe", `String`), `(&dAtA[iNdEx], intStringLen`, varName, `)`) } else { p.P(varName, ` = `, "string", `(dAtA[iNdEx:postStringIndex`, varName, `])`) } @@ -522,7 +513,7 @@ func (p *unmarshal) fieldItem(field *protogen.Field, fieldname string, message * p.P(`}`) str := "string(dAtA[iNdEx:postIndex])" if p.unsafe { - str = `unsafeBytesToString(dAtA[iNdEx:postIndex])` + str = p.Ident("unsafe", `String`) + `(&dAtA[iNdEx], intStringLen)` } if oneof { p.P(`m.`, fieldname, ` = &`, field.GoIdent, `{`, field.GoName, ": ", str, `}`) diff --git a/testproto/pool/pool_vtproto.pb.go b/testproto/pool/pool_vtproto.pb.go index 7e327f8..317363c 100644 --- a/testproto/pool/pool_vtproto.pb.go +++ b/testproto/pool/pool_vtproto.pb.go @@ -452,7 +452,7 @@ func (m *MemoryPoolExtension) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Foo1 = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.Foo1 = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex case 2: if wireType != 0 { @@ -495,7 +495,3 @@ func (m *MemoryPoolExtension) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/testproto/pool/pool_with_oneof_vtproto.pb.go b/testproto/pool/pool_with_oneof_vtproto.pb.go index 5b6d9ec..dc5a1db 100644 --- a/testproto/pool/pool_with_oneof_vtproto.pb.go +++ b/testproto/pool/pool_with_oneof_vtproto.pb.go @@ -10,6 +10,7 @@ import ( protoimpl "google.golang.org/protobuf/runtime/protoimpl" io "io" sync "sync" + unsafe "unsafe" ) const ( @@ -1687,7 +1688,7 @@ func (m *OneofTest_Test2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.B = append(m.B, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.B = append(m.B, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex default: iNdEx = preIndex diff --git a/testproto/pool/pool_with_slice_reuse_vtproto.pb.go b/testproto/pool/pool_with_slice_reuse_vtproto.pb.go index 18bd75d..441933b 100644 --- a/testproto/pool/pool_with_slice_reuse_vtproto.pb.go +++ b/testproto/pool/pool_with_slice_reuse_vtproto.pb.go @@ -10,6 +10,7 @@ import ( protoimpl "google.golang.org/protobuf/runtime/protoimpl" io "io" sync "sync" + unsafe "unsafe" ) const ( @@ -1383,7 +1384,7 @@ func (m *Test1) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Sl = append(m.Sl, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.Sl = append(m.Sl, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex default: iNdEx = preIndex @@ -1677,7 +1678,7 @@ func (m *Slice2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.C = append(m.C, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.C = append(m.C, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex case 4: if wireType != 2 { @@ -1745,7 +1746,7 @@ func (m *Slice2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.E = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.E = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex case 6: if wireType != 0 { diff --git a/testproto/proto2/scalars_vtproto.pb.go b/testproto/proto2/scalars_vtproto.pb.go index 019ee86..7d390c5 100644 --- a/testproto/proto2/scalars_vtproto.pb.go +++ b/testproto/proto2/scalars_vtproto.pb.go @@ -10167,7 +10167,7 @@ func (m *StringMessage) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.RequiredField = &s iNdEx = postIndex hasFields[0] |= uint64(0x00000001) @@ -10201,7 +10201,7 @@ func (m *StringMessage) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.OptionalField = &s iNdEx = postIndex case 3: @@ -10234,7 +10234,7 @@ func (m *StringMessage) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.RepeatedField = append(m.RepeatedField, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.RepeatedField = append(m.RepeatedField, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex default: iNdEx = preIndex @@ -10644,7 +10644,3 @@ func (m *EnumMessage) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/testproto/proto3opt/opt_vtproto.pb.go b/testproto/proto3opt/opt_vtproto.pb.go index 0b4c2bd..7ccb0d7 100644 --- a/testproto/proto3opt/opt_vtproto.pb.go +++ b/testproto/proto3opt/opt_vtproto.pb.go @@ -1211,7 +1211,7 @@ func (m *OptionalFieldInProto3) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - s := unsafeBytesToString(dAtA[iNdEx:postIndex]) + s := unsafe.String(&dAtA[iNdEx], intStringLen) m.OptionalString = &s iNdEx = postIndex case 15: @@ -1287,7 +1287,3 @@ func (m *OptionalFieldInProto3) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/testproto/unsafe/unsafe_test.go b/testproto/unsafe/unsafe_test.go index 3ae4fb4..179faed 100644 --- a/testproto/unsafe/unsafe_test.go +++ b/testproto/unsafe/unsafe_test.go @@ -1,7 +1,6 @@ package unsafe import ( - "reflect" "testing" "unsafe" @@ -11,15 +10,14 @@ import ( // assertStringIsOriginal asserts whether the underlying array of s belongs to originalData. // -// Note: performing checks on the Data uintptr of headers works as long as the garbage collector -// doesn't move memory. To provide guarantee that this test works, consider using +// Note: performing checks on the uintptr to the underlying arrays works as long as the garbage +// collector doesn't move memory. To provide guarantee that this test works, consider using // https://pkg.go.dev/runtime#Pinner when upgrading Go to >= 1.21. func assertStringIsOriginal(t *testing.T, s string, belongs bool, originalData []byte) { - originalStart := (*reflect.SliceHeader)(unsafe.Pointer(&originalData)).Data + originalStart := uintptr(unsafe.Pointer(unsafe.SliceData(originalData))) originalEnd := originalStart + uintptr(len(originalData)) - 1 - hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) - start := hdr.Data + start := uintptr(unsafe.Pointer(unsafe.StringData(s))) end := start + uintptr(len(s)) - 1 assert.Equal(t, belongs, start >= originalStart && start < originalEnd) assert.Equal(t, belongs, end > originalStart && end <= originalEnd) @@ -27,11 +25,10 @@ func assertStringIsOriginal(t *testing.T, s string, belongs bool, originalData [ // assertBytesAreOriginal is the same as assertStringIsOriginal for a []byte. func assertBytesAreOriginal(t *testing.T, b []byte, belongs bool, originalData []byte) { - originalStart := (*reflect.SliceHeader)(unsafe.Pointer(&originalData)).Data + originalStart := uintptr(unsafe.Pointer(unsafe.SliceData(originalData))) originalEnd := originalStart + uintptr(len(originalData)) - 1 - hdr := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - start := hdr.Data + start := uintptr(unsafe.Pointer(unsafe.SliceData(b))) end := start + uintptr(len(b)) - 1 assert.Equal(t, belongs, start >= originalStart && start < originalEnd) assert.Equal(t, belongs, end > originalStart && end <= originalEnd) diff --git a/testproto/unsafe/unsafe_vtproto.pb.go b/testproto/unsafe/unsafe_vtproto.pb.go index 20aefe3..9534758 100644 --- a/testproto/unsafe/unsafe_vtproto.pb.go +++ b/testproto/unsafe/unsafe_vtproto.pb.go @@ -2223,7 +2223,7 @@ func (m *UnsafeTest_Sub1) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.S = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.S = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex case 2: if wireType != 2 { @@ -2337,7 +2337,7 @@ func (m *UnsafeTest_Sub2) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.S = append(m.S, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.S = append(m.S, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex case 2: if wireType != 2 { @@ -2500,7 +2500,7 @@ func (m *UnsafeTest_Sub3) UnmarshalVTUnsafe(dAtA []byte) error { if postStringIndexmapkey > l { return io.ErrUnexpectedEOF } - mapkey = unsafeBytesToString(dAtA[iNdEx:postStringIndexmapkey]) + mapkey = unsafe.String(&dAtA[iNdEx], intStringLenmapkey) iNdEx = postStringIndexmapkey } else if fieldNum == 2 { var mapbyteLen uint64 @@ -2629,7 +2629,7 @@ func (m *UnsafeTest_Sub4) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Foo = &UnsafeTest_Sub4_S{S: unsafeBytesToString(dAtA[iNdEx:postIndex])} + m.Foo = &UnsafeTest_Sub4_S{S: unsafe.String(&dAtA[iNdEx], intStringLen)} iNdEx = postIndex case 2: if wireType != 2 { @@ -2900,7 +2900,3 @@ func (m *UnsafeTest) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/testproto/wkt/wkt_vtproto.pb.go b/testproto/wkt/wkt_vtproto.pb.go index e32cd95..7f83b5f 100644 --- a/testproto/wkt/wkt_vtproto.pb.go +++ b/testproto/wkt/wkt_vtproto.pb.go @@ -22,7 +22,6 @@ import ( wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" io "io" bits "math/bits" - unsafe "unsafe" ) const ( @@ -1745,7 +1744,3 @@ func (m *MessageWithWKT) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/types/known/anypb/any_vtproto.pb.go b/types/known/anypb/any_vtproto.pb.go index 31da324..aa49968 100644 --- a/types/known/anypb/any_vtproto.pb.go +++ b/types/known/anypb/any_vtproto.pb.go @@ -432,7 +432,7 @@ func (m *Any) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.TypeUrl = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.TypeUrl = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex case 2: if wireType != 2 { @@ -486,7 +486,3 @@ func (m *Any) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/types/known/durationpb/duration_vtproto.pb.go b/types/known/durationpb/duration_vtproto.pb.go index 3d0c8ce..349e70f 100644 --- a/types/known/durationpb/duration_vtproto.pb.go +++ b/types/known/durationpb/duration_vtproto.pb.go @@ -10,7 +10,6 @@ import ( durationpb "google.golang.org/protobuf/types/known/durationpb" io "io" bits "math/bits" - unsafe "unsafe" ) const ( @@ -419,7 +418,3 @@ func (m *Duration) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/types/known/emptypb/empty_vtproto.pb.go b/types/known/emptypb/empty_vtproto.pb.go index dcf9d9e..07f7ac7 100644 --- a/types/known/emptypb/empty_vtproto.pb.go +++ b/types/known/emptypb/empty_vtproto.pb.go @@ -10,7 +10,6 @@ import ( emptypb "google.golang.org/protobuf/types/known/emptypb" io "io" bits "math/bits" - unsafe "unsafe" ) const ( @@ -309,7 +308,3 @@ func (m *Empty) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/types/known/fieldmaskpb/field_mask_vtproto.pb.go b/types/known/fieldmaskpb/field_mask_vtproto.pb.go index 4fd37b1..759b021 100644 --- a/types/known/fieldmaskpb/field_mask_vtproto.pb.go +++ b/types/known/fieldmaskpb/field_mask_vtproto.pb.go @@ -388,7 +388,7 @@ func (m *FieldMask) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Paths = append(m.Paths, unsafeBytesToString(dAtA[iNdEx:postIndex])) + m.Paths = append(m.Paths, unsafe.String(&dAtA[iNdEx], intStringLen)) iNdEx = postIndex default: iNdEx = preIndex @@ -411,7 +411,3 @@ func (m *FieldMask) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/types/known/timestamppb/timestamp_vtproto.pb.go b/types/known/timestamppb/timestamp_vtproto.pb.go index 5e04ed8..231bc65 100644 --- a/types/known/timestamppb/timestamp_vtproto.pb.go +++ b/types/known/timestamppb/timestamp_vtproto.pb.go @@ -10,7 +10,6 @@ import ( timestamppb "google.golang.org/protobuf/types/known/timestamppb" io "io" bits "math/bits" - unsafe "unsafe" ) const ( @@ -419,7 +418,3 @@ func (m *Timestamp) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -} diff --git a/types/known/wrapperspb/wrappers_vtproto.pb.go b/types/known/wrapperspb/wrappers_vtproto.pb.go index e130e3f..8942b43 100644 --- a/types/known/wrapperspb/wrappers_vtproto.pb.go +++ b/types/known/wrapperspb/wrappers_vtproto.pb.go @@ -2233,7 +2233,7 @@ func (m *StringValue) UnmarshalVTUnsafe(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Value = unsafeBytesToString(dAtA[iNdEx:postIndex]) + m.Value = unsafe.String(&dAtA[iNdEx], intStringLen) iNdEx = postIndex default: iNdEx = preIndex @@ -2337,7 +2337,3 @@ func (m *BytesValue) UnmarshalVTUnsafe(dAtA []byte) error { } return nil } - -func unsafeBytesToString(b []byte) string { - return *(*string)(unsafe.Pointer(&b)) -}