Giter Site home page Giter Site logo

protobuf's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kuustudio gcorpse

protobuf's Issues

当结构体字段的内容长度为0x7F时,字段size计算错误

复现代码

package main

import (
	"encoding/hex"
	"fmt"
	"github.com/RomiChan/protobuf/proto" // v0.1.1-0.20220524030518-4f349493f9da
)

type ST1 struct {
	ST *ST2 `protobuf:"bytes,1,opt"`
}

type ST2 struct {
	ST *ST3 `protobuf:"bytes,1,opt"`
}

type ST3 struct {
	Str []byte `protobuf:"bytes,1,opt"`
}

func main() {
	st := &ST1{&ST2{&ST3{
		Str: make([]byte, 0x7D),
	}}}
	b, _ := proto.Marshal(st)
	fmt.Println(hex.Dump(b))

	st.ST = nil
	err := proto.Unmarshal(b, st)
	if err != nil {
		fmt.Println(err)
	}
}

输出结果

00000000  0a 82 01 0a 7f 0a 7d 00  00 00 00 00 00 00 00 00  |......}.........|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00                                       |....|            

field number 1 with wire type 2: unexpected EOF

image

原因

在 \proto\walker.go 中,计算内容长度时误把tagsize算了进去

...
func (w *walker) structCodec(t reflect.Type) *codec {
	...
	c.size = func(p unsafe.Pointer, f *structField) int {
		p = deref(p)
		if p != nil {
			n := info.size(p) + f.tagsize	// ???
			n += sizeOfVarint(uint64(n))
			return n
		}
		return 0
	}
	...
}
...

更改为以下代码即可

...
func (w *walker) structCodec(t reflect.Type) *codec {
	...
	c.size = func(p unsafe.Pointer, f *structField) int {
		p = deref(p)
		if p != nil {
			n := info.size(p)
			n += sizeOfVarint(uint64(n)) + f.tagsize	// √
			return n
		}
		return 0
	}
	...
}
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.