mysql - vb.net 算术运算导致溢出

标签 mysql vb.net visual-studio-2010 ascii

出现以下错误:

Arithmetic operation resulted in an overflow

在下面的代码中:

Public Function DoEncode(ByVal aPassword As String, ByVal aPassword2 As String) As String
        Dim ascii As New ASCIIEncoding()
        Dim b As Byte() = ascii.GetBytes(aPassword)
        Dim b2 As Byte() = ascii.GetBytes(aPassword2)

        Dim iDiff As UInt32 = b2(0) - b(0)
        For i As Integer = 0 To b.Length - 1
            b(i) += CByte(iDiff)
        Next

        Return ascii.GetString(b)
    End Function

我在登录表单中使用了该功能:

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click

        If UsernameTextBox.Text.Trim() = "" Then
            MessageBox.Show("Please enter your user id", "User ID", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return
        End If

        If PasswordTextBox.Text.Trim() = "" Then
            MessageBox.Show("Please enter your password", "User ID", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return
        End If

        If O.AppMan.DoLogin(UsernameTextBox.Text, PasswordTextBox.Text) Then
            DialogResult = System.Windows.Forms.DialogResult.OK
        End If
    End Sub

上层代码用在:

Public Class TApplicationManager
    Public Class O
        Public Shared AppMan As New TApplicationManager()
    End Class
    Public Function DoEncode(ByVal aPassword As String, ByVal aPassword2 As String) As String
        Dim ascii As New ASCIIEncoding()
        Dim b As Byte() = ascii.GetBytes(aPassword)
        Dim b2 As Byte() = ascii.GetBytes(aPassword2)

        Dim iDiff As UInt32 = b2(0) - b(0)
        For i As Integer = 0 To b.Length - 1
            b(i) += CByte(iDiff)
        Next

        Return ascii.GetString(b)
    End Function

    Public Function DoLogin(ByVal strUser As String, ByVal strPwd As String) As Boolean
        Dim dtbUser As New DataTable()
        Dim dtbUserCare As New DataTable()
        Dim UserName As String
        Dim UserPass As String
        Dim cnn As New MySqlConnection(My.Settings.sys_dbConnectionString)
        Dim strLoginCare As String = "SELECT * FROM tblusers where Username = '" + strUser + "'"
        Dim daUser As New MySqlDataAdapter(strLoginCare, cnn)
        dtbUser.Clear()
        daUser.Fill(dtbUser)

        If dtbUser.Rows.Count = 0 Then
            MessageBox.Show("User does not exist", "User status", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        End If

        Dim drwUser As DataRow = dtbUser.Rows(0)
        UserPass = drwUser("Password").ToString()
        UserName = drwUser("Username").ToString()

        Dim daUserCare As New MySqlDataAdapter(strLoginCare, cnn)
        dtbUserCare.Clear()
        daUserCare.Fill(dtbUserCare)

        If dtbUserCare.Rows.Count = 0 Then
            MessageBox.Show("Invalid user id", "User status", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        End If

        Dim drwUserCare As DataRow = dtbUserCare.Rows(0)

        If drwUserCare("Password").ToString() <> DoEncode(strPwd, drwUserCare("Password").ToString()) Then
            MessageBox.Show("Invalid password. Please try again", "User status", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        End If



        If drwUserCare("status").ToString() <> "Y" Then
            MessageBox.Show("User is not active", "User status", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
            Return False
        End If

        UserPass = drwUserCare("Password").ToString()
        UserName = drwUserCare("Username").ToString()

        Return True
    End Function

End Class

为什么会出现此错误?

最佳答案

好吧,我要试试这个:

    Dim iDiff As UInt32 = b2(0) - b(0)
    For i As Integer = 0 To b.Length - 1
        b(i) += CByte(iDiff)
   Next

A) Uint32 不能为负 - 并且您正在执行两个字节之间的减法。

B) 然后将减去的值转换为 Byte - bytes 不能为负数,如果您尝试这样做,将从内存中抛出此异常。

我实际上不明白你在上面的循环中试图做什么。

编辑

根据您对这段代码在 C# 中运行但在 VB 中运行无效的评论,我的回答仍然适用。由于负数或大于 255,转换失败。

C# 不会像 VB 那样自动检查溢出错误。

尽管我认为将代码更改为不溢出会更好,但您可以禁用此行为。您可能会得到意想不到的结果。

参见:https://msdn.microsoft.com/en-us/library/aa716336(v=vs.60).aspx

您可以通过以下方式禁用它:

Project properties, enable Configuration Properties => Optimizations => Remove Integer Overflow Checks.

通过将转换/计算包装在“已检查” block 中,可以在 C# 中实现相同的行为。

关于mysql - vb.net 算术运算导致溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33932721/

相关文章:

PHP:未知语法可能是一个没有括号的函数?

mysql - 如果不满足条件,如何在选定范围中包含上一行

vb.net - 求解一次方程 - 代码优化

vb.net - 声明 bool 值时分配默认值与使用 if 语句的两个子句?

c# - 我可以使用 VS2010 PrivateObject 访问静态类中的静态字段吗?

qt - 无法使用 MSVC 2010 x86 构建 qt5.2.1 静态

mysql - 错误: BLOB/TEXT column 'multimedia' used in key specification without a key length

c# - 将 Byte[] 数组保存到 MYSQL

asp.net - 从 WindowsIdentity 获取用户的显示名称

visual-studio-2010 - Visual Studio 2010 IDE布局