Fix decoding of pkl-binary into Java Pattern (#1895)

Patterns that originate from the language runtime will add
`Pattern.UNICODE_CHARACTER_CLASS | Pattern.UNICODE_CASE` as implicit
regex flags, so we need to do the same on the binary decoding side.
This commit is contained in:
Daniel Chao authored and GitHub committed 2026-10-05 09:09:30 -07:00
1 parent ba25f4820c
commit dbcecd9677
2 files changed
+14 -1

No files matched your search

@@ -31,6 +31,7 @@ import org.msgpack.core.MessageUnpacker;
import org.pkl.core.DataSizeUnit;
import org.pkl.core.DurationUnit;
import org.pkl.core.Pair;
import org.pkl.core.runtime.VmUtils;
import org.pkl.core.util.LateInit;
/**
@@ -317,10 +318,11 @@ public abstract class AbstractPklBinaryDecoder {
return result;
}
@SuppressWarnings("MagicConstant")
private Object decodeRegex(int len) throws IOException {
assertLength(PklBinaryCode.REGEX, len, 1);
currPath.push("'regex");
var result = doDecodeRegex(Pattern.compile(unpacker.unpackString()));
var result = doDecodeRegex(Pattern.compile(unpacker.unpackString(), VmUtils.REGEX_FLAGS));
unpacker.skipValue(len - 2);
currPath.pop();
return result;
@@ -20,6 +20,7 @@ import java.util.regex.Pattern
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.msgpack.core.MessagePack
import org.pkl.core.util.pklbinary.PklBinaryCode
class PklBinaryDecoderTest {
@@ -261,4 +262,14 @@ class PklBinaryDecoderTest {
"Unable to decode set of length 65536, exceeded maximum collection size",
)
}
@Test
fun `decode regex preserves Pkl's implicit flags`() {
val packer = MessagePack.newDefaultBufferPacker()
packer.packArrayHeader(2)
packer.packByte(0x0B)
packer.packString("(?i)é")
val decoded = PklBinaryDecoder.decode(packer.toByteArray()) as Pattern
assertThat(decoded.matcher("É")).matches()
}
}