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:
parent
7fe6dedb62
commit
e8657c7107
14 changed files with 63 additions and 63 deletions
|
|
@ -367,7 +367,7 @@ method log*(logger: ConsoleLogger, level: Level, args: varargs[string, `$`]) =
|
|||
if level >= logging.level and level >= logger.levelThreshold:
|
||||
let ln = substituteLog(logger.fmtStr, level, args)
|
||||
when defined(js):
|
||||
let cln: cstring = ln
|
||||
let cln = ln.cstring
|
||||
case level
|
||||
of lvlDebug: {.emit: "console.debug(`cln`);".}
|
||||
of lvlInfo: {.emit: "console.info(`cln`);".}
|
||||
|
|
|
|||
|
|
@ -300,7 +300,7 @@ proc getAddrInfo*(address: string, port: Port, domain: Domain = AF_INET,
|
|||
if domain == AF_INET6:
|
||||
hints.ai_flags = AI_V4MAPPED
|
||||
let socketPort = if sockType == SOCK_RAW: "" else: $port
|
||||
var gaiResult = getaddrinfo(address, socketPort, addr(hints), result)
|
||||
var gaiResult = getaddrinfo(address, socketPort.cstring, addr(hints), result)
|
||||
if gaiResult != 0'i32:
|
||||
when useWinVersion or defined(freertos):
|
||||
raiseOSError(osLastError())
|
||||
|
|
@ -460,10 +460,10 @@ when not useNimNetLite:
|
|||
const size = 256
|
||||
result = newString(size)
|
||||
when useWinVersion:
|
||||
let success = winlean.gethostname(result, size)
|
||||
let success = winlean.gethostname(result.cstring, size)
|
||||
else:
|
||||
# Posix
|
||||
let success = posix.gethostname(result, size)
|
||||
let success = posix.gethostname(result.cstring, size)
|
||||
if success != 0.cint:
|
||||
raiseOSError(osLastError())
|
||||
let x = len(cstring(result))
|
||||
|
|
|
|||
|
|
@ -682,7 +682,7 @@ when defineSsl:
|
|||
var found = false
|
||||
let useEnvVars = (if verifyMode == CVerifyPeerUseEnvVars: true else: false)
|
||||
for fn in scanSSLCertificates(useEnvVars = useEnvVars):
|
||||
if newCTX.SSL_CTX_load_verify_locations(fn, nil) == VerifySuccess:
|
||||
if newCTX.SSL_CTX_load_verify_locations(fn.cstring, nil) == VerifySuccess:
|
||||
found = true
|
||||
break
|
||||
if not found:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1086,9 +1086,9 @@ elif not defined(useNimRtl):
|
|||
var pid: Pid
|
||||
|
||||
if (poUsePath in data.options):
|
||||
res = posix_spawnp(pid, data.sysCommand, fops, attr, data.sysArgs, data.sysEnv)
|
||||
res = posix_spawnp(pid, data.sysCommand.cstring, fops, attr, data.sysArgs, data.sysEnv)
|
||||
else:
|
||||
res = posix_spawn(pid, data.sysCommand, fops, attr, data.sysArgs, data.sysEnv)
|
||||
res = posix_spawn(pid, data.sysCommand.cstring, fops, attr, data.sysArgs, data.sysEnv)
|
||||
|
||||
discard posix_spawn_file_actions_destroy(fops)
|
||||
discard posix_spawnattr_destroy(attr)
|
||||
|
|
@ -1174,14 +1174,14 @@ elif not defined(useNimRtl):
|
|||
when defined(uClibc) or defined(linux) or defined(haiku):
|
||||
# uClibc environment (OpenWrt included) doesn't have the full execvpe
|
||||
let exe = findExe(data.sysCommand)
|
||||
discard execve(exe, data.sysArgs, data.sysEnv)
|
||||
discard execve(exe.cstring, data.sysArgs, data.sysEnv)
|
||||
else:
|
||||
# MacOSX doesn't have execvpe, so we need workaround.
|
||||
# On MacOSX we can arrive here only from fork, so this is safe:
|
||||
environ = data.sysEnv
|
||||
discard execvp(data.sysCommand, data.sysArgs)
|
||||
discard execvp(data.sysCommand.cstring, data.sysArgs)
|
||||
else:
|
||||
discard execve(data.sysCommand, data.sysArgs, data.sysEnv)
|
||||
discard execve(data.sysCommand.cstring, data.sysArgs, data.sysEnv)
|
||||
|
||||
startProcessFail(data)
|
||||
{.pop.}
|
||||
|
|
|
|||
|
|
@ -221,7 +221,7 @@ proc parseString(my: var JsonParser): TokKind =
|
|||
add(my.a, 'u')
|
||||
inc(pos, 2)
|
||||
var pos2 = pos
|
||||
var r = parseEscapedUTF16(my.buf, pos)
|
||||
var r = parseEscapedUTF16(cstring(my.buf), pos)
|
||||
if r < 0:
|
||||
my.err = errInvalidToken
|
||||
break
|
||||
|
|
@ -231,7 +231,7 @@ proc parseString(my: var JsonParser): TokKind =
|
|||
my.err = errInvalidToken
|
||||
break
|
||||
inc(pos, 2)
|
||||
var s = parseEscapedUTF16(my.buf, pos)
|
||||
var s = parseEscapedUTF16(cstring(my.buf), pos)
|
||||
if (s and 0xfc00) == 0xdc00 and s > 0:
|
||||
r = 0x10000 + (((r - 0xd800) shl 10) or (s - 0xdc00))
|
||||
else:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue