فهرست منبع

Improve invalid reflect value handling.

It was previously possible for an invalid reflect value to lead to a panic
in certain obscure cases.  Rather than adding multiple checks for the
invalid reflect value, handle invalid reflect values immediately.
Dave Collins 13 سال پیش
والد
کامیت
a9907c1584
2فایلهای تغییر یافته به همراه18 افزوده شده و 4 حذف شده
  1. 9 2
      spew/dump.go
  2. 9 2
      spew/format.go

+ 9 - 2
spew/dump.go

@@ -139,8 +139,14 @@ func (d *dumpState) dumpPtr(v reflect.Value) {
 // appropriately.  It is a recursive function, however circular data structures
 // are detected and handled properly.
 func (d *dumpState) dump(v reflect.Value) {
-	// Handle pointers specially.
+	// Handle invalid reflect values immediately.
 	kind := v.Kind()
+	if kind == reflect.Invalid {
+		d.w.Write(invalidAngleBytes)
+		return
+	}
+
+	// Handle pointers specially.
 	if kind == reflect.Ptr {
 		d.pad()
 		d.dumpPtr(v)
@@ -169,7 +175,8 @@ func (d *dumpState) dump(v reflect.Value) {
 
 	switch kind {
 	case reflect.Invalid:
-		d.w.Write(invalidAngleBytes)
+		// Do nothing.  We should never get here since invalid has already
+		// been handled above.
 
 	case reflect.Bool:
 		printBool(d.w, v.Bool())

+ 9 - 2
spew/format.go

@@ -197,8 +197,14 @@ func (f *formatState) formatPtr(v reflect.Value) {
 // dealing with and formats it appropriately.  It is a recursive function,
 // however circular data structures are detected and handled properly.
 func (f *formatState) format(v reflect.Value) {
-	// Handle pointers specially.
+	// Handle invalid reflect values immediately.
 	kind := v.Kind()
+	if kind == reflect.Invalid {
+		f.fs.Write(invalidAngleBytes)
+		return
+	}
+
+	// Handle pointers specially.
 	if kind == reflect.Ptr {
 		f.formatPtr(v)
 		return
@@ -224,7 +230,8 @@ func (f *formatState) format(v reflect.Value) {
 
 	switch kind {
 	case reflect.Invalid:
-		f.fs.Write(invalidAngleBytes)
+		// Do nothing.  We should never get here since invalid has already
+		// been handled above.
 
 	case reflect.Bool:
 		printBool(f.fs, v.Bool())