3

我正在使用 C# 进行开发,并使用 Dotnetzip 的源代码将应用程序放在一个文件中。

但是在运行时,我得到了异常:

System.OverflowException: Arithmetic operation resulted in an overflow.
   at Ionic.Crc.CRC32..ctor(Int32 polynomial, Boolean reverseBits) in DotNetZip\CRC32.cs:line 452
   at Ionic.Crc.CRC32..ctor(Boolean reverseBits) in DotNetZip\CRC32.cs:line 418
   at Ionic.Crc.CRC32..ctor() in DotNetZip\CRC32.cs:line 398
   at Ionic.Crc.CrcCalculatorStream..ctor(Boolean leaveOpen, Int64 length, Stream stream, CRC32 crc32) in DotNetZip\CRC32.cs:line 622
   at Ionic.Crc.CrcCalculatorStream..ctor(Stream stream) in DotNetZip\CRC32.cs:line 519
   at Ionic.Zip.ZipEntry.ExtractOne(Stream output) in DotNetZip\ZipEntry.Extract.cs:line 1043
   at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, String password) in DotNetZip\ZipEntry.Extract.cs:line 870
   at Ionic.Zip.ZipEntry.Extract(String baseDirectory, ExtractExistingFileAction extractExistingFile) in DotNetZip\ZipEntry.Extract.cs:line 240
   at ItxInstaller.Package.GetData(String instFilePath) in Package.cs:line 32
   at ItxInstaller.Install..ctor() in Install.cs:line 26
   at ItxInstaller.Program.Main(String[] args) in Program.cs:line 54

Install.cs 是这样的:

public Install()
{
    // The InitializeComponent() call is required for Windows Forms designer support.
    this.InitializeComponent();
    Package pack = Package.GetData(Program.args[1]);
    this.Title.Text = pack.AppName;
    this.PathBox.Text = pack.GeneratePath();
}

Package.cs 包含 GetData():

class Package
    {
        public string AppName;
        public string ExePath;

        private string FilePath;

        public Package(string filePath)
        {
            this.ValueInit(filePath);
            this.FilePath = filePath;
        }

        public static Package GetData(string instFilePath)
        {
            using (ZipFile Package = ZipFile.Read(instFilePath))
            {
                Package["app.itx"].Extract(Path.GetTempPath(), ExtractExistingFileAction.OverwriteSilently);
            }

            return new Package(Path.Combine(Path.GetTempPath(), "app.itx"));
        }
}

如何解决这个异常?

4

1 回答 1

1

当使用“-checked+”作为参数时,我可以用 Mono 重现这个问题,即启用算术检查。省略此参数会生成一个有效的可执行文件。

在 Visual Studio 中还有一个项目设置。

我已将相关代码提取到此工作程序中,以便您自己测试:

using System;

namespace Test {
    public class Program {
    public static void Main(string[] args) {
            CRC32 test = new CRC32();
            Console.Out.WriteLine(test.dwPolynomial);
        }
    }

    public class CRC32 {
        public UInt32 dwPolynomial;

        public CRC32() : this(false) {
        }

        public CRC32(bool reverseBits) :
            this(unchecked((int)0xEDB88320), reverseBits) {
        }

        public CRC32(int polynomial, bool reverseBits) {
            this.dwPolynomial = (uint) polynomial; // this is line 452
        }
    }
}

使用dcms test.cs && mono test.exe运行良好。

使用dcms -checked+ test.cs && mono test.exe结果

Unhandled Exception: System.OverflowException: Number overflow.
    at Test.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.OverflowException: Number overflow.
    at Test.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0

更新:如果您不能或不想禁用算术检查,那么您可以将 452 之类的源代码修改为

this.dwPolynomial = unchecked((uint) polynomial);

这将关闭表达式的算术检查。

于 2013-06-14T08:48:21.737 回答