Skip to content

Commit

Permalink
introduce csize_t instead of fixing csize (#12497)
Browse files Browse the repository at this point in the history
  • Loading branch information
krux02 authored and Araq committed Oct 31, 2019
1 parent 0c7b6c9 commit 99078d8
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 97 deletions.
2 changes: 1 addition & 1 deletion compiler/linter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ proc beautifyName(s: string, k: TSymKind): string =
# Types should start with a capital unless builtins like 'int' etc.:
if s =~ ["int", "uint", "cint", "cuint", "clong", "cstring", "string",
"char", "byte", "bool", "openArray", "seq", "array", "void",
"pointer", "float", "csize", "cdouble", "cchar", "cschar",
"pointer", "float", "csize", "csize_t", "cdouble", "cchar", "cschar",
"cshort", "cu", "nil", "typedesc", "auto", "any",
"range", "openarray", "varargs", "set", "cfloat", "ref", "ptr",
"untyped", "typed", "static", "sink", "lent", "type", "owned"]:
Expand Down
6 changes: 6 additions & 0 deletions lib/posix/posix.nim
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,15 @@ proc CMSG_FIRSTHDR*(mhdr: ptr Tmsghdr): ptr Tcmsghdr {.
importc, header: "<sys/socket.h>".}

proc CMSG_SPACE*(len: csize): csize {.
importc, header: "<sys/socket.h>", deprecated: "argument `len` should be of type `csize_t`".}

proc CMSG_SPACE*(len: csize_t): csize_t {.
importc, header: "<sys/socket.h>".}

proc CMSG_LEN*(len: csize): csize {.
importc, header: "<sys/socket.h>", deprecated: "argument `len` should be of type `csize_t`".}

proc CMSG_LEN*(len: csize_t): csize_t {.
importc, header: "<sys/socket.h>".}

const
Expand Down
16 changes: 8 additions & 8 deletions lib/posix/posix_linux_amd64.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ type

Glob* {.importc: "glob_t", header: "<glob.h>",
final, pure.} = object ## glob_t
gl_pathc*: csize ## Count of paths matched by pattern.
gl_pathv*: cstringArray ## Pointer to a list of matched pathnames.
gl_offs*: csize ## Slots to reserve at the beginning of gl_pathv.
gl_pathc*: csize_t ## Count of paths matched by pattern.
gl_pathv*: cstringArray ## Pointer to a list of matched pathnames.
gl_offs*: csize_t ## Slots to reserve at the beginning of gl_pathv.
gl_flags*: cint
gl_closedir*: pointer
gl_readdir*: pointer
Expand Down Expand Up @@ -382,7 +382,7 @@ type
aio_lio_opcode*: cint ## Operation to be performed.
aio_reqprio*: cint ## Request priority offset.
aio_buf*: pointer ## Location of buffer.
aio_nbytes*: csize ## Length of transfer.
aio_nbytes*: csize_t ## Length of transfer.
aio_sigevent*: SigEvent ## Signal number and value.
next_prio: pointer
abs_prio: cint
Expand Down Expand Up @@ -445,22 +445,22 @@ type
IOVec* {.importc: "struct iovec", pure, final,
header: "<sys/uio.h>".} = object ## struct iovec
iov_base*: pointer ## Base address of a memory region for input or output.
iov_len*: csize ## The size of the memory pointed to by iov_base.
iov_len*: csize_t ## The size of the memory pointed to by iov_base.

Tmsghdr* {.importc: "struct msghdr", pure, final,
header: "<sys/socket.h>".} = object ## struct msghdr
msg_name*: pointer ## Optional address.
msg_namelen*: SockLen ## Size of address.
msg_iov*: ptr IOVec ## Scatter/gather array.
msg_iovlen*: csize ## Members in msg_iov.
msg_iovlen*: csize_t ## Members in msg_iov.
msg_control*: pointer ## Ancillary data; see below.
msg_controllen*: csize ## Ancillary data buffer len.
msg_controllen*: csize_t ## Ancillary data buffer len.
msg_flags*: cint ## Flags on received message.


Tcmsghdr* {.importc: "struct cmsghdr", pure, final,
header: "<sys/socket.h>".} = object ## struct cmsghdr
cmsg_len*: csize ## Data byte count, including the cmsghdr.
cmsg_len*: csize_t ## Data byte count, including the cmsghdr.
cmsg_level*: cint ## Originating protocol.
cmsg_type*: cint ## Protocol-specific type.

Expand Down
4 changes: 2 additions & 2 deletions lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,7 @@ proc find*(a: SkipTable, s, sub: string, start: Natural = 0, last = 0): int
return -1

when not (defined(js) or defined(nimdoc) or defined(nimscript)):
proc c_memchr(cstr: pointer, c: char, n: csize): pointer {.
proc c_memchr(cstr: pointer, c: char, n: csize_t): pointer {.
importc: "memchr", header: "<string.h>".}
const hasCStringBuiltin = true
else:
Expand All @@ -1872,7 +1872,7 @@ proc find*(s: string, sub: char, start: Natural = 0, last = 0): int {.noSideEffe
when hasCStringBuiltin:
let L = last-start+1
if L > 0:
let found = c_memchr(s[start].unsafeAddr, sub, L)
let found = c_memchr(s[start].unsafeAddr, sub, cast[csize_t](L))
if not found.isNil:
return cast[ByteAddress](found) -% cast[ByteAddress](s.cstring)
else:
Expand Down
8 changes: 5 additions & 3 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,9 @@ type # these work for most platforms:
## This is the same as the type ``short`` in *C*.
cint* {.importc: "int", nodecl.} = int32
## This is the same as the type ``int`` in *C*.
csize* {.importc: "size_t", nodecl.} = int
csize* {.importc: "size_t", nodecl, deprecated: "use `csize_t` instead".} = int
## This isn't the same as ``size_t`` in *C*. Don't use it.
csize_t* {.importc: "size_t", nodecl.} = uint
## This is the same as the type ``size_t`` in *C*.
clonglong* {.importc: "long long", nodecl.} = int64
## This is the same as the type ``long long`` in *C*.
Expand Down Expand Up @@ -3638,7 +3640,7 @@ when not defined(JS): #and not defined(nimscript):
when declared(memTrackerOp):
memTrackerOp("copyMem", dest, size)
proc moveMem(dest, source: pointer, size: Natural) =
c_memmove(dest, source, size)
c_memmove(dest, source, csize_t(size))
when declared(memTrackerOp):
memTrackerOp("moveMem", dest, size)
proc equalMem(a, b: pointer, size: Natural): bool =
Expand All @@ -3656,7 +3658,7 @@ when not defined(JS): #and not defined(nimscript):
else: result = 0
else:
let minlen = min(x.len, y.len)
result = int(nimCmpMem(x.cstring, y.cstring, minlen.csize))
result = int(nimCmpMem(x.cstring, y.cstring, cast[csize_t](minlen)))
if result == 0:
result = x.len - y.len

Expand Down
20 changes: 10 additions & 10 deletions lib/system/ansi_c.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
when not defined(nimHasHotCodeReloading):
{.pragma: nonReloadable.}

proc c_memchr*(s: pointer, c: cint, n: csize): pointer {.
proc c_memchr*(s: pointer, c: cint, n: csize_t): pointer {.
importc: "memchr", header: "<string.h>".}
proc c_memcmp*(a, b: pointer, size: csize): cint {.
proc c_memcmp*(a, b: pointer, size: csize_t): cint {.
importc: "memcmp", header: "<string.h>", noSideEffect.}
proc c_memcpy*(a, b: pointer, size: csize): pointer {.
proc c_memcpy*(a, b: pointer, size: csize_t): pointer {.
importc: "memcpy", header: "<string.h>", discardable.}
proc c_memmove*(a, b: pointer, size: csize): pointer {.
proc c_memmove*(a, b: pointer, size: csize_t): pointer {.
importc: "memmove", header: "<string.h>",discardable.}
proc c_memset*(p: pointer, value: cint, size: csize): pointer {.
proc c_memset*(p: pointer, value: cint, size: csize_t): pointer {.
importc: "memset", header: "<string.h>", discardable.}
proc c_strcmp*(a, b: cstring): cint {.
importc: "strcmp", header: "<string.h>", noSideEffect.}
proc c_strlen*(a: cstring): csize {.
proc c_strlen*(a: cstring): csize_t {.
importc: "strlen", header: "<string.h>", noSideEffect.}
proc c_abort*() {.
importc: "abort", header: "<stdlib.h>", noSideEffect, noreturn.}
Expand Down Expand Up @@ -132,18 +132,18 @@ proc c_sprintf*(buf, frmt: cstring): cint {.
importc: "sprintf", header: "<stdio.h>", varargs, noSideEffect.}
# we use it only in a way that cannot lead to security issues

proc c_malloc*(size: csize): pointer {.
proc c_malloc*(size: csize_t): pointer {.
importc: "malloc", header: "<stdlib.h>".}
proc c_free*(p: pointer) {.
importc: "free", header: "<stdlib.h>".}
proc c_realloc*(p: pointer, newsize: csize): pointer {.
proc c_realloc*(p: pointer, newsize: csize_t): pointer {.
importc: "realloc", header: "<stdlib.h>".}

proc c_fwrite*(buf: pointer, size, n: csize, f: CFilePtr): cint {.
proc c_fwrite*(buf: pointer, size, n: csize_t, f: CFilePtr): cint {.
importc: "fwrite", header: "<stdio.h>".}

proc rawWrite*(f: CFilePtr, s: cstring) {.compilerproc, nonReloadable, inline.} =
# we cannot throw an exception here!
discard c_fwrite(s, 1, s.len, f)
discard c_fwrite(s, 1, cast[csize_t](s.len), f)

{.pop.}
18 changes: 9 additions & 9 deletions lib/system/io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ proc c_feof(f: File): cint {.
importc: "feof", header: "<stdio.h>".}

when not declared(c_fwrite):
proc c_fwrite(buf: pointer, size, n: csize, f: File): cint {.
proc c_fwrite(buf: pointer, size, n: csize_t, f: File): cint {.
importc: "fwrite", header: "<stdio.h>".}

# C routine that is used here:
proc c_fread(buf: pointer, size, n: csize, f: File): csize {.
proc c_fread(buf: pointer, size, n: csize_t, f: File): csize_t {.
importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect].}
when defined(windows):
when not defined(amd64):
Expand All @@ -107,7 +107,7 @@ else:
importc: "ftello", header: "<stdio.h>", tags: [].}
proc c_ferror(f: File): cint {.
importc: "ferror", header: "<stdio.h>", tags: [].}
proc c_setvbuf(f: File, buf: pointer, mode: cint, size: csize): cint {.
proc c_setvbuf(f: File, buf: pointer, mode: cint, size: csize_t): cint {.
importc: "setvbuf", header: "<stdio.h>", tags: [].}

proc c_fprintf(f: File, frmt: cstring): cint {.
Expand Down Expand Up @@ -153,7 +153,7 @@ proc readBuffer*(f: File, buffer: pointer, len: Natural): int {.
## reads `len` bytes into the buffer pointed to by `buffer`. Returns
## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater.
result = c_fread(buffer, 1, len, f)
result = cast[int](c_fread(buffer, 1, cast[csize_t](len), f))
if result != len: checkErr(f)

proc readBytes*(f: File, a: var openArray[int8|uint8], start, len: Natural): int {.
Expand Down Expand Up @@ -185,7 +185,7 @@ proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {.
## writes the bytes of buffer pointed to by the parameter `buffer` to the
## file `f`. Returns the number of actual written bytes, which may be less
## than `len` in case of an error.
result = c_fwrite(buffer, 1, len, f)
result = cast[int](c_fwrite(buffer, 1, cast[csize_t](len), f))
checkErr(f)

proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {.
Expand Down Expand Up @@ -296,7 +296,7 @@ proc readLine*(f: File, line: var TaintedString): bool {.tags: [ReadIOEffect],
## character(s) are not part of the returned string. Returns ``false``
## if the end of the file has been reached, ``true`` otherwise. If
## ``false`` is returned `line` contains no new data.
proc c_memchr(s: pointer, c: cint, n: csize): pointer {.
proc c_memchr(s: pointer, c: cint, n: csize_t): pointer {.
importc: "memchr", header: "<string.h>".}

var pos = 0
Expand All @@ -312,7 +312,7 @@ proc readLine*(f: File, line: var TaintedString): bool {.tags: [ReadIOEffect],

var fgetsSuccess = c_fgets(addr line.string[pos], sp.cint, f) != nil
if not fgetsSuccess: checkErr(f)
let m = c_memchr(addr line.string[pos], '\L'.ord, sp)
let m = c_memchr(addr line.string[pos], '\L'.ord, cast[csize_t](sp))
if m != nil:
# \l found: Could be our own or the one by fgets, in any case, we're done
var last = cast[ByteAddress](m) - cast[ByteAddress](addr line.string[0])
Expand Down Expand Up @@ -536,7 +536,7 @@ proc open*(f: var File, filename: string,
result = true
f = cast[File](p)
if bufSize > 0 and bufSize <= high(cint).int:
discard c_setvbuf(f, nil, IOFBF, bufSize.cint)
discard c_setvbuf(f, nil, IOFBF, cast[csize_t](bufSize))
elif bufSize == 0:
discard c_setvbuf(f, nil, IONBF, 0)

Expand Down Expand Up @@ -622,7 +622,7 @@ when declared(stdout):
when defined(windows):
writeWindows(stdout, s)
else:
discard c_fwrite(s.cstring, s.len, 1, stdout)
discard c_fwrite(s.cstring, cast[csize_t](s.len), 1, stdout)
const linefeed = "\n"
discard c_fwrite(linefeed.cstring, linefeed.len, 1, stdout)
discard c_fflush(stdout)
Expand Down
8 changes: 4 additions & 4 deletions lib/system/memory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ when useLibC:

proc nimCopyMem*(dest, source: pointer, size: Natural) {.nonReloadable, compilerproc, inline.} =
when useLibC:
c_memcpy(dest, source, size)
c_memcpy(dest, source, cast[csize_t](size))
else:
let d = cast[ptr UncheckedArray[byte]](dest)
let s = cast[ptr UncheckedArray[byte]](source)
Expand All @@ -21,7 +21,7 @@ proc nimCopyMem*(dest, source: pointer, size: Natural) {.nonReloadable, compiler

proc nimSetMem*(a: pointer, v: cint, size: Natural) {.nonReloadable, inline.} =
when useLibC:
c_memset(a, v, size)
c_memset(a, v, cast[csize_t](size))
else:
let a = cast[ptr UncheckedArray[byte]](a)
var i = 0
Expand All @@ -35,7 +35,7 @@ proc nimZeroMem*(p: pointer, size: Natural) {.compilerproc, nonReloadable, inlin

proc nimCmpMem*(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadable, inline.} =
when useLibC:
c_memcmp(a, b, size)
c_memcmp(a, b, cast[csize_t](size))
else:
let a = cast[ptr UncheckedArray[byte]](a)
let b = cast[ptr UncheckedArray[byte]](b)
Expand All @@ -45,7 +45,7 @@ proc nimCmpMem*(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadabl
if d != 0: return d
inc i

proc nimCStrLen*(a: cstring): csize {.compilerproc, nonReloadable, inline.} =
proc nimCStrLen*(a: cstring): csize_t {.compilerproc, nonReloadable, inline.} =
when useLibC:
c_strlen(a)
else:
Expand Down
10 changes: 5 additions & 5 deletions lib/system/osalloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,24 @@ elif defined(posix):
MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint
MAP_PRIVATE {.importc: "MAP_PRIVATE", header: "<sys/mman.h>".}: cint

proc mmap(adr: pointer, len: csize, prot, flags, fildes: cint,
proc mmap(adr: pointer, len: csize_t, prot, flags, fildes: cint,
off: int): pointer {.header: "<sys/mman.h>".}

proc munmap(adr: pointer, len: csize): cint {.header: "<sys/mman.h>".}
proc munmap(adr: pointer, len: csize_t): cint {.header: "<sys/mman.h>".}

proc osAllocPages(size: int): pointer {.inline.} =
result = mmap(nil, size, PROT_READ or PROT_WRITE,
result = mmap(nil, cast[csize_t](size), PROT_READ or PROT_WRITE,
MAP_PRIVATE or MAP_ANONYMOUS, -1, 0)
if result == nil or result == cast[pointer](-1):
raiseOutOfMem()

proc osTryAllocPages(size: int): pointer {.inline.} =
result = mmap(nil, size, PROT_READ or PROT_WRITE,
result = mmap(nil, cast[csize_t](size), PROT_READ or PROT_WRITE,
MAP_PRIVATE or MAP_ANONYMOUS, -1, 0)
if result == cast[pointer](-1): result = nil

proc osDeallocPages(p: pointer, size: int) {.inline.} =
when reallyOsDealloc: discard munmap(p, size)
when reallyOsDealloc: discard munmap(p, cast[csize_t](size))

elif defined(windows):
const
Expand Down
2 changes: 1 addition & 1 deletion lib/system/strmantle.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ proc cmpStrings(a, b: string): int {.inline, compilerproc.} =
let blen = b.len
let minlen = min(alen, blen)
if minlen > 0:
result = c_memcmp(unsafeAddr a[0], unsafeAddr b[0], minlen.csize)
result = c_memcmp(unsafeAddr a[0], unsafeAddr b[0], cast[csize_t](minlen))
if result == 0:
result = alen - blen
else:
Expand Down
2 changes: 1 addition & 1 deletion lib/system/threadlocalstorage.nim
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ else:
proc cpusetIncl(cpu: cint; s: var CpuSet) {.
importc: "CPU_SET", header: schedh.}

proc setAffinity(thread: SysThread; setsize: csize; s: var CpuSet) {.
proc setAffinity(thread: SysThread; setsize: csize_t; s: var CpuSet) {.
importc: "pthread_setaffinity_np", header: pthreadh.}


Expand Down
2 changes: 1 addition & 1 deletion lib/windows/winlean.nim
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ type
ai_family*: cint ## Address family of socket.
ai_socktype*: cint ## Socket type.
ai_protocol*: cint ## Protocol of socket.
ai_addrlen*: csize ## Length of socket address.
ai_addrlen*: csize_t ## Length of socket address.
ai_canonname*: cstring ## Canonical name of service location.
ai_addr*: ptr SockAddr ## Socket address of socket.
ai_next*: ptr AddrInfo ## Pointer to next in list.
Expand Down
8 changes: 4 additions & 4 deletions lib/wrappers/openssl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -685,9 +685,9 @@ type

{.push callconv:cdecl, dynlib:DLLUtilName.}
proc md5_Init*(c: var MD5_CTX): cint{.importc: "MD5_Init".}
proc md5_Update*(c: var MD5_CTX; data: pointer; len: csize): cint{.importc: "MD5_Update".}
proc md5_Update*(c: var MD5_CTX; data: pointer; len: csize_t): cint{.importc: "MD5_Update".}
proc md5_Final*(md: cstring; c: var MD5_CTX): cint{.importc: "MD5_Final".}
proc md5*(d: ptr cuchar; n: csize; md: ptr cuchar): ptr cuchar{.importc: "MD5".}
proc md5*(d: ptr cuchar; n: csize_t; md: ptr cuchar): ptr cuchar{.importc: "MD5".}
proc md5_Transform*(c: var MD5_CTX; b: ptr cuchar){.importc: "MD5_Transform".}
{.pop.}

Expand All @@ -712,7 +712,7 @@ proc md5_File*(file: string): string {.raises: [IOError,Exception].} =

discard md5_Init(ctx)
while(let bytes = f.readChars(buf, 0, sz); bytes > 0):
discard md5_Update(ctx, buf[0].addr, bytes)
discard md5_Update(ctx, buf[0].addr, cast[csize_t](bytes))

discard md5_Final(buf[0].addr, ctx)
f.close
Expand All @@ -731,7 +731,7 @@ proc md5_Str*(str: string): string =
var i = 0
while i < str.len:
let L = min(str.len - i, 512)
discard md5_Update(ctx, input[i].addr, L)
discard md5_Update(ctx, input[i].addr, cast[csize_t](L))
i += L

discard md5_Final(addr res, ctx)
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp/temitlist.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ main()
#bug #6837
type StdString {.importCpp: "std::string", header: "<string>", byref.} = object
proc initString(): StdString {.constructor, importCpp: "std::string(@)", header: "<string>".}
proc size(this: var StdString): csize {.importCpp: "size", header: "<string>".}
proc size(this: var StdString): csize_t {.importCpp: "size", header: "<string>".}

proc f(): csize =
proc f(): csize_t =
var myString: StdString = initString()
return myString.size()

Expand Down
4 changes: 2 additions & 2 deletions tests/cpp/tempty_generic_obj.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ proc bar(): VectorAlt[cstring] =
var x = foo()
var y = bar()

proc init[T; Self: Vector[T]](_: typedesc[Self], n: csize): Vector[T]
proc init[T; Self: Vector[T]](_: typedesc[Self], n: csize_t): Vector[T]
{.importcpp: "std::vector<'*0>(@)", header: "<vector>", constructor, nodecl.}
proc size[T](x: Vector[T]): csize
proc size[T](x: Vector[T]): csize_t
{.importcpp: "#.size()", header: "<vector>", nodecl.}

var z = Vector[int16].init(32)
Expand Down
Loading

0 comments on commit 99078d8

Please sign in to comment.