Lewis Jackson created THRIFT-5238:
-------------------------------------
Summary: GetHashCode can throw NullReferenceException
Key: THRIFT-5238
URL: https://issues.apache.org/jira/browse/THRIFT-5238
Project: Thrift
Issue Type: Bug
Components: netstd - Compiler
Affects Versions: 0.13.0
Environment: OS: Windows 10 Pro Version 1903 Build 18362.900
Reporter: Lewis Jackson
Attachments: Thrift-NullReferenceException-master.zip
Thrift generates a setter for each field like this:
{code:c#}
public string WebhooksSecret
{
get
{
return _WebhooksSecret;
}
set
{
__isset.WebhooksSecret = true;
this._WebhooksSecret = value;
}
}
{code}
But if you set the field to _null_, it will be marked as set in the _isset_
dictionary. Thrift also overrides _GetHashCode()_ like this:
{code:c#}
public override int GetHashCode() {
int hashcode = 157;
unchecked {
if(__isset.Url)
hashcode = (hashcode * 397) + Url.GetHashCode();
if(__isset.WebhooksSecret)
hashcode = (hashcode * 397) + WebhooksSecret.GetHashCode();
if(__isset.Username)
hashcode = (hashcode * 397) + Username.GetHashCode();
if(__isset.WebhooksTypes)
hashcode = (hashcode * 397) + TCollections.GetHashCode(WebhooksTypes);
}
return hashcode;
}
{code}
Note that it doesn't protect against _null_, where other overrides like
_ToString()_ do:
{code:c#}
public override string ToString()
{
//...
if (WebhooksSecret != null && __isset.WebhooksSecret)
{
if(!__first) { sb.Append(", "); }
__first = false;
sb.Append("WebhooksSecret: ");
sb.Append(WebhooksSecret);
}
//...
}
{code}
This can result in the field being set to _null_ then throwing a
_NullReferenceException_ if a consumer calls _GetHashCode()_.
I've attached a repro in the fro
--
This message was sent by Atlassian Jira
(v8.3.4#803005)