io-prepl
使用 valf
序列化 eval 结果,但如果序列化值在打印时抛出异常,则保留异常原样。
复现
# terminal 1
clj -X clojure.core.server/start-server \
:name '"io-prepl"' \
:port 7777 \
:accept clojure.core.server/io-prepl \
:server-daemon false
# terminal 2
nc localhost 7777
(reify Object (toString [_] (/ 1 0)))
{:tag :ret, :val {:via [{:type java.lang.ArithmeticException, :message "Divide by zero", :at [clojure.lang.Numbers divide "Numbers.java" 188]}], :trace [[clojure.lang.Numbers divide "Numbers.java" 188] [clojure.lang.Numbers divide "Numbers.java" 3901] [user$eval220$reify__221 toString "NO_SOURCE_FILE" 3] [clojure.core$str invokeStatic "core.clj" 555] [clojure.core$print_object invokeStatic "core_print.clj" 117] [clojure.core$fn__7297 invokeStatic "core_print.clj" 120] [clojure.core$fn__7297 invoke "core_print.clj" 120] [clojure.lang.MultiFn invoke "MultiFn.java" 234] [clojure.core$pr_on invokeStatic "core.clj" 3662] [clojure.core$pr invokeStatic "core.clj" 3665] [clojure.core$pr invoke "core.clj" 3665] [clojure.lang.AFn applyToHelper "AFn.java" 154] [clojure.lang.RestFn applyTo "RestFn.java" 132] [clojure.core$apply invokeStatic "core.clj" 667] [clojure.core$pr_str invokeStatic "core.clj" 4724] [clojure.core$pr_str doInvoke "core.clj" 4724] [clojure.lang.RestFn invoke "RestFn.java" 408] [clojure.core.server$io_prepl$fn__8978$fn__8979 invoke "server.clj" 289] [clojure.core.server$io_prepl$fn__8978 invoke "server.clj" 288] [clojure.core.server$prepl$fn__8964 invoke "server.clj" 239] [clojure.core.server$prepl invokeStatic "server.clj" 228] [clojure.core.server$prepl doInvoke "server.clj" 191] [clojure.lang.RestFn invoke "RestFn.java" 425] [clojure.core.server$io_prepl invokeStatic "server.clj" 283] [clojure.core.server$io_prepl doInvoke "server.clj" 272] [clojure.lang.RestFn invoke "RestFn.java" 397] [clojure.lang.AFn applyToHelper "AFn.java" 152] [clojure.lang.RestFn applyTo "RestFn.java" 132] [clojure.lang.Var applyTo "Var.java" 705] [clojure.core$apply invokeStatic "core.clj" 667] [clojure.core.server$accept_connection invokeStatic "server.clj" 73] [clojure.core.server$start_server$fn__8902$fn__8903$fn__8905 invoke "server.clj" 117] [clojure.lang.AFn run "AFn.java" 22] [java.lang.Thread run "Thread.java" 834]], :cause "Divide by zero", :phase :print-eval-result}, :ns "user", :ms 3, :form "(reify Object (toString [_] (/ 1 0)))", :exception true}
这会给其他连接到 io-prepl 的预处理器(如 remote-prepl
)造成问题,因为这些预处理器总是使用其 valf 进行反序列化。
# terminal 3
clj
Clojure 1.10.3
user=> ((requiring-resolve 'clojure.core.server/remote-prepl) "localhost" 7777 *in* prn)
(reify Object (toString [_] (/ 1 0)))
{:tag :ret, :val {:via [{:type java.lang.ClassCastException, :message "class clojure.lang.PersistentArrayMap cannot be cast to class java.lang.String (clojure.lang.PersistentArrayMap is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')", :at [clojure.core$read_string invokeStatic "core.clj" 3793]}], :trace [[clojure.core$read_string invokeStatic "core.clj" 3793] [clojure.core$read_string invoke "core.clj" 3793] [clojure.core.server$remote_prepl$fn__8992$fn__8994 invoke "server.clj" 322] [clojure.core.server$remote_prepl$fn__8992 invoke "server.clj" 321] [clojure.lang.AFn run "AFn.java" 22] [java.lang.Thread run "Thread.java" 834]], :cause "class clojure.lang.PersistentArrayMap cannot be cast to class java.lang.String (clojure.lang.PersistentArrayMap is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')", :phase :read-eval-result}, :ns "user", :ms 1, :form "(reify Object (toString [_] (/ 1 0)))", :exception true}
在这种情况下,原始异常(打印时分母为零)被另一个异常(通过尝试将映射 read-string
化为类引起的类转换异常)所覆盖。