Skip to content

Commit

Permalink
Fix OSError errorCode field is not assigned a value (#22954)
Browse files Browse the repository at this point in the history
In this PR, the following changes were made:
1. Replaced `raise newException(OSError, osErrorMsg(errno))` in batches
with `raiseOSError(errcode)`.
2. Replaced `newException(OSError, osErrorMsg(errno))` in batches with
`newOSError(errcode)`.

There are still some places that have not been replaced. After checking,
they are not system errors in the traditional sense.

```nim
proc dlclose(lib: LibHandle) =
  raise newException(OSError, "dlclose not implemented on Nintendo Switch!")
```

```nim
if not fileExists(result) and not dirExists(result):
  # consider using: `raiseOSError(osLastError(), result)`
  raise newException(OSError, "file '" & result & "' does not exist")
```

```nim
proc paramStr*(i: int): string =
  raise newException(OSError, "paramStr is not implemented on Genode")
```
  • Loading branch information
haoyu234 authored Nov 17, 2023
1 parent fbfd4de commit 39fbd30
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 54 deletions.
20 changes: 10 additions & 10 deletions lib/posix/posix_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ proc uname*(): Uname =

var u: Utsname
if uname(u) != 0:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

result.sysname = charArrayToString u.sysname
result.nodename = charArrayToString u.nodename
Expand All @@ -45,17 +45,17 @@ proc uname*(): Uname =
proc fsync*(fd: int) =
## synchronize a file's buffer cache to the storage device
if fsync(fd.cint) != 0:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

proc stat*(path: string): Stat =
## Returns file status in a `Stat` structure
if stat(path.cstring, result) != 0:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

proc memoryLock*(a1: pointer, a2: int) =
## Locks pages starting from a1 for a1 bytes and prevent them from being swapped.
if mlock(a1, a2) != 0:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

proc memoryLockAll*(flags: int) =
## Locks all memory for the running process to prevent swapping.
Expand All @@ -65,23 +65,23 @@ proc memoryLockAll*(flags: int) =
## memoryLockAll(MCL_CURRENT or MCL_FUTURE)
## ```
if mlockall(flags.cint) != 0:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

proc memoryUnlock*(a1: pointer, a2: int) =
## Unlock pages starting from a1 for a1 bytes and allow them to be swapped.
if munlock(a1, a2) != 0:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

proc memoryUnlockAll*() =
## Unlocks all memory for the running process to allow swapping.
if munlockall() != 0:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

proc sendSignal*(pid: Pid, signal: int) =
## Sends a signal to a running process by calling `kill`.
## Raise exception in case of failure e.g. process not running.
if kill(pid, signal.cint) != 0:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

proc mkstemp*(prefix: string, suffix=""): (string, File) =
## Creates a unique temporary file from a prefix string. A six-character string
Expand All @@ -103,14 +103,14 @@ proc mkstemp*(prefix: string, suffix=""): (string, File) =
var f: File
if open(f, fd, fmReadWrite):
return ($tmpl, f)
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))

proc mkdtemp*(prefix: string): string =
## Creates a unique temporary directory from a prefix string. Adds a six chars suffix.
## The directory is created with permissions 0700. Returns the directory name.
var tmpl = cstring(prefix & "XXXXXX")
if mkdtemp(tmpl) == nil:
raise newException(OSError, $strerror(errno))
raiseOSError(OSErrorCode(errno))
return $tmpl

proc osReleaseFile*(): Config {.since: (1, 5).} =
Expand Down
38 changes: 19 additions & 19 deletions lib/pure/asyncdispatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ when defined(windows) or defined(nimdoc):
if flags.isDisconnectionError(errcode):
retFuture.complete("")
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
if dataBuf.buf != nil:
dealloc dataBuf.buf
dataBuf.buf = nil
Expand All @@ -551,7 +551,7 @@ when defined(windows) or defined(nimdoc):
if flags.isDisconnectionError(err):
retFuture.complete("")
else:
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
elif ret == 0:
# Request completed immediately.
if bytesReceived != 0:
Expand Down Expand Up @@ -603,7 +603,7 @@ when defined(windows) or defined(nimdoc):
if flags.isDisconnectionError(errcode):
retFuture.complete(0)
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
if dataBuf.buf != nil:
dataBuf.buf = nil
)
Expand All @@ -619,7 +619,7 @@ when defined(windows) or defined(nimdoc):
if flags.isDisconnectionError(err):
retFuture.complete(0)
else:
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
elif ret == 0:
# Request completed immediately.
if bytesReceived != 0:
Expand Down Expand Up @@ -667,7 +667,7 @@ when defined(windows) or defined(nimdoc):
if flags.isDisconnectionError(err):
retFuture.complete()
else:
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
else:
retFuture.complete()
# We don't deallocate `ol` here because even though this completed
Expand Down Expand Up @@ -702,7 +702,7 @@ when defined(windows) or defined(nimdoc):
if errcode == OSErrorCode(-1):
retFuture.complete()
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
)

