Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No error checking on fclose #19600

Closed
benob opened this issue Mar 9, 2022 · 0 comments · Fixed by #24468
Closed

No error checking on fclose #19600

benob opened this issue Mar 9, 2022 · 0 comments · Fixed by #24468

Comments

@benob
Copy link
Contributor

benob commented Mar 9, 2022

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
bung87 added a commit to bung87/Nim that referenced this issue May 27, 2022
@Araq Araq closed this as completed in 04e4a5e Aug 29, 2022
ringabout added a commit that referenced this issue Sep 2, 2022
Araq pushed a commit that referenced this issue Sep 2, 2022
@ringabout ringabout reopened this Sep 3, 2022
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
* fix nim-lang#19600 No error checking on fclose

* add IOError to open
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
Araq pushed a commit that referenced this issue Nov 23, 2024
narimiran pushed a commit that referenced this issue Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants