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

GetHashCode can throw NullReferenceException

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 0.13.0
    • Fix Version/s: 0.14.0
    • Component/s: netstd - Compiler
    • Labels:
      None
    • Environment:

      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

              • Assignee:
                jensg Jens Geyer
                Reporter:
                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