Skip to content

Commit 6f6ccca

Browse files
paulirwinclaude
andcommitted
UnicodeUtil: Fix UTF8toUTF16WithFallback byte[] overload to use fallback path, #1024
The byte[]/offset/length overload was delegating to the throwing UTF8toUTF16 variant, so callers like BytesRef.Utf8ToStringWithFallback() would throw DecoderFallbackException on invalid UTF-8 instead of substituting U+FFFD as the method name and docs promise. Delegate to the ReadOnlySpan<byte> fallback overload and add a regression test covering the 4-arg signature with a non-zero offset. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 60f4026 commit 6f6ccca

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/Lucene.Net.Tests/Util/TestUnicodeUtil.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,25 @@ public void TestUTF8toUTF16WithFallback(byte[] utf8, string expected)
377377
Assert.AreEqual(expected, scratch.ToString());
378378
}
379379

380+
[Test]
381+
[LuceneNetSpecific]
382+
[TestCase(new byte[] { 0x63, 0x61, 0xc3 }, "ca\ufffd")] // ca�, start of 2-byte sequence
383+
[TestCase(new byte[] { 0x63, 0x61, 0xe3 }, "ca\ufffd")] // ca�, start of 3-byte sequence
384+
[TestCase(new byte[] { 0x63, 0x61, 0xf3 }, "ca\ufffd")] // ca�, start of 4-byte sequence
385+
[TestCase(new byte[] { 0x63, 0x61, 0xc3, 0xb1, 0x6f, 0x6e }, "cañon")]
386+
public void TestUTF8toUTF16WithFallback_ByteArrayOverload(byte[] utf8, string expected)
387+
{
388+
// Regression: the 4-arg byte[] overload must delegate to the fallback implementation,
389+
// not the throwing one. Exercise it via a non-zero offset to confirm the arguments flow through.
390+
var scratch = new CharsRef();
391+
var padded = new byte[utf8.Length + 2];
392+
Array.Copy(utf8, 0, padded, 2, utf8.Length);
393+
394+
UnicodeUtil.UTF8toUTF16WithFallback(padded, 2, utf8.Length, scratch);
395+
396+
Assert.AreEqual(expected, scratch.ToString());
397+
}
398+
380399
[Test]
381400
[LuceneNetSpecific]
382401
[Repeat(100)]

src/Lucene.Net/Util/UnicodeUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ public static void UTF8toUTF16(ReadOnlySpan<byte> utf8, CharsRef chars)
10651065
// TODO: broken if chars.offset != 0
10661066
public static void UTF8toUTF16WithFallback(byte[] utf8, int offset, int length, CharsRef chars)
10671067
{
1068-
UTF8toUTF16(utf8.AsSpan(offset, length), chars);
1068+
UTF8toUTF16WithFallback(utf8.AsSpan(offset, length), chars);
10691069
}
10701070

10711071
/// <summary>

0 commit comments

Comments
 (0)