naotsugu opened a new issue, #395:
URL: https://github.com/apache/maven-wrapper/issues/395

   ### Affected version
   
   3.3.3
   
   ### Bug description
   
   ## Description
   
   In `mvnw.cmd`, the logic introduced by PR #143 to handle symlinks attempts 
to access an array index on the `Target` property without checking if it is 
null.
   
   `mvnw.cmd` fails immediately upon execution with a "Cannot index into a null 
array" error. This issue specifically occurs when the script runs under the 
**Local System account** in a **32-bit process** (typical for Jenkins Agents 
running as a Windows Service), where the default `.m2` home resolves to the 
System Profile directory.
   
   ## Error Log
   
   
   ```Plaintext
   icm : Cannot index into a null array
   At line:1 char:81
   + ... 'mvnw.cmd'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw 
...
   +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       + CategoryInfo          : InvalidOperation: (:) [Invoke-Command], 
RuntimeException
       + FullyQualifiedErrorId : 
NullArray,Microsoft.PowerShell.Commands.InvokeCommandCommand
   ```
   
   ## Environment
   
   - **OS:** Windows Server 2022    
   - **Process Architecture:** 32-bit (x86)    
   - **User:** `NT AUTHORITY\SYSTEM`    
   - **Target Path:** `C:\WINDOWS\system32\config\systemprofile\.m2`    
   - **PowerShell:** 5.1    
   
   ## Root Cause Analysis
   
   The script contains the following logic to handle symlinks:
   
   ```PowerShell
   if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) { ... }
   ```
   
   We verified the behavior of the `Target` property on this specific 
environment:
   
   1. **Standard Paths (e.g., Workspace drive):** The `Target` property returns 
an **empty collection**. Accessing `[0]` returns `$null` safely (in PowerShell 
5.1).    
   2. **System Profile Path (`.m2`):** When accessing the `.m2` directory in 
the System Profile from a 32-bit process, the `Target` property returns 
**strictly `$null`**.    
   
   Since `mvnw.cmd` defaults `MAVEN_M2_PATH` to the user home (System Profile), 
the script attempts to access index `[0]` on `$null`, causing a fatal 
`RuntimeException`.
   
   ## Verification Code & Result
   
   We ran the following diagnostic inside the failing Jenkins environment, 
specifically targeting the `.m2` directory:
   
   ```PowerShell
   $basePath = $env:USERPROFILE
   $m2Path = Join-Path $basePath ".m2"
   
   # Ensure .m2 exists for testing
   if (-not (Test-Path -Path $m2Path)) { New-Item -Path $m2Path -ItemType 
Directory | Out-Null }
   
   $prop = (Get-Item $m2Path).Target
   
   Write-Host "Testing Path: $m2Path"
   Write-Host "Is Null? : " ($prop -eq $null)
   try { $null = $prop[0]; Write-Host "Safe" } catch { Write-Host "CRASH" }
   ```
   
   **Output:**
   
   ```
   Testing Path: C:\WINDOWS\system32\config\systemprofile\.m2
   Is Null? : True
   CRASH
   ```
   
   ## Proposed Fix
   
   The script must check if the `Target` property is not null before attempting 
to index it.
   
   **Suggested Change:**
   
   
   ```PowerShell
   $m2Item = Get-Item $MAVEN_M2_PATH
   
   # Safe check: Ensure property exists and is not null before indexing
   if ($m2Item.PSObject.Properties['Target'] -ne $null -and $m2Item.Target -ne 
$null) {
       $MAVEN_WRAPPER_DISTS = $m2Item.Target + "/wrapper/dists"
   } else {
       $MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
   }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to