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

Thrift.Protocol.TCompactProtocol, intToZigZag data lost (TCompactProtocol.cs)

    XMLWordPrintableJSON

    Details

      Description

      In TCompactProtocol.cs, the function:
      /**

      • Convert n into a zigzag int. This allows negative numbers to be
      • represented compactly as a varint.
        */
        private uint intToZigZag(int n) { return (uint)(((uint)n << 1) ^ ((uint)n >> 31)); }
        will make wrong number while the integer is negative. Check the test code below: (so do longToZigZag.)

        ===============
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;

        namespace Test
        {
        class Program
        {
        static void Main(string[] args)
        {
        unchecked
        {
        int num = -1;
        if (num != zigzagToInt(intToZigZag(num)))
        { Console.WriteLine("Transform failed!"); }
        else
        { Console.WriteLine("Transform passed!"); }

        if (num != zigzagToInt(intToZigZagNew(num)))
        { Console.WriteLine("Transform failed!"); }
        else
        { Console.WriteLine("Transform passed!"); }

        Console.ReadLine();
        }
        }

        static int zigzagToInt(uint n)
        { return (int)(n >> 1) ^ (-(int)(n & 1)); }


        static uint intToZigZag(int n)
        { return (uint)(((uint)n << 1) ^ ((uint)n >> 31)); }

      static uint intToZigZagNew(int n)

      { return (uint)((n << 1) ^ (n >> 31)); }

      }
      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                luhan LuHan
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - 12h
                  12h
                  Remaining:
                  Remaining Estimate - 12h
                  12h
                  Logged:
                  Time Spent - Not Specified
                  Not Specified