cosmos-sdk/codec
Emmanuel T Odeke 56fc3fc572
codec/types: avoid unnecessary allocations for NewAnyWithCustomTypeURL on error (#8605)
Avoids a bleed out attack in which a node can be made to allocate
memory slowly or very fast in small strides, by sending bad data
to code that invokes NewAnyWithCustomTypeURL, in which we
unconditionally returned a new Any object. On a 64-bit machine,
this would waste 96 bytes per invocation even on error.

Added a test to ensure zero allocations with a fixed error returned.
Also added a benchmark which shows reduction in wasted allocations and
wasted CPU time:

```shell
$ benchstat before.txt after.txt
name                                            old time/op    new time/op    delta
NewAnyWithCustomTypeURLWithErrorReturned-8      142ns ± 6%      55ns ±12%   -61.65%  (p=0.000 n=9+10)

name                                            old alloc/op   new alloc/op   delta
NewAnyWithCustomTypeURLWithErrorReturned-8      96.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)

name                                            old allocs/op  new allocs/op  delta
NewAnyWithCustomTypeURLWithErrorReturned-8      1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
```

Fixes #8537
2021-02-17 10:13:00 +00:00
..
legacy Fix CryptoCdc inconsistent (#7987) 2020-12-02 14:50:50 +01:00
types codec/types: avoid unnecessary allocations for NewAnyWithCustomTypeURL on error (#8605) 2021-02-17 10:13:00 +00:00
unknownproto unknownproto: check result from protowire.ConsumeFieldValue and return an error (#7770) 2020-11-09 17:58:13 +00:00
amino.go adr-028 address generation (#8415) 2021-02-15 15:32:51 +00:00
amino_codec.go Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080) 2020-12-08 09:27:08 +00:00
amino_codec_test.go codecs: rename MakeCodecs rename and docs update (#8245) 2021-01-07 21:50:52 +00:00
any_test.go Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080) 2020-12-08 09:27:08 +00:00
codec.go Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080) 2020-12-08 09:27:08 +00:00
codec_common_test.go Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080) 2020-12-08 09:27:08 +00:00
json.go Add ADR 031 BaseApp and codec infrastructure (#7519) 2020-10-15 13:07:59 +00:00
proto_codec.go Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080) 2020-12-08 09:27:08 +00:00
proto_codec_test.go Move and update codec.MarshalAny functions to codec.Marshaler interface (#8080) 2020-12-08 09:27:08 +00:00
yaml.go docs: Update the code.MarshalYAML docs (#7496) 2020-10-09 14:08:02 +00:00
yaml_test.go Migrate BaseAccount PubKey to use Any (#7268) 2020-09-25 08:41:16 +00:00