none
VB.NET Random System.StackOverflowException Error

    Question

  • Hello everybody,

    I'm a newbie in VB.NET dev.

    I get a code from a app and i would to test it alone: this is a xor decrypter.

    Sometimes it works well and often it makes me cry with System.StackOverflowException from Public Sub New()

    there is lot of line (0 to 999) like this

    Me
    .keyarr(0) = Chr(35) & Chr(54) & Chr(158) & Chr(43) & Chr(90) & Chr(143) & Chr(107) & Chr(172) & Chr(64) & Chr(150) & Chr(139) & Chr(199) & Chr(1) & Chr(26) & Chr(71) & Chr(51) & Chr(139) & Chr(187) & Chr(245) & Chr(212) & Chr(44) & Chr(232) & Chr(67) & Chr(93) & Chr(153) & Chr(206) & Chr(100) & Chr(212) & Chr(81) & Chr(159) & Chr(182) & Chr(47) & Chr(82) & Chr(11) & Chr(190) & Chr(147) & Chr(29) & Chr(57) & Chr(249) & Chr(106) & Chr(252) & Chr(32) & Chr(132) & Chr(180) & Chr(126) & Chr(152) & Chr(252) & Chr(231) & Chr(50) & Chr(243) & Chr(195) & Chr(185) & Chr(125) & Chr(129) & Chr(193) & Chr(50) & Chr(169) & Chr(202) & Chr(16) & Chr(81) & Chr(250) & Chr(231) & Chr(168) & Chr(158) & Chr(233) & Chr(195) & Chr(63) & Chr(24) & Chr(85) & Chr(161) & Chr(180) & Chr(169) & Chr(212) & Chr(190) & Chr(19) & Chr(180) & Chr(225) & Chr(122) & Chr(151) & Chr(209) & Chr(26) & Chr(164) & Chr(189) & Chr(83) & Chr(76) & Chr(166) & Chr(178) & Chr(180) & Chr(134) & Chr(253) & Chr(79) & Chr(123) & Chr(217) & Chr(156) & Chr(225) & Chr(164) & Chr(42) & Chr(1) & Chr(245) & Chr(78) & Chr(132) & Chr(58) & Chr(68) & Chr(138) & Chr(118) & Chr(236) & Chr(43) & Chr(35) & Chr(248) & Chr(103) & Chr(188) & Chr(57) & Chr(35) & Chr(218) & Chr(179) & Chr(129) & Chr(122) & Chr(249) & Chr(122) & Chr(17) & Chr(249) & Chr(201) & Chr(139) & Chr(120) & Chr(154) & Chr(19) & Chr(77) & Chr(20)
    

     

    I think there is enough memory in my computer but i don't understand why i get this error, in the app there is no problem.

    May i made a mistake in call function or something like that ?

     

    Here the rest of the code:

    Imports System
    
    Module Module1
    
    
    
    
     Sub Main()
    
      Dim password As New Password
    
      password.EncryptedPassword = "2945D7F0F653C2EDFF"
      password.keyindex = 154
    
    
    
      password.getPassword()
    
    
    
    
    
     End Sub
     Public Class Password
    
    
    
      ' Keyarray, where the Xor-Keys are stored (To compile, use 0 to 999, for debugging use 0 to 25!)
    
      Const debugging As Boolean = True
    
      Private keyarr(0 To 999) As String
    
      ' Keyindex is used to find the Xor-Key in the Keyarray
      Public keyindex As Integer = 0
    
      ' Stores the encrypted password
      Public EncryptedPassword As String = ""
    
      ' Initiates the Keyarray
    
    
    
    
    
    [Public Sub() ... End Sub] 0 to 999 lines ...
    
    
    
    
    
    'This Function decrypts a password. The Function takes the Password and the KeyIndex. Encryption will be done with an Xor operation, and the result will be HEX-encoded, so the result is a string
      Private Function dec(ByVal Hex As String, ByVal keyindex As Integer) As String
    
       'lenght of the password
       Dim Lenght As Integer
    
       'The whole key, which will be used to encrypt the key. This key will be shorten to the needed lenght, and then be stored in the xorwith-variable.
       Dim Key As String
    
       'The result, as cleartext
       Dim Result As String = ""
    
       'Counter Variable
       Dim pwPosition As Integer
    
       'This is set to the numeric value of the character which is going to be xored against the key
       Dim intXorValueHex As Short
    
       'This is set to the numeric value of the character which is going to be xored against the Hex
       Dim intXorValueKey As Short
    
       'String for Temporary Use
       Dim TempStr As String
    
       'Integer for Temporary Use
       Dim TempInt As Integer
    
       Key = keyarr(keyindex)
    
       If Me.EncryptedPassword.Length > 0 Then
    
    
    
        Try
         'get the lenght of the password, it's half of the hex-lenght
         Lenght = Hex.Length / 2
    
         'As long as we are inside the Password
         For pwPosition = 1 To Lenght Step 1
    
          'Get the Char from the Key and the Hex as integer, at which position we are.
          intXorValueHex = CInt("&H" & Mid(Hex, pwPosition * 2 - 1, 2))
          intXorValueKey = Asc(Mid(Key, pwPosition, 1))
    
          'XOR this characters against eachother
          TempInt = (intXorValueHex Xor intXorValueKey)
    
          'Write the Char as Hex into the TempStr
          TempStr = Chr(TempInt)
    
          'Append the two chars of the Hex to the result-string
          Result &= TempStr
         Next
        Catch ex As System.Exception
         'MsgBox(ex.Message & vbNewLine & "Most possible, the Password was unencrypted")
        End Try
    
       End If
    
    
    
       Console.WriteLine(Result)
    
       Return Result
    
      End Function
    
    
      Public Function getPassword() As String
       ' Function which returns the Password as string
       Return Me.dec(EncryptedPassword, keyindex)
      End Function
    
    
     End Class
    
    
    End Module
    

     

    Thanks for your help

     

    Best Regards

     

    Samuel

    Tuesday, April 27, 2010 11:01 AM

