Why did this code started hanging in Mac debugger after latest update

This code worked in the debugger on my iMac ever since 2019R1 stopping at the second conditional Break statement and finishing to the Return after that if I let it go that far. It hangs now after the latest update, apparently never reaching either of the two conditional “Break” statements. Why is this happening now?

If MaxUInt32 = 0 Then
  Initialize
End If
Var iril As UInteger = iri.Length
Var result As Text = ""
Var handledCount, basicCount, bias, n, m, q, k, t, delta As UInt32
Var outputCount As UInteger = 0
n = INITIAL_N
bias = INITIAL_BIAS
delta = 0
For Each cp As UInt32 In iri.Codepoints
  If (cp > &h10FFFF) Or (cp >= &hD800 And _
    cp <= &hD8FF) Then
    #If DebugBuild
      Break
    #Else
      Raise New IllegalInputException
    #EndIf
  Elseif IsBasicCodepoint(cp, False) Then
    result = result + Text.FromUnicodeCodepoint(cp)
    outputCount = outputCount + 1
  End If
Next
handledCount = outputCount
basicCount = outputCount
If basicCount > 0 Then
  result = result + Text.FromUnicodeCodepoint(DELIMITER)
End If
While handledCount < iril 
  m = MaxUInt32
  For Each cp As UInt32 In iri.Codepoints
    If cp >= n And cp < m Then
      m = cp
    End If
  Next
  If (m - n) > ((MaxUInt32 - delta) \ (handledCount + 1)) Then
    #If DebugBuild
      Break
    #Else
      Raise New OverflowException
    #EndIf
    delta = delta + ((m - n) * (handledCount + 1))
    n = m
    For Each cp As UInt32 In iri.Codepoints
      If cp < n And delta + 1 > MaxUInt32 Then
        #If DebugBuild 
          Break
        #Else
          Raise New OverflowException
        #EndIf
      End If
      If cp = n Then
        k = BASE
        q = delta
        While True
          If k <= bias Then
            t = T_MINIMUM
          Elseif k >= (bias + T_MAXIMUM) Then
            t = T_MAXIMUM
          Else
            t = k - bias
          End If
          If q < t Then
            Exit While
          End If
          result = result + EncodeDigit(t + (q - t) Mod (BASE - t), _
          lowercaseResult)
          #If DebugBuild
            If result.Length = 4 Then Break
          #EndIf
          q = (q - t) \ (BASE - t)
          k = k + BASE
        Wend
        result = result + EncodeDigit(q, lowercaseResult)
        #If DebugBuild
          If result.Length = 4 Then Break
        #EndIf
        bias = AdaptForBias(delta, handledCount + 1, _
        (handledCount = basicCount))
        delta = 0
        handledCount = handledCount + 1
      End If
    Next
    delta = delta + 1
    n = n + 1
  End If
Wend
Return result

Recent versions of Xojo fixed some longstanding bugs with 32 / 64 bit math not working as expected. Perhaps you were relying on one of those bugs by accident?

See https://forum.xojo.com/t/xojo-math-badly-broken-32bit-problems-in-a-64bit-environment/

Edit: specifically, I’m referring to MaxUInt32 which I see in your code.

If you put the following just above #If DebugBuild do you ever see it appear in the message pane, i.e. is the code actually getting in there?

system.debuglog("test")

If you follow along in the debugger step by step then WHERE does it hang? For all we know it might be in the Initialize method you are calling . Seems the logical thing to do to me …

1 Like

I assume you used to run this in 32bit as doing so exhibits the behaviour you explained.

If so, your integer divisions ( \ ) return Int64 now where as they used to be Int32’s which you relied on overflowing and being set to -1 which wont happen now as you have more space to store the value.

One of the fixes would be:

If (m - n) > CType(((MaxUInt32 - delta) \ (handledCount + 1)), Int32) Then

I’ve not looked around the rest of the code to check for other problems, I’ll let you do that :smiley:

I need sleep so this explanation might not be 100% accurate.

Found the problem. The line

delta = delta + ((m - n) * (handledCount + 1))

originally had an ELSE above it that somehow disappeared.