let ret = WSASendTo(socket.SocketHandle, addr dataBuf, 1, addr bytesSent,
Expand All @@ -712,7 +712,7 @@ when defined(windows) or defined(nimdoc):
let err = osLastError()
if err.int32 != ERROR_IO_PENDING:
GC_unref(ol)
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
else:
retFuture.complete()
# We don't deallocate `ol` here because even though this completed
Expand Down Expand Up @@ -746,7 +746,7 @@ when defined(windows) or defined(nimdoc):
else:
# datagram sockets don't have disconnection,
# so we can just raise an exception
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
)

let res = WSARecvFrom(socket.SocketHandle, addr dataBuf, 1,
Expand All @@ -757,7 +757,7 @@ when defined(windows) or defined(nimdoc):
let err = osLastError()
if err.int32 != ERROR_IO_PENDING:
GC_unref(ol)
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
else:
# Request completed immediately.
if bytesReceived != 0:
Expand Down Expand Up @@ -808,7 +808,7 @@ when defined(windows) or defined(nimdoc):
else:
retFuture.complete(newAcceptFut.read)
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))

template completeAccept() {.dirty.} =
var listenSock = socket
Expand Down Expand Up @@ -1468,7 +1468,7 @@ else:
if flags.isDisconnectionError(lastError):
retFuture.complete("")
else:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
result = false # We still want this callback to be called.
elif res == 0:
Expand Down Expand Up @@ -1497,7 +1497,7 @@ else:
if flags.isDisconnectionError(lastError):
retFuture.complete(0)
else:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
result = false # We still want this callback to be called.
else:
Expand Down Expand Up @@ -1563,7 +1563,7 @@ else:
let lastError = osLastError()
if lastError.int32 != EINTR and lastError.int32 != EWOULDBLOCK and
lastError.int32 != EAGAIN:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
result = false # We still want this callback to be called.
else:
Expand All @@ -1589,7 +1589,7 @@ else:
let lastError = osLastError()
if lastError.int32 != EINTR and lastError.int32 != EWOULDBLOCK and
lastError.int32 != EAGAIN:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
result = false
else:
Expand Down Expand Up @@ -1630,7 +1630,7 @@ else:
if flags.isDisconnectionError(lastError):
return false
else:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
try:
let address = getAddrString(cast[ptr SockAddr](addr sockAddress))
Expand Down Expand Up @@ -1763,7 +1763,7 @@ when defined(windows) or defined(nimdoc):
socket.SocketHandle.setSockOptInt(SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, 1) # 15022
retFuture.complete()
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
)

let ret = connectEx(socket.SocketHandle, addrInfo.ai_addr,
Expand All @@ -1781,7 +1781,7 @@ when defined(windows) or defined(nimdoc):
# With ERROR_IO_PENDING `ol` will be deallocated in `poll`,
# and the future will be completed/failed there, too.
GC_unref(ol)
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
proc doConnect(socket: AsyncFD, addrInfo: ptr AddrInfo): owned(Future[void]) =
let retFuture = newFuture[void]("doConnect")
Expand All @@ -1798,7 +1798,7 @@ else:
# interrupted, keep waiting
return false
else:
retFuture.fail(newException(OSError, osErrorMsg(OSErrorCode(ret))))
retFuture.fail(newOSError(OSErrorCode(ret)))
return true

let ret = connect(socket.SocketHandle,
Expand All @@ -1812,7 +1812,7 @@ else:
if lastError.int32 == EINTR or lastError.int32 == EINPROGRESS:
addWrite(socket, cb)
else:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))

