make implicit cstring conversions explicit (#19488)

The Nim manual says that an implicit conversion to cstring will
eventually not be allowed [1]:

    A Nim `string` is implicitly convertible to `cstring` for convenience.

    [...]

    Even though the conversion is implicit, it is not *safe*: The garbage collector
    does not consider a `cstring` to be a root and may collect the underlying
    memory. For this reason, the implicit conversion will be removed in future
    releases of the Nim compiler. Certain idioms like conversion of a `const` string
    to `cstring` are safe and will remain to be allowed.

And from Nim 1.6.0, such a conversion triggers a warning [2]:

    A dangerous implicit conversion to `cstring` now triggers a `[CStringConv]` warning.
    This warning will become an error in future versions! Use an explicit conversion
    like `cstring(x)` in order to silence the warning.

However, some files in this repo produced such a warning. For example,
before this commit, compiling `parsejson.nim` would produce:

    /foo/Nim/lib/pure/parsejson.nim(221, 37) Warning: implicit conversion to 'cstring' from a non-const location: my.buf; this will become a compile time error in the future [CStringConv]
    /foo/Nim/lib/pure/parsejson.nim(231, 39) Warning: implicit conversion to 'cstring' from a non-const location: my.buf; this will become a compile time error in the future [CStringConv]

This commit resolves the most visible `CStringConv` warnings, making the
cstring conversions explicit.

[1] d2318d9ccf/doc/manual.md (cstring-type)
[2] d2318d9ccf/changelogs/changelog_1_6_0.md (type-system)
This commit is contained in:
ee7 2022-08-19 21:40:53 +02:00 • committed by GitHub
commit e8657c7107
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 63 additions and 63 deletions

View file

@ -1259,12 +1259,12 @@ proc findExe*(exe: string, followSymlinks: bool = true;
while followSymlinks: # doubles as if here
if x.symlinkExists:
var r = newString(maxSymlinkLen)
var len = readlink(x, r, maxSymlinkLen)
var len = readlink(x.cstring, r.cstring, maxSymlinkLen)
if len < 0:
raiseOSError(osLastError(), exe)
if len > maxSymlinkLen:
r = newString(len+1)
len = readlink(x, r, len)
len = readlink(x.cstring, r.cstring, len)
setLen(r, len)
if isAbsolute(r):
x = r
@ -1404,8 +1404,8 @@ when not defined(nimscript):
var bufsize = 1024 # should be enough
result = newString(bufsize)
while true:
if getcwd(result, bufsize) != nil:
setLen(result, c_strlen(result))
if getcwd(result.cstring, bufsize) != nil:
setLen(result, c_strlen(result.cstring))
break
else:
let err = osLastError()
@ -1774,12 +1774,12 @@ proc expandSymlink*(symlinkPath: string): string {.noWeirdTarget.} =
result = symlinkPath
else:
result = newString(maxSymlinkLen)
var len = readlink(symlinkPath, result, maxSymlinkLen)
var len = readlink(symlinkPath, result.cstring, maxSymlinkLen)
if len < 0:
raiseOSError(osLastError(), symlinkPath)
if len > maxSymlinkLen:
result = newString(len+1)
len = readlink(symlinkPath, result, len)
len = readlink(symlinkPath, result.cstring, len)
setLen(result, len)
const hasCCopyfile = defined(osx) and not defined(nimLegacyCopyFile)
@ -2344,7 +2344,7 @@ iterator walkDir*(dir: string; relative = false, checkDir = false):
var k = pcFile
template kSetGeneric() = # pure Posix component `k` resolution
if lstat(path, s) < 0'i32: continue # don't yield
if lstat(path.cstring, s) < 0'i32: continue # don't yield
elif S_ISDIR(s.st_mode):
k = pcDir
elif S_ISLNK(s.st_mode):
@ -3041,10 +3041,10 @@ when not weirdTarget and (defined(freebsd) or defined(dragonfly) or defined(netb
when not weirdTarget and (defined(linux) or defined(solaris) or defined(bsd) or defined(aix)):
proc getApplAux(procPath: string): string =
result = newString(maxSymlinkLen)
var len = readlink(procPath, result, maxSymlinkLen)
var len = readlink(procPath, result.cstring, maxSymlinkLen)
if len > maxSymlinkLen:
result = newString(len+1)
len = readlink(procPath, result, len)
len = readlink(procPath, result.cstring, len)
setLen(result, len)
when not weirdTarget and defined(openbsd):
@ -3169,7 +3169,7 @@ proc getAppFilename*(): string {.rtl, extern: "nos$1", tags: [ReadIOEffect], noW
var size = cuint32(0)
getExecPath1(nil, size)
result = newString(int(size))
if getExecPath2(result, size):
if getExecPath2(result.cstring, size):
result = "" # error!
if result.len > 0:
result = result.expandFilename