internal/impl: fix size for zero-length packed extensions

The size calculation for packed repeated extension fields was
considering a zero-length list as encoding to a zero-length
wire.BytesType field, rather than being omitted entirely.

Change-Id: I7d4424a21ca8afd4fa81391caede49cadb4e2505
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/212297
Reviewed-by: Joe Tsai <joetsai@google.com>
diff --git a/internal/impl/codec_gen.go b/internal/impl/codec_gen.go
index bc7e414..060d5bd 100644
--- a/internal/impl/codec_gen.go
+++ b/internal/impl/codec_gen.go
@@ -296,8 +296,12 @@
 // sizeBoolPackedSliceValue returns the size of wire encoding a []bool value as a packed repeated Bool.
 func sizeBoolPackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
 	n := 0
-	for i, llen := 0, list.Len(); i < llen; i++ {
+	for i, llen := 0, llen; i < llen; i++ {
 		v := list.Get(i)
 		n += wire.SizeVarint(wire.EncodeBool(v.Bool()))
 	}
@@ -420,8 +424,12 @@
 // sizeEnumPackedSliceValue returns the size of wire encoding a [] value as a packed repeated Enum.
 func sizeEnumPackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
 	n := 0
-	for i, llen := 0, list.Len(); i < llen; i++ {
+	for i, llen := 0, llen; i < llen; i++ {
 		v := list.Get(i)
 		n += wire.SizeVarint(uint64(v.Enum()))
 	}
@@ -737,8 +745,12 @@
 // sizeInt32PackedSliceValue returns the size of wire encoding a []int32 value as a packed repeated Int32.
 func sizeInt32PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
 	n := 0
-	for i, llen := 0, list.Len(); i < llen; i++ {
+	for i, llen := 0, llen; i < llen; i++ {
 		v := list.Get(i)
 		n += wire.SizeVarint(uint64(int32(v.Int())))
 	}
@@ -1054,8 +1066,12 @@
 // sizeSint32PackedSliceValue returns the size of wire encoding a []int32 value as a packed repeated Sint32.
 func sizeSint32PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
 	n := 0
-	for i, llen := 0, list.Len(); i < llen; i++ {
+	for i, llen := 0, llen; i < llen; i++ {
 		v := list.Get(i)
 		n += wire.SizeVarint(wire.EncodeZigZag(int64(int32(v.Int()))))
 	}
@@ -1371,8 +1387,12 @@
 // sizeUint32PackedSliceValue returns the size of wire encoding a []uint32 value as a packed repeated Uint32.
 func sizeUint32PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
 	n := 0
-	for i, llen := 0, list.Len(); i < llen; i++ {
+	for i, llen := 0, llen; i < llen; i++ {
 		v := list.Get(i)
 		n += wire.SizeVarint(uint64(uint32(v.Uint())))
 	}
@@ -1688,8 +1708,12 @@
 // sizeInt64PackedSliceValue returns the size of wire encoding a []int64 value as a packed repeated Int64.
 func sizeInt64PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
 	n := 0
-	for i, llen := 0, list.Len(); i < llen; i++ {
+	for i, llen := 0, llen; i < llen; i++ {
 		v := list.Get(i)
 		n += wire.SizeVarint(uint64(v.Int()))
 	}
@@ -2005,8 +2029,12 @@
 // sizeSint64PackedSliceValue returns the size of wire encoding a []int64 value as a packed repeated Sint64.
 func sizeSint64PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
 	n := 0
-	for i, llen := 0, list.Len(); i < llen; i++ {
+	for i, llen := 0, llen; i < llen; i++ {
 		v := list.Get(i)
 		n += wire.SizeVarint(wire.EncodeZigZag(v.Int()))
 	}
@@ -2322,8 +2350,12 @@
 // sizeUint64PackedSliceValue returns the size of wire encoding a []uint64 value as a packed repeated Uint64.
 func sizeUint64PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
 	n := 0
-	for i, llen := 0, list.Len(); i < llen; i++ {
+	for i, llen := 0, llen; i < llen; i++ {
 		v := list.Get(i)
 		n += wire.SizeVarint(v.Uint())
 	}
@@ -2627,7 +2659,11 @@
 // sizeSfixed32PackedSliceValue returns the size of wire encoding a []int32 value as a packed repeated Sfixed32.
 func sizeSfixed32PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
-	n := list.Len() * wire.SizeFixed32()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
+	n := llen * wire.SizeFixed32()
 	return tagsize + wire.SizeBytes(n)
 }
 
@@ -2924,7 +2960,11 @@
 // sizeFixed32PackedSliceValue returns the size of wire encoding a []uint32 value as a packed repeated Fixed32.
 func sizeFixed32PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
-	n := list.Len() * wire.SizeFixed32()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
+	n := llen * wire.SizeFixed32()
 	return tagsize + wire.SizeBytes(n)
 }
 
@@ -3221,7 +3261,11 @@
 // sizeFloatPackedSliceValue returns the size of wire encoding a []float32 value as a packed repeated Float.
 func sizeFloatPackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
-	n := list.Len() * wire.SizeFixed32()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
+	n := llen * wire.SizeFixed32()
 	return tagsize + wire.SizeBytes(n)
 }
 
@@ -3518,7 +3562,11 @@
 // sizeSfixed64PackedSliceValue returns the size of wire encoding a []int64 value as a packed repeated Sfixed64.
 func sizeSfixed64PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
-	n := list.Len() * wire.SizeFixed64()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
+	n := llen * wire.SizeFixed64()
 	return tagsize + wire.SizeBytes(n)
 }
 
@@ -3815,7 +3863,11 @@
 // sizeFixed64PackedSliceValue returns the size of wire encoding a []uint64 value as a packed repeated Fixed64.
 func sizeFixed64PackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
-	n := list.Len() * wire.SizeFixed64()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
+	n := llen * wire.SizeFixed64()
 	return tagsize + wire.SizeBytes(n)
 }
 
@@ -4112,7 +4164,11 @@
 // sizeDoublePackedSliceValue returns the size of wire encoding a []float64 value as a packed repeated Double.
 func sizeDoublePackedSliceValue(listv protoreflect.Value, tagsize int, _ marshalOptions) (size int) {
 	list := listv.List()
-	n := list.Len() * wire.SizeFixed64()
+	llen := list.Len()
+	if llen == 0 {
+		return 0
+	}
+	n := llen * wire.SizeFixed64()
 	return tagsize + wire.SizeBytes(n)
 }