Skip to content

Commit

Permalink
fix #19600; No error checking on fclose (#24468)
Browse files Browse the repository at this point in the history
fix #19600
  • Loading branch information
ringabout authored Nov 23, 2024
1 parent 96043bd commit 555191a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
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

0 comments on commit 555191a

Please sign in to comment.