Details
-
Bug
-
Status: Closed
-
Critical
-
Resolution: Fixed
-
0.9
-
Win7, amd64
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
- duplicates
-
THRIFT-1973 TCompactProtocol in C# lib does not serialize and deserialize negative int32 and int64 number correctly
- Closed