introduce csize_t instead of fixing csize (#12497)

This commit is contained in:
Arne Döring 2019-10-31 19:18:12 +01:00 • committed by Andreas Rumpf
commit 99078d80d7
18 changed files with 101 additions and 97 deletions

View file

@ -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.}
@ -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.}

View file

@ -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):
@ -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 {.
@ -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 {.
@ -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 {.
@ -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
@ -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])
@ -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)
@ -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)

View file

@ -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)
@ -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
@ -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)
@ -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:

View file

@ -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

View file

@ -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:

View file

@ -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.}