1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 01:39:23 +01:00

Throw an exception when text formatter macros resolve recursively too many times

This commit is contained in:
Ell 2021-11-22 17:50:17 +01:00
parent 84e2544dc8
commit c9c9e566b1
3 changed files with 12 additions and 0 deletions

View file

@ -20,6 +20,7 @@ Improvements
- Exposed Camera's RoundPosition
- Exposed the epsilon value used by Camera
- Added Padding.Empty
- Throw an exception when text formatter macros resolve recursively too many times
### MLEM.Ui
Additions

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
@ -114,6 +115,7 @@ namespace MLEM.Formatting {
/// <returns>The final, recursively resolved string</returns>
public string ResolveMacros(string s) {
// resolve macros that resolve into macros
var rec = 0;
bool matched;
do {
matched = false;
@ -124,6 +126,9 @@ namespace MLEM.Formatting {
return macro.Value(this, m, macro.Key);
});
}
rec++;
if (rec >= 16)
throw new ArithmeticException($"A string resolved macros recursively too many times. Does it contain any conflicting macros?\n{s}");
} while (matched);
return s;
}

View file

@ -1,3 +1,4 @@
using System;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@ -107,6 +108,11 @@ namespace Tests {
const string strg = "This text uses a bunch of non-breaking~spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <testmacro> text</c>.";
const string goal = "This text uses a bunch of non-breaking\u00A0spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <c Blue>blue text</c>.";
Assert.AreEqual(this.formatter.ResolveMacros(strg), goal);
// test recursive macros
this.formatter.Macros.Add(new Regex("<rec1>"), (f, m, r) => "<rec2>");
this.formatter.Macros.Add(new Regex("<rec2>"), (f, m, r) => "<rec1>");
Assert.Throws<ArithmeticException>(() => this.formatter.ResolveMacros("Test <rec1> string"));
}
[Test]