iOS Question [B4X] BBCodeView - ParseAndDraw has no list NULL check

Alexander Stolte

Expert
Licensed User
Longtime User
Hello,

I have received a number of error messages from my users that are caused by the BBCodeView.
I have reverse engineered the B4XTextEngine.b4xlib to find out where the error is occurring.


I have the following error and stacktrace in the log file:
B4X:
Application Specific Information:
*** Terminating app due to uncaught exception '', reason: 'Object was not initialized (NSArray)'

Thread 0 Crashed:
0   CoreFoundation                     0x000000018ac4f21c 0x18ab35000 + 1155612 (<redacted> + 164)
1   libobjc.A.dylib                         0x00000001880e9abc 0x1880b8000 + 203452 (objc_exception_throw + 88)
2   CoreFoundation                      0x000000018acadea0 0x18ab35000 + 1543840 (<redacted> + 128)
3   MyAppName                           0x00000001023bd028 0x1020b0000 + 3199016 (-[B4IObjectWrapper object] + 116)
4   MyAppName                           0x00000001023cae00 0x1020b0000 + 3255808 (-[B4IList Size] + 32)
5   MyAppName                           0x00000001022dbc78 0x1020b0000 + 2276472 (-[b4i_bbcodeparser _createruns::] + 152)
6   MyAppName                           0x00000001022e5290 0x1020b0000 + 2314896 (-[b4i_bbcodeview _parseanddraw] + 1032)
7   MyAppName                           0x00000001022e5b50 0x1020b0000 + 2317136 (-[b4i_bbcodeview _settext:] + 88)
8   MyAppName                           0x00000001022927c0 0x1020b0000 + 1976256 (-[b4i_b4xp_notes _addnote:::] + 4308)

The ParseAndDraw function is called in the BBCodeView, there is the following code which is problematic:
B4X:
Public Sub ParseAndDraw
    ParseData.NeedToReparseWhenResize = False
    ParseData.Text = mText
    ParseData.URLs.Clear
    ParseData.Width = (mBase.Width - Padding.Left - Padding.Right)
    If RTL Then mTextEngine.RTLAware = True
    Dim pe As List = mTextEngine.TagParser.Parse(ParseData)
    sv.ScrollViewInnerPanel.RemoveAllViews
    If TouchPanel.IsInitialized Then
        sv.ScrollViewInnerPanel.AddView(TouchPanel, 0, 0, 0, 0)
    End If
    sv.ScrollViewInnerPanel.AddView(ForegroundImageView, 0, 0, 2dip, 2dip)
    If ExternalRuns.IsInitialized And ExternalRuns.Size > 0 Then
        Runs = ExternalRuns
    Else
        Runs = mTextEngine.TagParser.CreateRuns(pe, ParseData)
    End If
    
    Redraw
End Sub
in the 1st highlighted code you can see that the TagParser is used, in the parse function I found a line that can return “null” if something is wrong:
Parse function from BBCodeParser.bas:
Dim t As BBCodeTagNode = ParseTag(tag)
            If AllowedTags.Contains(t.Tag) = False Then
                Error("Invalid tag: " & tag)
                Return Null
            End If
This means that if this case occurs, the list is filled with “null” and this in turn is passed to the TagParser.CreateRuns, where the crash occurs because the list is null.

This must be intercepted in the Sub ParseAndDraw function, it must be checked whether the list is null to avoid this crash.

Thank you
Alex
 
Top