From ae043136a1f29331f806e01db72e88aa99bd9885 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 23 Feb 2026 04:30:36 -0500 Subject: [PATCH] fix: address lexer code review findings (newline handling, emit cleanup, null guard) --- .../Configuration/NatsConfLexer.cs | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/NATS.Server/Configuration/NatsConfLexer.cs b/src/NATS.Server/Configuration/NatsConfLexer.cs index 8f878fb..2a17cab 100644 --- a/src/NATS.Server/Configuration/NatsConfLexer.cs +++ b/src/NATS.Server/Configuration/NatsConfLexer.cs @@ -61,6 +61,7 @@ public sealed class NatsConfLexer public static IReadOnlyList Tokenize(string input) { + ArgumentNullException.ThrowIfNull(input); var lx = new NatsConfLexer(input); LexState? state = LexTop; while (state is not null) @@ -85,7 +86,17 @@ public sealed class NatsConfLexer private void Emit(TokenType type) { - var val = string.Concat(_stringParts) + _input[_start.._pos]; + string val; + if (_stringParts.Count > 0) + { + val = string.Concat(_stringParts) + _input[_start.._pos]; + _stringParts.Clear(); + } + else + { + val = _input[_start.._pos]; + } + var pos = _pos - _ilstart - val.Length; _items.Add(new Token(type, val, _line, pos)); _start = _pos; @@ -660,7 +671,7 @@ public sealed class NatsConfLexer private static LexState? LexKeyEnd(NatsConfLexer lx) { var r = lx.Next(); - if (char.IsWhiteSpace(r) && !IsNL(r)) + if (char.IsWhiteSpace(r)) { return LexSkip(lx, LexKeyEnd); } @@ -961,7 +972,7 @@ public sealed class NatsConfLexer private static LexState? LexMapKeyEnd(NatsConfLexer lx) { var r = lx.Next(); - if (char.IsWhiteSpace(r) && !IsNL(r)) + if (char.IsWhiteSpace(r)) { return LexSkip(lx, LexMapKeyEnd); } @@ -1239,13 +1250,13 @@ public sealed class NatsConfLexer private static LexState? LexStringBinary(NatsConfLexer lx) { var r1 = lx.Next(); - if (IsNL(r1)) + if (IsNL(r1) || r1 == Eof) { return lx.Errorf("Expected two hexadecimal digits after '\\x', but hit end of line"); } var r2 = lx.Next(); - if (IsNL(r2)) + if (IsNL(r2) || r2 == Eof) { return lx.Errorf("Expected two hexadecimal digits after '\\x', but hit end of line"); }