Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-5238

GetHashCode can throw NullReferenceException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 0.13.0
    • 0.14.0
    • netstd - Compiler
    • None
    • OS: Windows 10 Pro Version 1903 Build 18362.900

       

    Description

      Thrift generates a setter for each field like this:

          public string WebhooksSecret
          {
            get
            {
              return _WebhooksSecret;
            }
            set
            {
              __isset.WebhooksSecret = true;
              this._WebhooksSecret = value;
            }
          }
      

      But if you set the field to null, it will be marked as set in the isset dictionary. Thrift also overrides GetHashCode() like this:

          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;
          }
      

      Note that it doesn't protect against null, where other overrides like ToString() do:

          public override string ToString()
          {
      //...
            if (WebhooksSecret != null && __isset.WebhooksSecret)
            {
              if(!__first) { sb.Append(", "); }
              __first = false;
              sb.Append("WebhooksSecret: ");
              sb.Append(WebhooksSecret);
            }
      //...
          }
      

      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.

      Attachments

        Issue Links

          Activity

            People

              jensg Jens Geyer
              lewisjackson Lewis Jackson
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m