Browse Source

codec: json: fix base64 decoding for binary data

DecodedLen is the maximum length. This means that we MUST check
the actual decoded length after decoding, and possibly truncate
the returned slice appropriately.

Fixes #70
Ugorji Nwoke 10 năm trước cách đây
mục cha
commit
9a5173b885
1 tập tin đã thay đổi với 8 bổ sung1 xóa
  1. 8 1
      codec/json.go

+ 8 - 1
codec/json.go

@@ -733,7 +733,14 @@ func (d *jsonDecDriver) DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut [
 	} else {
 		bsOut = make([]byte, slen)
 	}
-	base64.StdEncoding.Decode(bsOut, bs0)
+	slen2, err := base64.StdEncoding.Decode(bsOut, bs0)
+	if err != nil {
+		d.d.errorf("json: error decoding base64 binary '%s': %v", bs0, err)
+		return nil
+	}
+	if slen != slen2 {
+		bsOut = bsOut[:slen2]
+	}
 	return
 }