Answers

  • Hi Samuel.  If I understand you correctly the character concatenation may be causing the stack overflow exception.  Maybe you could try a different approach by avoiding concatenation by using a StringBuilder.  This can be done by passing in an array of the char number values to a helper function that returns the desired string value, like the following code.  The benefit is that the function is reusable for each set of values and potentially useful if your array values become dynamic.  See if this code fixes the issue.

      Public Sub New()
    
        Me.keyarr(0) = GetKeyArrayValue(New Int32() {35, 54, 158, 43, 90, 143, 107, 172, 64, 150, 139, 199, 1, 26, 71, 51, 139, 187, 245, 212, 44, 232, 67, 93, 153, 206, 100, 212, 81, 159, 182, 47, 82, 11, 190, 147, 29, 57, 249, 106, 252, 32, 132, 180, 126, 152, 252, 231, 50, 243, 195, 185, 125, 129, 193, 50, 169, 202, 16, 81, 250, 231, 168, 158, 233, 195, 63, 24, 85, 161, 180, 169, 212, 190, 19, 180, 225, 122, 151, 209, 26, 164, 189, 83, 76, 166, 178, 180, 134, 253, 79, 123, 217, 156, 225, 164, 42, 1, 245, 78, 132, 58, 68, 138, 118, 236, 43, 35, 248, 103, 188, 57, 35, 218, 179, 129, 122, 249, 122, 17, 249, 201, 139, 120, 154, 19, 77, 20})
    
      End Sub
    
      Private Function GetKeyArrayValue(ByVal AscValues() As Int32) As String
        Dim sb As New System.Text.StringBuilder
        For Each i As Int32 In AscValues
          sb.Append(Chr(i))
        Next
        Return sb.ToString
      End Function
    Tuesday, April 27, 2010 2:19 PM
  • To get a stackoverflow there is mostly caused by recursion.

    Computer memory is currently much less caussing the error.

    However, I see no recursion in your code. Therefore

    What is in this code,

    ' Initiates the Keyarray


    [Public Sub() ... End Sub] 0 to 999 lines ...

    probably is in this part your stack overflow


    Success
    Cor
    Tuesday, April 27, 2010 2:48 PM
  • I just understand that is the debbuger can't run the program :( , i can run after compilating

     

    thanks for all

     

    Sammy

    Wednesday, April 28, 2010 10:28 AM

