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

fix #19600; No error checking on fclose #24468

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ rounding guarantees (via the
avoid conflicts with `system.default`, so named argument usage for this
parameter like `getOrDefault(..., default = ...)` will have to be changed.

- With `-d:nimPreviewCheckedClose`, the `close` function in the `std/syncio` module now raises an IO exception in case of an error.

## Standard library additions and changes

[//]: # "Additions:"
Expand Down
1 change: 1 addition & 0 deletions compiler/nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ define:nimPreviewCstringConversion
define:nimPreviewProcConversion
define:nimPreviewRangeDefault
define:nimPreviewNonVarDestructor
define:nimPreviewCheckedClose
threads:off

#import:"$projectpath/testability"
Expand Down
23 changes: 19 additions & 4 deletions lib/std/syncio.nim
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,26 @@ elif defined(windows):
const
BufSize = 4000

proc close*(f: File) {.tags: [], gcsafe, sideEffect.} =
template closeIgnoreError(f: File) =
## Closes the file.
if not f.isNil:
discard c_fclose(f)


when defined(nimPreviewCheckedClose):
proc close*(f: File) {.tags: [], gcsafe, sideEffect.} =
## Closes the file.
##
## Raises an IO exception in case of an error.
if not f.isNil:
let x = c_fclose(f)
if x < 0:
checkErr(f)
else:
proc close*(f: File) {.tags: [], gcsafe, sideEffect.} =
## Closes the file.
closeIgnoreError(f)

proc readChar*(f: File): char {.tags: [ReadIOEffect].} =
## Reads a single character from the stream `f`. Should not be used in
## performance sensitive code.
Expand Down Expand Up @@ -708,12 +723,12 @@ proc open*(f: var File, filename: string,
# be opened.
var res {.noinit.}: Stat
if c_fstat(getFileHandle(f2), res) >= 0'i32 and modeIsDir(res.st_mode):
close(f2)
closeIgnoreError(f2)
return false
when not defined(nimInheritHandles) and declared(setInheritable) and
NoInheritFlag.len == 0:
if not setInheritable(getOsFileHandle(f2), false):
close(f2)
closeIgnoreError(f2)
return false

result = true
Expand All @@ -736,7 +751,7 @@ proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
when not defined(nimInheritHandles) and declared(setInheritable) and
NoInheritFlag.len == 0:
if not setInheritable(getOsFileHandle(f), false):
close(f)
closeIgnoreError(f)
return false
result = true

Expand Down
1 change: 1 addition & 0 deletions tests/config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ switch("define", "nimPreviewJsonutilsHoleyEnum")
switch("define", "nimPreviewHashRef")
switch("define", "nimPreviewRangeDefault")
switch("define", "nimPreviewNonVarDestructor")
switch("define", "nimPreviewCheckedClose")

switch("warningAserror", "UnnamedBreak")
when not defined(testsConciseTypeMismatch):
Expand Down