Decoding a message-valued oneof arm after a scalar arm crashes

Đang mở
#47 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
backend

Hướng nghiên cứu

The crash happens in hyperpb's decoding of a message-valued oneof arm after a scalar arm, reproduced by the Go program in the issue body that builds an Envelope descriptor with a scalar and a nested Child arm and calls message.Unmarshal. Start by running that reproducer, then read the unmarshal path in the hyperpb package to see how oneof arms are tracked when a later field replaces an earlier one. Done means the reproducer returns nested with nested.value == 9 and the scalar arm is cleared, with a regression test added.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

Unmarshalling a payload with two different arms of the same oneof crashes when the first arm is scalar and the later arm is a message. Protobuf permits this wire input. The last arm should be active.

Versions / platform

  • buf.build/go/hyperpb v0.1.3
  • google.golang.org/protobuf v1.36.9
  • Go 1.27.4 on darwin/arm64

Reproducer

Save as main.go in an empty directory and run:

go mod init repro
go get buf.build/go/[email protected] google.golang.org/[email protected]
go run .
package main

import (
	"buf.build/go/hyperpb"
	"google.golang.org/protobuf/encoding/protowire"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protodesc"
	"google.golang.org/protobuf/types/descriptorpb"
)

func main() {
	file := &descriptorpb.FileDescriptorProto{
		Name:    proto.String("repro.proto"),
		Package: proto.String("repro"),
		Syntax:  proto.String("proto3"),
		MessageType: []*descriptorpb.DescriptorProto{
			{
				Name: proto.String("Child"),
				Field: []*descriptorpb.FieldDescriptorProto{{
					Name: proto.String("value"), Number: proto.Int32(1),
					Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
				}},
			},
			{
				Name:      proto.String("Envelope"),
				OneofDecl: []*descriptorpb.OneofDescriptorProto{{Name: proto.String("choice")}},
				Field: []*descriptorpb.FieldDescriptorProto{
					{Name: proto.String("scalar"), Number: proto.Int32(1), Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(), OneofIndex: proto.Int32(0)},
					{Name: proto.String("nested"), Number: proto.Int32(2), Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(), TypeName: proto.String(".repro.Child"), OneofIndex: proto.Int32(0)},
				},
			},
		},
	}
	descriptor, err := protodesc.NewFile(file, nil)
	check(err)
	envelope := descriptor.Messages().ByName("Envelope")

	// scalar = 1 followed by nested { value: 9 }
	var payload []byte
	payload = protowire.AppendTag(payload, 1, protowire.VarintType)
	payload = protowire.AppendVarint(payload, 1)
	payload = protowire.AppendTag(payload, 2, protowire.BytesType)
	payload = protowire.AppendBytes(payload, []byte{8, 9})

	shared := new(hyperpb.Shared)
	defer shared.Free()
	message := shared.NewMessage(hyperpb.CompileMessageDescriptor(envelope))
	check(message.Unmarshal(payload))
}

func check(err error) {
	if err != nil {
		panic(err)
	}
}

Observed

go run . panics with runtime error: invalid memory address or nil pointer dereference while decoding the message arm.

Expected

Decoding succeeds with nested as the active arm and nested.value == 9. That value comes from the later message arm. The earlier scalar arm is discarded.

Ngôn ngữ chính
Go
Star
691
Fork
19
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của bufbuild/hyperpb-go

Tất cả issue của bufbuild/hyperpb-go

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.