template asyncAddrInfoLoop(addrInfo: ptr AddrInfo, fd: untyped,
protocol: Protocol = IPPROTO_RAW) =
Expand Down
32 changes: 16 additions & 16 deletions lib/pure/asyncfile.nim
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ proc readBuffer*(f: AsyncFile, buf: pointer, size: int): Future[int] =
if errcode.int32 == ERROR_HANDLE_EOF:
retFuture.complete(0)
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
)
ol.offset = DWORD(f.offset and 0xffffffff)
ol.offsetHigh = DWORD(f.offset shr 32)
Expand All @@ -162,7 +162,7 @@ proc readBuffer*(f: AsyncFile, buf: pointer, size: int): Future[int] =
# This happens in Windows Server 2003
retFuture.complete(0)
else:
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
else:
# Request completed immediately.
var bytesRead: DWORD
Expand All @@ -173,7 +173,7 @@ proc readBuffer*(f: AsyncFile, buf: pointer, size: int): Future[int] =
if err.int32 == ERROR_HANDLE_EOF:
retFuture.complete(0)
else:
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))
retFuture.fail(newOSError(osLastError()))
else:
assert bytesRead > 0
assert bytesRead <= size
Expand All @@ -186,7 +186,7 @@ proc readBuffer*(f: AsyncFile, buf: pointer, size: int): Future[int] =
if res < 0:
let lastError = osLastError()
if lastError.int32 != EAGAIN:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
result = false # We still want this callback to be called.
elif res == 0:
Expand Down Expand Up @@ -228,7 +228,7 @@ proc read*(f: AsyncFile, size: int): Future[string] =
if errcode.int32 == ERROR_HANDLE_EOF:
retFuture.complete("")
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
if buffer != nil:
dealloc buffer
buffer = nil
Expand All @@ -251,7 +251,7 @@ proc read*(f: AsyncFile, size: int): Future[string] =
# This happens in Windows Server 2003
retFuture.complete("")
else:
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
else:
# Request completed immediately.
var bytesRead: DWORD
Expand All @@ -262,7 +262,7 @@ proc read*(f: AsyncFile, size: int): Future[string] =
if err.int32 == ERROR_HANDLE_EOF:
retFuture.complete("")
else:
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))
retFuture.fail(newOSError(osLastError()))
else:
assert bytesRead > 0
assert bytesRead <= size
Expand All @@ -279,7 +279,7 @@ proc read*(f: AsyncFile, size: int): Future[string] =
if res < 0:
let lastError = osLastError()
if lastError.int32 != EAGAIN:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
result = false # We still want this callback to be called.
elif res == 0:
Expand Down Expand Up @@ -350,7 +350,7 @@ proc writeBuffer*(f: AsyncFile, buf: pointer, size: int): Future[void] =
assert bytesCount == size.int32
retFuture.complete()
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
)
# passing -1 here should work according to MSDN, but doesn't. For more
# information see
Expand All @@ -367,14 +367,14 @@ proc writeBuffer*(f: AsyncFile, buf: pointer, size: int): Future[void] =
let err = osLastError()
if err.int32 != ERROR_IO_PENDING:
GC_unref(ol)
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
else:
# Request completed immediately.
var bytesWritten: DWORD
let overlappedRes = getOverlappedResult(f.fd.Handle,
cast[POVERLAPPED](ol), bytesWritten, false.WINBOOL)
if not overlappedRes.bool:
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))
retFuture.fail(newOSError(osLastError()))
else:
assert bytesWritten == size.int32
retFuture.complete()
Expand All @@ -389,7 +389,7 @@ proc writeBuffer*(f: AsyncFile, buf: pointer, size: int): Future[void] =
if res < 0:
let lastError = osLastError()
if lastError.int32 != EAGAIN:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
result = false # We still want this callback to be called.
else:
Expand Down Expand Up @@ -423,7 +423,7 @@ proc write*(f: AsyncFile, data: string): Future[void] =
assert bytesCount == data.len.int32
retFuture.complete()
else:
retFuture.fail(newException(OSError, osErrorMsg(errcode)))
retFuture.fail(newOSError(errcode))
if buffer != nil:
dealloc buffer
buffer = nil
Expand All @@ -442,14 +442,14 @@ proc write*(f: AsyncFile, data: string): Future[void] =
dealloc buffer
buffer = nil
GC_unref(ol)
retFuture.fail(newException(OSError, osErrorMsg(err)))
retFuture.fail(newOSError(err))
else:
# Request completed immediately.
var bytesWritten: DWORD
let overlappedRes = getOverlappedResult(f.fd.Handle,
cast[POVERLAPPED](ol), bytesWritten, false.WINBOOL)
if not overlappedRes.bool:
retFuture.fail(newException(OSError, osErrorMsg(osLastError())))
retFuture.fail(newOSError(osLastError()))
else:
assert bytesWritten == data.len.int32
retFuture.complete()
Expand All @@ -470,7 +470,7 @@ proc write*(f: AsyncFile, data: string): Future[void] =
if res < 0:
let lastError = osLastError()
if lastError.int32 != EAGAIN:
retFuture.fail(newException(OSError, osErrorMsg(lastError)))
retFuture.fail(newOSError(lastError))
else:
result = false # We still want this callback to be called.
else:
Expand Down
Loading

0 comments on commit 39fbd30

Please sign in to comment.