All replies

  • Hi Samuel.  If I understand you correctly the character concatenation may be causing the stack overflow exception.  Maybe you could try a different approach by avoiding concatenation by using a StringBuilder.  This can be done by passing in an array of the char number values to a helper function that returns the desired string value, like the following code.  The benefit is that the function is reusable for each set of values and potentially useful if your array values become dynamic.  See if this code fixes the issue.

      Public Sub New()
    
        Me.keyarr(0) = GetKeyArrayValue(New Int32() {35, 54, 158, 43, 90, 143, 107, 172, 64, 150, 139, 199, 1, 26, 71, 51, 139, 187, 245, 212, 44, 232, 67, 93, 153, 206, 100, 212, 81, 159, 182, 47, 82, 11, 190, 147, 29, 57, 249, 106, 252, 32, 132, 180, 126, 152, 252, 231, 50, 243, 195, 185, 125, 129, 193, 50, 169, 202, 16, 81, 250, 231, 168, 158, 233, 195, 63, 24, 85, 161, 180, 169, 212, 190, 19, 180, 225, 122, 151, 209, 26, 164, 189, 83, 76, 166, 178, 180, 134, 253, 79, 123, 217, 156, 225, 164, 42, 1, 245, 78, 132, 58, 68, 138, 118, 236, 43, 35, 248, 103, 188, 57, 35, 218, 179, 129, 122, 249, 122, 17, 249, 201, 139, 120, 154, 19, 77, 20})
    
      End Sub
    
      Private Function GetKeyArrayValue(ByVal AscValues() As Int32) As String
        Dim sb As New System.Text.StringBuilder
        For Each i As Int32 In AscValues
          sb.Append(Chr(i))
        Next
        Return sb.ToString
      End Function
    Tuesday, April 27, 2010 2:19 PM
  • To get a stackoverflow there is mostly caused by recursion.

    Computer memory is currently much less caussing the error.

    However, I see no recursion in your code. Therefore

    What is in this code,

    ' Initiates the Keyarray


    [Public Sub() ... End Sub] 0 to 999 lines ...

    probably is in this part your stack overflow


    Success
    Cor
    Tuesday, April 27, 2010 2:48 PM
  • i forget to say that sometimes it work one time and not the second, when i boot my computer i can do one time and no futhermore

    except if i build with

     password.EncryptedPassword = ""
    
    password.keyindex = 0

    then i rebuild next with and it work sometimes or not... may be it is a clue ?

     password.EncryptedPassword = "2945D7F0F653C2EDFF"
    
    password.keyindex = 154

    I don't know if you will understand that i would say :S

    Wednesday, April 28, 2010 6:16 AM
  • That module which you show is that true or is that an example?

    (Is it maybe a kind of service which is running?)


    Success
    Cor
    Wednesday, April 28, 2010 6:33 AM
  • thanks for your answer

     

    it should work for the encrypted password wich have keyindex = 0

    but i have all from 0 to 999

    there is no way to allow the code to take more memory ? that what overflow means there is no enought memory :(

    Wednesday, April 28, 2010 8:20 AM
  • Stack overflow does not mean not enough memory (altough it is related)

    It means mostly that it has used to many stacks, which happens when you are endless creating new stacks, which have no end.

    (Every method is a stack)

    This happens mostly when you call from a method the same method (that is called recursive)


    Success
    Cor
    Wednesday, April 28, 2010 8:35 AM
  • thanks for the explaination

    i should write the 1000lines like

    .keyarr(0) = Chr(35) & Chr(54) & Chr(158) & Chr(43) & Chr(90) & Chr(143) & Chr(107) & Chr(172) & Chr(64) & Chr(150) & Chr(139) & Chr(199) & Chr(1) & Chr(26) & Chr(71) & Chr(51) & Chr(139) & Chr(187) & Chr(245) & Chr(212) & Chr(44) & Chr(232) & Chr(67) & Chr(93) & Chr(153) & Chr(206) & Chr(100) & Chr(212) & Chr(81) & Chr(159) & Chr(182) & Chr(47) & Chr(82) & Chr(11) & Chr(190) & Chr(147) & Chr(29) & Chr(57) & Chr(249) & Chr(106) & Chr(252) & Chr(32) & Chr(132) & Chr(180) & Chr(126) & Chr(152) & Chr(252) & Chr(231) & Chr(50) & Chr(243) & Chr(195) & Chr(185) & Chr(125) & Chr(129) & Chr(193) & Chr(50) & Chr(169) & Chr(202) & Chr(16) & Chr(81) & Chr(250) & Chr(231) & Chr(168) & Chr(158) & Chr(233) & Chr(195) & Chr(63) & Chr(24) & Chr(85) & Chr(161) & Chr(180) & Chr(169) & Chr(212) & Chr(190) & Chr(19) & Chr(180) & Chr(225) & Chr(122) & Chr(151) & Chr(209) & Chr(26) & Chr(164) & Chr(189) & Chr(83) & Chr(76) & Chr(166) & Chr(178) & Chr(180) & Chr(134) & Chr(253) & Chr(79) & Chr(123) & Chr(217) & Chr(156) & Chr(225) & Chr(164) & Chr(42) & Chr(1) & Chr(245) & Chr(78) & Chr(132) & Chr(58) & Chr(68) & Chr(138) & Chr(118) & Chr(236) & Chr(43) & Chr(35) & Chr(248) & Chr(103) & Chr(188) & Chr(57) & Chr(35) & Chr(218) & Chr(179) & Chr(129) & Chr(122) & Chr(249) & Chr(122) & Chr(17) & Chr(249) & Chr(201) & Chr(139) & Chr(120) & Chr(154) & Chr(19) & Chr(77) & Chr(20)

    in order to decrypt whatever password but i don't understand why sometime it works and some time no...

    may the stack are staying in memory ?

     

    thanks

     

    Wednesday, April 28, 2010 8:44 AM
  • Each thread gets 1MB of stack memory by default.  You can change the value in the constructor.
    Wednesday, April 28, 2010 9:53 AM
  • I get the idea that you somewhere calls your own method.

    That is almost the only method to let it not go out of scope.

    1000 lines like that is 128000 bytes.

    But it is as far as I can see even in the mainstack, not even in the method.


    Success
    Cor
    Wednesday, April 28, 2010 10:25 AM
  • I just understand that is the debbuger can't run the program :( , i can run after compilating

     

    thanks for all

     

    Sammy

    Wednesday, April 28, 2010 10:28 AM