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.
This commit is contained in:
Etan Kissling 2024-06-13 13:33:17 +02:00 committed by GitHub
parent abcaafedda
commit cb1fc73f35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 6 deletions

View File

@ -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
try:
when nimvm:
let input = staticRead(filename)
stream = VMInputStream(pos: 0, data: toVMString(input))
else:
stream = memFileInput(filename)
try:
var reader = unpackArgs(init, [TomlReader, stream, params])
when RecordType is (seq or array) and uTypeIsRecord(RecordType):
reader.readTableArray(RecordType, key, tomlCase)