13
13
import std/ private/ miscdollars
14
14
import stacktraces
15
15
16
+ const noStacktraceAvailable = " No stack traceback available\n "
17
+
16
18
var
17
19
errorMessageWriter* : (proc (msg: string ) {.tags : [WriteIOEffect ], benign ,
18
20
nimcall .})
36
38
proc writeToStdErr (msg: cstring , length: int ) =
37
39
discard MessageBoxA (nil , msg, nil , 0 )
38
40
41
+ proc writeToStdErr (msg: string ) {.inline .} =
42
+ # fix bug #13115: handles correctly '\0' unlike default implicit conversion to cstring
43
+ writeToStdErr (msg.cstring , msg.len)
44
+
39
45
proc showErrorMessage (data: cstring , length: int ) {.gcsafe , raises : [].} =
40
46
var toWrite = true
41
47
if errorMessageWriter != nil :
@@ -51,6 +57,9 @@ proc showErrorMessage(data: cstring, length: int) {.gcsafe, raises: [].} =
51
57
else :
52
58
writeToStdErr (data, length)
53
59
60
+ proc showErrorMessage2 (data: string ) {.inline .} =
61
+ showErrorMessage (data.cstring , data.len)
62
+
54
63
proc chckIndx (i, a, b: int ): int {.inline , compilerproc , benign .}
55
64
proc chckRange (i, a, b: int ): int {.inline , compilerproc , benign .}
56
65
proc chckRangeF (x, a, b: float ): float {.inline , compilerproc , benign .}
@@ -123,11 +132,11 @@ proc popSafePoint {.compilerRtl, inl.} =
123
132
proc pushCurrentException (e: sink (ref Exception )) {.compilerRtl , inl .} =
124
133
e.up = currException
125
134
currException = e
126
- # showErrorMessage "A"
135
+ # showErrorMessage2 "A"
127
136
128
137
proc popCurrentException {.compilerRtl , inl .} =
129
138
currException = currException.up
130
- # showErrorMessage "B"
139
+ # showErrorMessage2 "B"
131
140
132
141
proc popCurrentExceptionEx (id: uint ) {.compilerRtl .} =
133
142
discard " only for bootstrapping compatbility"
@@ -305,15 +314,15 @@ when hasSomeStackTrace:
305
314
auxWriteStackTraceWithOverride (s)
306
315
elif NimStackTrace :
307
316
if framePtr == nil :
308
- add (s, " No stack traceback available \n " )
317
+ add (s, noStacktraceAvailable )
309
318
else :
310
319
add (s, " Traceback (most recent call last)\n " )
311
320
auxWriteStackTrace (framePtr, s)
312
321
elif defined (nativeStackTrace) and nativeStackTraceSupported:
313
322
add (s, " Traceback from system (most recent call last)\n " )
314
323
auxWriteStackTraceWithBacktrace (s)
315
324
else :
316
- add (s, " No stack traceback available \n " )
325
+ add (s, noStacktraceAvailable )
317
326
318
327
proc rawWriteStackTrace (s: var seq [StackTraceEntry ]) =
319
328
when defined (nimStackTraceOverride):
@@ -363,7 +372,7 @@ proc reportUnhandledErrorAux(e: ref Exception) {.nodestroy.} =
363
372
if onUnhandledException != nil :
364
373
onUnhandledException (buf)
365
374
else :
366
- showErrorMessage (buf, buf.len )
375
+ showErrorMessage2 (buf)
367
376
`=destroy` (buf)
368
377
else :
369
378
# ugly, but avoids heap allocations :-)
@@ -504,16 +513,16 @@ proc writeStackTrace() =
504
513
when hasSomeStackTrace:
505
514
var s = " "
506
515
rawWriteStackTrace (s)
507
- cast [proc (s: cstring , length: int ) {.noSideEffect , tags : [], nimcall , raises : [].}](showErrorMessage)(s, s.len)
508
516
else :
509
- cast [proc (s: cstring , length: int ) {.noSideEffect , tags : [], nimcall , raises : [].}](showErrorMessage)(" No stack traceback available\n " , 32 )
517
+ let s = noStacktraceAvailable
518
+ cast [proc (s: string ) {.noSideEffect , tags : [], nimcall , raises : [].}](showErrorMessage2)(s)
510
519
511
520
proc getStackTrace (): string =
512
521
when hasSomeStackTrace:
513
522
result = " "
514
523
rawWriteStackTrace (result )
515
524
else :
516
- result = " No stack traceback available \n "
525
+ result = noStacktraceAvailable
517
526
518
527
proc getStackTrace (e: ref Exception ): string =
519
528
if not isNil (e):
@@ -543,7 +552,7 @@ proc callDepthLimitReached() {.noinline.} =
543
552
$ nimCallDepthLimit & " function calls). You can change it with " &
544
553
" -d:nimCallDepthLimit=<int> but really try to avoid deep " &
545
554
" recursions instead.\n "
546
- showErrorMessage (msg, msg.len )
555
+ showErrorMessage2 (msg)
547
556
quit (1 )
548
557
549
558
proc nimFrame (s: PFrame ) {.compilerRtl , inl , raises : [].} =
@@ -627,13 +636,16 @@ when not defined(noSignalHandler) and not defined(useNimRtl):
627
636
var buf = newStringOfCap (2000 )
628
637
rawWriteStackTrace (buf)
629
638
processSignal (sign, buf.add) # nice hu? currying a la Nim :-)
630
- showErrorMessage (buf, buf.len )
639
+ showErrorMessage2 (buf)
631
640
when not usesDestructors: GC_enable ()
632
641
else :
633
642
var msg: cstring
634
643
template asgn (y) =
635
644
msg = y
636
645
processSignal (sign, asgn)
646
+ # xxx use string for msg instead of cstring, and here use showErrorMessage2(msg)
647
+ # unless there's a good reason to use cstring in signal handler to avoid
648
+ # using gc?
637
649
showErrorMessage (msg, msg.len)
638
650
quit (1 ) # always quit when SIGABRT
639
651
0 commit comments