From e587d564f81f7cef23336fe48f0a996077eff96a Mon Sep 17 00:00:00 2001 From: Kaz Wesley Date: Fri, 20 Sep 2024 06:23:52 -0700 Subject: [PATCH] Improve backend error handling (#11136) - Fix debug logging for #11088 case--attempt to create an exception that is its own cause fails. - In case the parser is used after closing, throw an `IllegalStateException` instead of UB. (This case is not known to occur and doesn't seem to be behind the #11121, but we should handle it more safely if it does.) --- .../java/org/enso/syntax2/Parser.java | 18 ++++++++++++++---- .../java/org/enso/base/polyglot/EnsoMeta.java | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/rust/parser/generate-java/java/org/enso/syntax2/Parser.java b/lib/rust/parser/generate-java/java/org/enso/syntax2/Parser.java index 2c375ee8408..91731cdde52 100644 --- a/lib/rust/parser/generate-java/java/org/enso/syntax2/Parser.java +++ b/lib/rust/parser/generate-java/java/org/enso/syntax2/Parser.java @@ -75,10 +75,10 @@ public final class Parser implements AutoCloseable { return false; } - private long state; + private long stateUnlessClosed; private Parser(long stateIn) { - state = stateIn; + stateUnlessClosed = stateIn; } private static native long allocState(); @@ -115,7 +115,16 @@ public final class Parser implements AutoCloseable { return isIdentOrOperator(inputBuf); } + private long getState() { + if (stateUnlessClosed != 0) { + return stateUnlessClosed; + } else { + throw new IllegalStateException("Parser used after close()"); + } + } + public ByteBuffer parseInputLazy(CharSequence input) { + var state = getState(); byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8); ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length); inputBuf.put(inputBytes); @@ -123,6 +132,7 @@ public final class Parser implements AutoCloseable { } public Tree parse(CharSequence input) { + var state = getState(); byte[] inputBytes = input.toString().getBytes(StandardCharsets.UTF_8); ByteBuffer inputBuf = ByteBuffer.allocateDirect(inputBytes.length); inputBuf.put(inputBytes); @@ -140,7 +150,7 @@ public final class Parser implements AutoCloseable { @Override public void close() { - freeState(state); - state = 0; + freeState(stateUnlessClosed); + stateUnlessClosed = 0; } } diff --git a/std-bits/base/src/main/java/org/enso/base/polyglot/EnsoMeta.java b/std-bits/base/src/main/java/org/enso/base/polyglot/EnsoMeta.java index d3763348173..6749092ae63 100644 --- a/std-bits/base/src/main/java/org/enso/base/polyglot/EnsoMeta.java +++ b/std-bits/base/src/main/java/org/enso/base/polyglot/EnsoMeta.java @@ -18,7 +18,7 @@ public final class EnsoMeta { var ex = new NullPointerException( "Cannot get type for " + moduleName + " type: " + typeName + " at " + module); - ex.initCause(ex); + ex.initCause(e); throw ex; } }