From cb1fc73f3519fed5f3a8fbfa90afc9a96d5f5f5c Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Thu, 13 Jun 2024 13:33:17 +0200 Subject: [PATCH] Do not close `nil` stream when it failed to open (#87) When stream fails to open during loading of TOML file, `stream` remains set to `nil`. The `finally` block then triggers SIGSEGV when it tries to close the stream that was never fully opened. Only install the `finally` block once `stream` was initialized to fix this. --- toml_serialization.nim | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/toml_serialization.nim b/toml_serialization.nim index f5b7f43..3f7bbc1 100644 --- a/toml_serialization.nim +++ b/toml_serialization.nim @@ -139,15 +139,14 @@ template tomlLoadImpl*(filename: string, RecordType: distinct type, key: string, tomlCase: TomlCase, params: varargs[untyped]): auto = - mixin init, ReaderType, readValue var stream: InputStream + when nimvm: + let input = staticRead(filename) + stream = VMInputStream(pos: 0, data: toVMString(input)) + else: + stream = memFileInput(filename) try: - when nimvm: - let input = staticRead(filename) - stream = VMInputStream(pos: 0, data: toVMString(input)) - else: - stream = memFileInput(filename) var reader = unpackArgs(init, [TomlReader, stream, params]) when RecordType is (seq or array) and uTypeIsRecord(RecordType): reader.readTableArray(RecordType, key, tomlCase)