Skip to content

Commit 3fb261f

Browse files
Address PR review feedback
1 parent 58c5980 commit 3fb261f

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/functions/private/ConvertTo-LuaTable.ps1

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,16 @@
116116
if ($CurrentDepth -ge $MaxDepth) {
117117
Write-Warning "Depth limit ($MaxDepth) exceeded. Serializing remaining object as string."
118118
$str = $InputObject.ToString() `
119-
-replace '\\', '\\\\' `
120-
-replace '"', '\"'
119+
-replace '\\', '\\' `
120+
-replace '"', '\"' `
121+
-replace "`0", '\0' `
122+
-replace "`a", '\a' `
123+
-replace "`b", '\b' `
124+
-replace "`f", '\f' `
125+
-replace "`n", '\n' `
126+
-replace "`r", '\r' `
127+
-replace "`t", '\t' `
128+
-replace "`v", '\v'
121129
return "`"$str`""
122130
}
123131

@@ -195,7 +203,17 @@
195203
}
196204

197205
# Fallback: convert to string
198-
$escaped = ($InputObject.ToString()) -replace '\\', '\\\\' -replace '"', '\"'
206+
$escaped = $InputObject.ToString() `
207+
-replace '\\', '\\' `
208+
-replace '"', '\"' `
209+
-replace "`0", '\0' `
210+
-replace "`a", '\a' `
211+
-replace "`b", '\b' `
212+
-replace "`f", '\f' `
213+
-replace "`n", '\n' `
214+
-replace "`r", '\r' `
215+
-replace "`t", '\t' `
216+
-replace "`v", '\v'
199217
return "`"$escaped`""
200218
}
201219

tests/Lua.Tests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,15 @@ Describe 'ConvertTo-Lua' {
922922
$warnings = @($result | Where-Object { $_ -is [System.Management.Automation.WarningRecord] })
923923
$warnings.Count | Should -BeGreaterThan 0
924924
}
925+
926+
It 'Escapes control characters in depth-limit fallback string' {
927+
$inner = [System.Text.StringBuilder]::new("line1`nline2`ttab")
928+
$obj = [ordered]@{ x = $inner }
929+
$result = ConvertTo-Lua -InputObject $obj -Depth 1 -Compress 3>&1
930+
$output = @($result | Where-Object { $_ -is [string] })
931+
$output[-1] | Should -BeLike '*\n*'
932+
$output[-1] | Should -BeLike '*\t*'
933+
}
925934
}
926935

927936
Context 'AsArray' {
@@ -1001,6 +1010,12 @@ Describe 'ConvertTo-Lua' {
10011010
$result | Should -Not -Be '{}'
10021011
$result | Should -Match '^".*"$'
10031012
}
1013+
1014+
It 'Escapes control characters in generic fallback string' {
1015+
$sb = [System.Text.StringBuilder]::new("line1`nline2`ttab")
1016+
$result = ConvertTo-Lua -InputObject $sb -Compress
1017+
$result | Should -Be '"line1\nline2\ttab"'
1018+
}
10041019
}
10051020
}
10061021

0 commit comments

Comments
 (0)