Comments (2)
Interesting edge case, thank you for reporting this bug very clearly! For now I have a couple quick thoughts:
- when
intStringLenmapvalue
is0
, we could usenil
instead of&dAtA[iNdEx]
:unsafe.String(nil, 0)
should do the job AFAIK. - in this case, the value is not backed by the original data, which makes sense. I think it's OK to not perform this check for empty strings in the test cases because it doesn't make sense for them anyway. Maybe it would work to compare it against the
nil
pointer, though. Here is what I have in mind:
func assertStringIsOriginal(t *testing.T, s string, belongs bool, originalData []byte) {
+ start := uintptr(unsafe.Pointer(unsafe.StringData(s)))
+ // empty string has no underlying array, compare pointer to nil
+ if len(s) == 0 {
+ assert.Equal(t, uintptr(unsafe.Pointer(nil)), start)
+ return
+ }
+ end := start + uintptr(len(s)) - 1
+
originalStart := uintptr(unsafe.Pointer(unsafe.SliceData(originalData)))
originalEnd := originalStart + uintptr(len(originalData)) - 1
- 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)
}
What do you think about this?
from vtprotobuf.
@nockty Thanks, that fix looks good to me.
I also noticed this panic occurs with empty strings in oneof fields:
--- FAIL: Test_UnmarshalVTUnsafe (0.00s)
panic: runtime error: index out of range [2] with length 2 [recovered]
panic: runtime error: index out of range [2] with length 2
goroutine 18 [running]:
testing.tRunner.func1.2({0x139fea0, 0xc0000b0318})
/Users/maheesha/go/go1.20.1/src/testing/testing.go:1526 +0x24e
testing.tRunner.func1()
/Users/maheesha/go/go1.20.1/src/testing/testing.go:1529 +0x39f
panic({0x139fea0, 0xc0000b0318})
/Users/maheesha/go/go1.20.1/src/runtime/panic.go:884 +0x213
github.com/planetscale/vtprotobuf/testproto/unsafe.(*UnsafeTest_Sub4).UnmarshalVTUnsafe(0xc0000a2f00, {0xc0000a4842, 0x2, 0x2})
/Users/maheesha/go/src/vtprotobuf/testproto/unsafe/unsafe_vtproto.pb.go:3015 +0x835
github.com/planetscale/vtprotobuf/testproto/unsafe.(*UnsafeTest).UnmarshalVTUnsafe(0xc0000dbca8, {0xc0000a4840, 0x4, 0x4})
/Users/maheesha/go/src/vtprotobuf/testproto/unsafe/unsafe_vtproto.pb.go:3444 +0xa34
github.com/planetscale/vtprotobuf/testproto/unsafe.Test_UnmarshalVTUnsafe(0xc000082ea0?)
/Users/maheesha/go/src/vtprotobuf/testproto/unsafe/unsafe_test.go:153 +0x1811
testing.tRunner(0xc000082ea0, 0x13f15f8)
/Users/maheesha/go/go1.20.1/src/testing/testing.go:1576 +0x10b
created by testing.(*T).Run
/Users/maheesha/go/go1.20.1/src/testing/testing.go:1629 +0x3ea
I've used the same approach to fix this, will raise a PR now.
from vtprotobuf.
Related Issues (20)
- Uncompilable code generated for repeated groups
- Duplicated code generated for GRPC client/server HOT 1
- Panic after WKT commit: assignment to entry in nil map HOT 2
- Extensions are not encoded
- docs/feature: Nested ReturnToVTPool() HOT 4
- bug: oneof field that includes bytes field yields invalid ResetVT method
- Supporting Embedded and Nullable features from gogo proto HOT 2
- bug: Broken ResetVT generation for optional message HOT 1
- bug: grpc client methods does not use qualified idents
- feature: poolable messages paths with wildcard HOT 2
- unmarshal_unsafe feature produced code does not compile: "Cannot use intStringLen (type int) as the type IntegerType" HOT 1
- Run vtproto multiple times on different files of the same package HOT 3
- bug: IsMap not checked on message field's ReturnToVTpool
- Request for New Release that Includes Well-Known Type Support HOT 4
- gRPC codec has different semantic on Unmarshal than the default one HOT 1
- v0.6.0 is creating issues in ubuntu20 HOT 2
- Offering assistance and discussing upkeep / releases for vtprotobuf HOT 8
- wrong pool unmarshal slize HOT 3
- Add optional unsafe operations HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vtprotobuf.