Nim
Nim copied to clipboard
No error checking on fclose
It seems that there is no error checking when closing a file. In lib/system/io.nim:309 the result of c_fclose(f) is discarded. However, the C stdlib uses buffering and effectively writes small files on fclose. For example it only discovers that there is no space left on device at that point. The error is typically propagated as the result of fclose.
Example
Small file
writeFile("/dev/full", "hello\n")
Long file
var
c = newString(4096)
for i in 0 .. 4095:
c[i] = 'x'
writeFile("/dev/full", c)
Current Output
nothing for small file
Error: unhandled exception: errno: 28 `No space left on device` [IOError]
for long file
Expected Output
Error: unhandled exception: errno: 28 `No space left on device` [IOError]
for both.
Possible Solution
Raise exception when result of fclose is not 0. Note that ferror does not seem to be updated by fclose, which make checkErr useless in that context.
Additional Information
Note: this was discovered after reading https://blog.sunfishcode.online/bugs-in-hello-world/
$ Nim Compiler Version 1.6.4 [Linux: amd64]
Compiled at 2022-02-09
Copyright (c) 2006-2021 by Andreas Rumpf
git hash: 7994556f3804c217035c44b453a3feec64405758
active boot switches: -d:release