我收到以下错误:
算术运算导致溢出
代码如下:
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
为什么我收到此错误?
答案 0 :(得分:1)
好的我打算这样做是这样的:
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)然后您将该减去的值转换为字节 - 字节不能为负数,如果您尝试这样做,则从内存中将抛出此异常。
我实际上并不了解你在上面的循环中尝试做什么。
修改强>
根据您对使用c#而不是VB的代码的评论,我的答案仍然适用。由于负数或大于255,转换失败。
c#不会自动检查VB的溢出错误。
您可以禁用此行为,但在我看来,将代码更改为不溢出会更好。您可能会得到意想不到的结果。
请参阅:https://msdn.microsoft.com/en-us/library/aa716336(v=vs.60).aspx
您可以通过以下方式停用它:
项目属性,启用配置属性=&gt;优化 =&GT;删除整数溢出检查。
通过将转换/计算包装在“已检查”块中,可以在c#中实现相同的行为。