adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.25k stars 479 forks source link

Compiler Warning (level 3) CS0675 in DefalteManager.cs #79

Closed haykpetros closed 8 years ago

haykpetros commented 9 years ago

When building SharpCompress project with Warning level above 2 (usually 4 is default) it will fail on lines 683 and 695 of DeflateManager.cs:

unchecked
            {
                if (bi_valid > (int) Buf_size - len)
                {
                    //int val = value;
                    //      bi_buf |= (val << bi_valid);

                    bi_buf |= (short) ((value << bi_valid) & 0xffff); <-- THIS IS WHERE IT FAILS
                    //put_short(bi_buf);
                    pending[pendingCount++] = (byte) bi_buf;
                    pending[pendingCount++] = (byte) (bi_buf >> 8);

                    bi_buf = (short) ((uint) value >> (Buf_size - bi_valid));
                    bi_valid += len - Buf_size;
                }
                else
                {
                    //      bi_buf |= (value) << bi_valid;
                    bi_buf |= (short) ((value << bi_valid) & 0xffff); <-- THIS IS WHERE IT FAILS
                    bi_valid += len;
                }
            }

I believe following line:

bi_buf |= (short) ((value << bi_valid) & 0xffff);

can be replaced using following code instead:

bi_buf = (short)(bi_buf | (short)((value << bi_valid) & 0xffff));
adamhathcock commented 9 years ago

Thanks for this, could you make this a Pull Request by chance?

haykpetros commented 9 years ago

It seems that this might be a bug in Visual Studio 2015, so we are going to wait until it is verified.

Gachl commented 8 years ago

Considering that there's a wdc in MSDN about this I doubt this is a bug.

benshoof commented 8 years ago

It's never a bug in the compiler... except this time. Even worse, a commit has been made to SharpCompress that tried to fix this but instead broke the build on VS2013 with a legitimate case of the warning it tried to prevent.

The deflate code is from DotNetZip from 2009 and has been working without any compiler saying peep until VS2015 showed up and threw CS0675. This change in compiler behavior is a bug: https://github.com/dotnet/roslyn/issues/4027

About a month ago, pull request https://github.com/adamhathcock/sharpcompress/pull/101 containing 18bd8102281c04c7eb09ecfdd984d7382b629f2c changed the deflate code in what looks like an attempt to satisfy VS2015. Unfortunately it creates a real case of CS0675 that VS2013 does catch so right now the build is broken on VS2013.

I've created pull request https://github.com/adamhathcock/sharpcompress/pull/104 that reverts 18bd8102281c04c7eb09ecfdd984d7382b629f2c and disables CS0675 warnings on Defalte.send_bits().