chore: fix potential nil pointer errors (#3454)

master
Kevin Wan 1 year ago committed by GitHub
parent ad4cce959d
commit c3f57e9b0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -32,6 +32,10 @@ func (bp *BufferPool) Get() *bytes.Buffer {
// Put returns buf into bp. // Put returns buf into bp.
func (bp *BufferPool) Put(buf *bytes.Buffer) { func (bp *BufferPool) Put(buf *bytes.Buffer) {
if buf == nil {
return
}
if buf.Cap() < bp.capability { if buf.Cap() < bp.capability {
bp.pool.Put(buf) bp.pool.Put(buf)
} }

@ -13,3 +13,26 @@ func TestBufferPool(t *testing.T) {
pool.Put(bytes.NewBuffer(make([]byte, 0, 2*capacity))) pool.Put(bytes.NewBuffer(make([]byte, 0, 2*capacity)))
assert.True(t, pool.Get().Cap() <= capacity) assert.True(t, pool.Get().Cap() <= capacity)
} }
func TestBufferPool_Put(t *testing.T) {
t.Run("with nil buf", func(t *testing.T) {
pool := NewBufferPool(1024)
pool.Put(nil)
val := pool.Get()
assert.IsType(t, new(bytes.Buffer), val)
})
t.Run("with less-cap buf", func(t *testing.T) {
pool := NewBufferPool(1024)
pool.Put(bytes.NewBuffer(make([]byte, 0, 512)))
val := pool.Get()
assert.IsType(t, new(bytes.Buffer), val)
})
t.Run("with more-cap buf", func(t *testing.T) {
pool := NewBufferPool(1024)
pool.Put(bytes.NewBuffer(make([]byte, 0, 1024<<1)))
val := pool.Get()
assert.IsType(t, new(bytes.Buffer), val)
})
}

@ -878,7 +878,7 @@ func (u *Unmarshaler) processNamedFieldWithoutValue(fieldType reflect.Type, valu
func (u *Unmarshaler) unmarshalWithFullName(m valuerWithParent, v any, fullName string) error { func (u *Unmarshaler) unmarshalWithFullName(m valuerWithParent, v any, fullName string) error {
rv := reflect.ValueOf(v) rv := reflect.ValueOf(v)
if err := ValidatePtr(&rv); err != nil { if err := ValidatePtr(rv); err != nil {
return err return err
} }

@ -79,7 +79,7 @@ func SetMapIndexValue(tp reflect.Type, value, key, target reflect.Value) {
} }
// ValidatePtr validates v if it's a valid pointer. // ValidatePtr validates v if it's a valid pointer.
func ValidatePtr(v *reflect.Value) error { func ValidatePtr(v reflect.Value) error {
// sequence is very important, IsNil must be called after checking Kind() with reflect.Ptr, // sequence is very important, IsNil must be called after checking Kind() with reflect.Ptr,
// panic otherwise // panic otherwise
if !v.IsValid() || v.Kind() != reflect.Ptr || v.IsNil() { if !v.IsValid() || v.Kind() != reflect.Ptr || v.IsNil() {

@ -218,25 +218,25 @@ func TestParseSegments(t *testing.T) {
func TestValidatePtrWithNonPtr(t *testing.T) { func TestValidatePtrWithNonPtr(t *testing.T) {
var foo string var foo string
rve := reflect.ValueOf(foo) rve := reflect.ValueOf(foo)
assert.NotNil(t, ValidatePtr(&rve)) assert.NotNil(t, ValidatePtr(rve))
} }
func TestValidatePtrWithPtr(t *testing.T) { func TestValidatePtrWithPtr(t *testing.T) {
var foo string var foo string
rve := reflect.ValueOf(&foo) rve := reflect.ValueOf(&foo)
assert.Nil(t, ValidatePtr(&rve)) assert.Nil(t, ValidatePtr(rve))
} }
func TestValidatePtrWithNilPtr(t *testing.T) { func TestValidatePtrWithNilPtr(t *testing.T) {
var foo *string var foo *string
rve := reflect.ValueOf(foo) rve := reflect.ValueOf(foo)
assert.NotNil(t, ValidatePtr(&rve)) assert.NotNil(t, ValidatePtr(rve))
} }
func TestValidatePtrWithZeroValue(t *testing.T) { func TestValidatePtrWithZeroValue(t *testing.T) {
var s string var s string
e := reflect.Zero(reflect.TypeOf(s)) e := reflect.Zero(reflect.TypeOf(s))
assert.NotNil(t, ValidatePtr(&e)) assert.NotNil(t, ValidatePtr(e))
} }
func TestSetValueNotSettable(t *testing.T) { func TestSetValueNotSettable(t *testing.T) {

@ -146,7 +146,7 @@ func unmarshalRow(v any, scanner rowsScanner, strict bool) error {
} }
rv := reflect.ValueOf(v) rv := reflect.ValueOf(v)
if err := mapping.ValidatePtr(&rv); err != nil { if err := mapping.ValidatePtr(rv); err != nil {
return err return err
} }
@ -182,7 +182,7 @@ func unmarshalRow(v any, scanner rowsScanner, strict bool) error {
func unmarshalRows(v any, scanner rowsScanner, strict bool) error { func unmarshalRows(v any, scanner rowsScanner, strict bool) error {
rv := reflect.ValueOf(v) rv := reflect.ValueOf(v)
if err := mapping.ValidatePtr(&rv); err != nil { if err := mapping.ValidatePtr(rv); err != nil {
return err return err
} }

@ -26,6 +26,7 @@ const (
kindOtlpGrpc = "otlpgrpc" kindOtlpGrpc = "otlpgrpc"
kindOtlpHttp = "otlphttp" kindOtlpHttp = "otlphttp"
kindFile = "file" kindFile = "file"
protocolUdp = "udp"
) )
var ( var (
@ -65,9 +66,10 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) {
// Just support jaeger and zipkin now, more for later // Just support jaeger and zipkin now, more for later
switch c.Batcher { switch c.Batcher {
case kindJaeger: case kindJaeger:
u, _ := url.Parse(c.Endpoint) u, err := url.Parse(c.Endpoint)
if u.Scheme == "udp" { if err == nil && u.Scheme == protocolUdp {
return jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(u.Hostname()), jaeger.WithAgentPort(u.Port()))) return jaeger.New(jaeger.WithAgentEndpoint(jaeger.WithAgentHost(u.Hostname()),
jaeger.WithAgentPort(u.Port())))
} }
return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint))) return jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(c.Endpoint)))
case kindZipkin: case kindZipkin:

Loading…
Cancel
Save