fixes ptr to cstring warnings[backport] (#20848)

* fix =#13790 ptr char (+friends) should not implicitly convert to cstring

* Apply suggestions from code review

* first round; compiles on windows

* nimPreviewSlimSystem

* conversion is unsafe, cast needed

* fixes more tests

* fixes asyncnet

* another try another error

* last one

* true

* one more

* why bugs didn't show at once

* add `nimPreviewCstringConversion` switch

* typo

* fixes ptr to cstring warnings[backport]

* add fixes

Co-authored-by: xflywind <43030857+xflywind@users.noreply.github.com>
This commit is contained in:
ringabout 2022-11-16 17:22:51 +08:00 • committed by GitHub
commit 06cd15663d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 50 additions and 50 deletions

View file

@ -58,7 +58,7 @@ proc launchSwarm(name: ptr SockAddr) {.async.} =
16384, cast[ptr SockAddr](addr saddr),
addr slen)
size = 0
var grammString = $cstring(addr buffer)
var grammString = $cast[cstring](addr buffer)
if grammString == message:
saveSendingPort(sockport)
inc(recvCount)
@ -80,7 +80,7 @@ proc readMessages(server: AsyncFD) {.async.} =
16384, cast[ptr SockAddr](addr(saddr)),
addr(slen))
size = 0
var grammString = $cstring(addr buffer)
var grammString = $cast[cstring](addr buffer)
if grammString.startsWith("Message ") and
saddr.sin_addr.s_addr == nativesockets.ntohl(INADDR_LOOPBACK.uint32):
await sendTo(server, addr grammString[0], len(grammString),

View file

@ -8,7 +8,7 @@ proc c_sprintf(buf, fmt: cstring) {.importc:"sprintf", header: "<stdio.h>", vara
proc floatToStr(f: float64): string =
var buffer: array[128, char]
c_sprintf(addr buffer, "%.16e", f)
c_sprintf(cast[cstring](addr buffer), "%.16e", f)
result = ""
for ch in buffer:
if ch == '\0':

View file

@ -29,7 +29,7 @@ suite "inet_ntop tests":
check: ip4.s_addr == 0x10111213'u32
var buff: array[0..255, char]
let r = inet_ntop(AF_INET, cast[pointer](ip4.s_addr.addr), buff[0].addr, buff.len.int32)
let r = inet_ntop(AF_INET, cast[pointer](ip4.s_addr.addr), cast[cstring](buff[0].addr), buff.len.int32)
let res = if r == nil: "" else: $r
check: res == "19.18.17.16"
@ -41,7 +41,7 @@ suite "inet_ntop tests":
var ip6 = [0x1000'u16, 0x1001, 0x2000, 0x2001, 0x3000, 0x3001, 0x4000, 0x4001]
var buff: array[0..255, char]
let r = inet_ntop(AF_INET6, cast[pointer](ip6[0].addr), buff[0].addr, buff.len.int32)
let r = inet_ntop(AF_INET6, cast[pointer](ip6[0].addr), cast[cstring](buff[0].addr), buff.len.int32)
let res = if r == nil: "" else: $r
check: not ipv6Support or res == "10:110:20:120:30:130:40:140"

View file

@ -47,7 +47,7 @@ import strutils
let arr = ['H','e','l','l','o',' ','W','o','r','l','d','!','\0']
doAssert $arr == "['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\\x00']"
doAssert $cstring(addr arr) == "Hello World!"
doAssert $cast[cstring](addr arr) == "Hello World!"
proc takes(c: cstring) =
doAssert c == cstring""