I cannot understand problem with argument of the function

monarch_dodra monarchdodra at gmail.com
Wed Sep 18 02:56:25 PDT 2013


On Wednesday, 18 September 2013 at 09:09:29 UTC, mrd wrote:
> On Wednesday, 18 September 2013 at 09:07:09 UTC, mrd wrote:
>
>>> Looks like data corruption. Can't help you much without a 
>>> full piece of code that *reproduces* the issue (reducing it 
>>> helps too).
>>
>> I will try to do it later. (I tried to copy these functions in 
>> a separate file - the problem is not reproduced.)
>>
>
> this file:
> http://pastebin.com/MCm5Yu7K

I had to write "isMsbSet" myself. Also, you "main" doesn't 
compile:

//----
for( long i = -100; i < 100; i++ )
     packVarint( i );
//----

since "pack" only accepts unsigned.

After changing it to packVariant!ulong, it compiles. But it 
*could* explain the "weird" values you are seeing: -100 => 
18446744073709551516. I was wrong about the mask thing, that was 
just my windows calculator playing tricks on me.

Your out contracts also fail, because the body of your function 
modifies "value", but then, you use it in your contract. I had to 
change the body to use a copy of the passed in arguments, to 
verify them.

 From there, it *still* fails, because your unpackVarint is wrong. 
You store the result in a size_t, which immediatly overflows. The 
irony is you put a comment that says "big sized type used also 
for overflow checking". Why not use a T, or result directly?

Even, then, a problem remains that "data[i] & 0b_0111_1111" is of 
type int, causing overflow and sign mismatch for anything larger 
than int.max


More information about the Digitalmars-d-learn mailing list