Browse Source

goprotobuf: Fix handling of large tag numbers.

Fixes #19.

R=r
CC=golang-dev
http://codereview.appspot.com/5128059
David Symonds 14 years ago
parent
commit
d73d7b1d7a
3 changed files with 24 additions and 2 deletions
  1. 17 0
      proto/all_test.go
  2. 1 1
      proto/properties.go
  3. 6 1
      proto/testdata/test.proto

+ 17 - 0
proto/all_test.go

@@ -1284,6 +1284,23 @@ func TestSetDefaultsWithSubMessage(t *testing.T) {
 	}
 }
 
+func TestMaximumTagNumber(t *testing.T) {
+	m := &MaxTag{
+		LastField: String("natural goat essence"),
+	}
+	buf, err := Marshal(m)
+	if err != nil {
+		t.Fatalf("proto.Marshal failed: %v", err)
+	}
+	m2 := new(MaxTag)
+	if err := Unmarshal(buf, m2); err != nil {
+		t.Fatalf("proto.Unmarshal failed: %v", err)
+	}
+	if got, want := GetString(m2.LastField), *m.LastField; got != want {
+		t.Errorf("got %q, want %q", got, want)
+	}
+}
+
 func BenchmarkMarshal(b *testing.B) {
 	b.StopTimer()
 

+ 1 - 1
proto/properties.go

@@ -401,7 +401,7 @@ func (p *Properties) setEncAndDec(typ reflect.Type) {
 	if p.Packed {
 		wire = WireBytes
 	}
-	x := p.Tag<<3 | wire
+	x := uint32(p.Tag)<<3 | uint32(wire)
 	i := 0
 	for i = 0; x > 127; i++ {
 		p.tagbuf[i] = 0x80 | uint8(x&0x7F)

+ 6 - 1
proto/testdata/test.proto

@@ -167,7 +167,7 @@ message GoTest {
   optional group OptionalGroup = 90 {
     required string RequiredField = 91;
   };
-};
+}
 
 // For testing skipping of unrecognized fields.
 // Numbers are all big, larger than tag numbers in GoTestField,
@@ -193,6 +193,11 @@ message PackedTest {
   repeated int32 b = 1 [packed=true];
 }
 
+message MaxTag {
+  // Maximum possible tag number.
+  optional string last_field = 536870911;
+}
+
 // Smaller tests for ASCII formatting.
 
 message InnerMessage {