You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Lewis Jackson (Jira)" <ji...@apache.org> on 2020/06/24 11:17:00 UTC

[jira] [Updated] (THRIFT-5238) GetHashCode can throw NullReferenceException

     [ https://issues.apache.org/jira/browse/THRIFT-5238?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lewis Jackson updated THRIFT-5238:
----------------------------------
    Description: 
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 form of a unit test library. There are instructions to run this in the README of the zip.

  was:
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


> 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
>            Priority: Major
>         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 form of a unit test library. There are instructions to run this in the README of the zip.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)