Skip to content

Commit 38dee63

Browse files
committed
Fix deadloop in module version replacement
1 parent 404bb4f commit 38dee63

2 files changed

Lines changed: 15 additions & 24 deletions

File tree

Modules/DSCParser/DSCParser.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Microsoft365DSC Team
55
#
6-
# Generated on: 2026/04/14
6+
# Generated on: 2026/04/15
77
#
88

99
@{
@@ -12,7 +12,7 @@
1212
RootModule = 'DSCParser.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '3.0.0.3'
15+
ModuleVersion = '3.0.0.4'
1616

1717
# ID used to uniquely identify this module
1818
GUID = 'e168239a-233d-468d-9025-d6dfc0e4e2b6'

src/DSCParser.CSharp/DscParser.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -196,33 +196,24 @@ public static string ConvertFromDscObject(IEnumerable<Hashtable> dscResources, i
196196

197197
private static string RemoveModuleVersionInfo(string content, List<string>? uniqueModules = null)
198198
{
199-
int start = 0;
200-
do
199+
if (uniqueModules is null || uniqueModules.Count == 0)
201200
{
202-
start = content.IndexOf("import-dscresource", start, StringComparison.CurrentCultureIgnoreCase);
203-
if (start >= 0)
201+
return content;
202+
}
203+
204+
string pattern = @"(import-dscresource\b[^\n]*?)\s+-moduleversion\s+(?:""[^""]*""|'[^']*'|\S+)([^\n]*)";
205+
return Regex.Replace(content, pattern, match =>
206+
{
207+
string fullLine = match.Value;
208+
foreach (string module in uniqueModules)
204209
{
205-
int end = content.IndexOf("\n", start);
206-
if (end > start)
210+
if (fullLine.IndexOf(module, StringComparison.CurrentCultureIgnoreCase) >= 0)
207211
{
208-
foreach (string module in uniqueModules ?? [])
209-
{
210-
int moduleIndex = content.IndexOf(module, start, StringComparison.CurrentCultureIgnoreCase);
211-
if (moduleIndex >= 0 && moduleIndex < end)
212-
{
213-
start = content.IndexOf("-moduleversion", start, StringComparison.CurrentCultureIgnoreCase);
214-
if (start >= 0 && start < end)
215-
{
216-
content = content.Remove(start, end - start);
217-
break;
218-
}
219-
}
220-
}
221-
start += 1;
212+
return match.Groups[1].Value + match.Groups[2].Value;
222213
}
223214
}
224-
} while (start >= 0);
225-
return content;
215+
return fullLine;
216+
}, RegexOptions.IgnoreCase);
226217
}
227218

228219
private static void InitializeCimClasses()

0 commit comments

Comments
 (0)