Skip to content

Bug: Runtime crash "Variable used without being declared" in compiled mode under strict scoping (MustDeclareVars) #129

Description

@Blowcake

Hi @ioa747 and @mlipok,

i want to report a small scoping issue i encountered when testing the library (version 2.2.2-alpha) in compiled mode (@Compiled = 1) with strict variable declaration enabled (Opt("MustDeclareVars", 1)).


The Problem

When running a compiled binary, the global variable $_g_bNetWebView2_DebugDev is automatically set to True (via Global $_g_bNetWebView2_DebugDev = (@Compiled = 1)).

During default navigation (e.g. when $sExpectedTitle is empty ""), the function _NetWebView2_LoadWait is called.

Inside _NetWebView2_LoadWait (around lines 636~652), the code checks if the expected navigation events are reached:

ElseIf $iLastMessage >= $iWaitMessage Then ; RULE 6: checking requested events messages
    If $iWaitMessage = $NETWEBVIEW2_MESSAGE__TITLE_CHANGED And $sExpectedTitle Then ; RULE 7: checking Expected document title
        Local $sCurrentTitle = $oWebV2M.GetDocumentTitle()
        Local $bTitleCheck = (StringRegExp($sCurrentTitle, $sExpectedTitle, $STR_REGEXPMATCH) = 1)
        Local $s_DEV_Info = "! IFNC: DEV: TEST LOAD WAIT: Prefix:: " & $s_Prefix & " TitleCheck=" & $bTitleCheck & " LastMessage=" & $iLastMessage & " CurrentTitle=" & $sCurrentTitle
        If $_g_bNetWebView2_DebugDev Then ConsoleWrite($s_DEV_Info & " #SLN=" & @ScriptLineNumber & @CRLF)
        If $bTitleCheck Then
            $MSG = " TitleCheck=" & $bTitleCheck & " #SLN=" & @ScriptLineNumber
            $RET = True
            ExitLoop
        EndIf
    Else
        If $_g_bNetWebView2_DebugDev Then ConsoleWrite($s_DEV_Info & " #SLN=" & @ScriptLineNumber & @CRLF)
        $MSG = " LastMessage=" & $iLastMessage & " #SLN=" & @ScriptLineNumber
        $RET = True
        ExitLoop
    EndIf

Because $sExpectedTitle is empty, the If block is skipped, and execution goes to the Else block.
However, in the Else block, the code tries to reference $s_DEV_Info:

If $_g_bNetWebView2_DebugDev Then ConsoleWrite($s_DEV_Info & " #SLN=" & @ScriptLineNumber & @CRLF)

Since $s_DEV_Info is declared with Local inside the If block, it is never declared or initialized at runtime when entering the Else block. Because $_g_bNetWebView2_DebugDev is True when compiled, AutoIt evaluates this line and crashes immediately with:

Error: Variable used without being declared.

(Note: The issue doesn't show up in uncompiled mode because $_g_bNetWebView2_DebugDev is False, which skips the ConsoleWrite call at runtime).


Proposed Fix

We can easily fix this by changing the Else block's ConsoleWrite to not reference the undeclared $s_DEV_Info (or by declaring it outside the If/Else blocks).

For example, using an inline string in the Else block:

 	Else
-		If $_g_bNetWebView2_DebugDev Then ConsoleWrite($s_DEV_Info & " #SLN=" & @ScriptLineNumber & @CRLF)
+		If $_g_bNetWebView2_DebugDev Then ConsoleWrite("! IFNC: DEV: TEST LOAD WAIT: Prefix:: " & $s_Prefix & " LastMessage=" & $iLastMessage & " #SLN=" & @ScriptLineNumber & @CRLF)
 		$MSG = " LastMessage=" & $iLastMessage & " #SLN=" & @ScriptLineNumber
 		$RET = True
 		ExitLoop
 	EndIf

Thank you again for maintaining this library, and let me know if you need any further testing/feedback on this or the throttling changes!

Best regards
Harald Frank

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions