even more strict isNil handling for strings/seqs in order to detect bugs

This commit is contained in:
Araq 2018-08-22 12:35:46 +02:00
commit 64517445ea
36 changed files with 193 additions and 245 deletions

View file

@ -213,7 +213,7 @@ proc auxWriteStackTrace(f: PFrame; s: var seq[StackTraceEntry]) =
inc(i)
it = it.prev
var last = i-1
if s.isNil:
if s.len == 0:
s = newSeq[StackTraceEntry](i)
else:
last = s.len + i - 1
@ -361,10 +361,10 @@ proc raiseExceptionAux(e: ref Exception) =
else:
when hasSomeStackTrace:
var buf = newStringOfCap(2000)
if isNil(e.trace): rawWriteStackTrace(buf)
if e.trace.len == 0: rawWriteStackTrace(buf)
else: add(buf, $e.trace)
add(buf, "Error: unhandled exception: ")
if not isNil(e.msg): add(buf, e.msg)
add(buf, e.msg)
add(buf, " [")
add(buf, $e.name)
add(buf, "]\n")
@ -382,7 +382,7 @@ proc raiseExceptionAux(e: ref Exception) =
var buf: array[0..2000, char]
var L = 0
add(buf, "Error: unhandled exception: ")
if not isNil(e.msg): add(buf, e.msg)
add(buf, e.msg)
add(buf, " [")
xadd(buf, e.name, e.name.len)
add(buf, "]\n")
@ -397,7 +397,7 @@ proc raiseExceptionAux(e: ref Exception) =
proc raiseException(e: ref Exception, ename: cstring) {.compilerRtl.} =
if e.name.isNil: e.name = ename
when hasSomeStackTrace:
if e.trace.isNil:
if e.trace.len == 0:
rawWriteStackTrace(e.trace)
elif framePtr != nil:
e.trace.add reraisedFrom(reraisedFromBegin)
@ -427,7 +427,7 @@ proc getStackTrace(): string =
result = "No stack traceback available\n"
proc getStackTrace(e: ref Exception): string =
if not isNil(e) and not isNil(e.trace):
if not isNil(e):
result = $e.trace
else:
result = ""

View file

@ -266,7 +266,7 @@ when not defined(useNimRtl):
of tyChar: add result, reprChar(cast[ptr char](p)[])
of tyString:
let sp = cast[ptr string](p)
reprStrAux(result, if sp[].isNil: nil else: sp[].cstring, sp[].len)
reprStrAux(result, sp[].cstring, sp[].len)
of tyCString:
let cs = cast[ptr cstring](p)[]
if cs.isNil: add result, "nil"

View file

@ -232,10 +232,7 @@ proc reprAux(result: var string, p: pointer, typ: PNimType,
of tyString:
var fp: int
{. emit: "`fp` = `p`;\n" .}
if cast[string](fp).isNil:
add(result, "nil")
else:
add( result, reprStr(cast[string](p)) )
add( result, reprStr(cast[string](p)) )
of tyCString:
var fp: cstring
{. emit: "`fp` = `p`;\n" .}

View file

@ -144,13 +144,7 @@ proc getFileHandle*(f: File): FileHandle = c_fileno(f)
proc readLine(f: File, line: var TaintedString): bool =
var pos = 0
var sp: cint = 80
# Use the currently reserved space for a first try
if line.string.isNil:
line = TaintedString(newStringOfCap(80))
else:
when not defined(nimscript) and not defined(gcDestructors):
sp = cint(cast[PGenericSeq](line.string).space)
line.string.setLen(sp)
line.string.setLen(sp)
while true:
# memset to \L so that we can tell how far fgets wrote, even on EOF, where
# fgets doesn't append an \L

View file

@ -393,8 +393,9 @@ proc onThreadDestruction*(handler: proc () {.closure, gcsafe.}) =
## A thread is destructed when the ``.thread`` proc returns
## normally or when it raises an exception. Note that unhandled exceptions
## in a thread nevertheless cause the whole process to die.
if threadDestructionHandlers.isNil:
threadDestructionHandlers = @[]
when not defined(nimNoNilSeqs):
if threadDestructionHandlers.isNil:
threadDestructionHandlers = @[]
threadDestructionHandlers.add handler
template afterThreadRuns() =