Skip to content

Commit

Permalink
Update encodings.nim, fix open with bad arg raising no `EncodingErr…
Browse files Browse the repository at this point in the history
…or` (#23481)

On POSIX, `std/encodings` uses iconv, and `iconv_open` returns
`(iconv_t) -1` on failure, not `NULL`
  • Loading branch information
litlighilit committed Apr 6, 2024
1 parent 8c9fde7 commit c23d6a3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/pure/encodings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ proc open*(destEncoding = "UTF-8", srcEncoding = "CP1252"): EncodingConverter =
## Raises `EncodingError` if it cannot fulfill the request.
when not defined(windows):
result = iconvOpen(destEncoding, srcEncoding)
if result == nil:
if result == cast[EncodingConverter](-1):
raise newException(EncodingError,
"cannot create encoding converter from " &
srcEncoding & " to " & destEncoding)
Expand Down
4 changes: 4 additions & 0 deletions tests/stdlib/tencodings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ block:
doAssert orig == "\195\182\195\164\195\188\195\159"
doAssert ibm850 == "\148\132\129\225"
doAssert convert(ibm850, current, "ibm850") == orig

block: # fixes about #23481
doAssertRaises EncodingError:
discard open(destEncoding="this is a invalid enc")

0 comments on commit c23d6a3

Please sign in to comment.