From d1911d40d678071487d246bd3c86f978ca34e0c8 Mon Sep 17 00:00:00 2001 From: Frits van Bommel Date: Wed, 18 Mar 2009 15:33:19 +0100 Subject: [PATCH] Fix some issues with fawzi's patch. - It now actually compiles: - import stdc.stdio for string formatting functions) - remove extra '{' - Use snprintf() instead of sprintf(). - Use return value from snprintf instead of strlen(). - Don't print the filename in Exception.writeOut() if it has zero length and the line number is 0 (It would previously only skip these if the filename was null, but not if it was a different empty string) - Ignore empty filename + line number 0 in FrameInfo.writeOut() as well. --- runtime/internal/genobj.d | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/runtime/internal/genobj.d b/runtime/internal/genobj.d index 5ad72107..600feb50 100644 --- a/runtime/internal/genobj.d +++ b/runtime/internal/genobj.d @@ -45,7 +45,7 @@ private import tango.stdc.string; // : memcmp, memcpy, memmove; import tango.stdc.stdlib; // : calloc, realloc, free; import util.string; - debug(PRINTF) import tango.stdc.stdio; // : printf; + import tango.stdc.stdio; // : printf, snprintf; extern (C) void onOutOfMemoryError(); extern (C) Object _d_allocclass(ClassInfo ci); @@ -915,13 +915,15 @@ class Exception : Object void writeOut(void delegate(char[])sink){ char[25] buf; sink(func); - sprintf(buf.ptr,"@%zx ",address); - sink(buf[0..strlen(buf.ptr)]); - sprintf(buf.ptr," %+td ",address); - sink(buf[0..strlen(buf.ptr)]); - sink(file); - sprintf(buf.ptr,":%ld",line); - sink(buf[0..strlen(buf.ptr)]); + auto len = snprintf(buf.ptr,buf.length,"@%zx ",address); + sink(buf[0..len]); + len = snprintf(buf.ptr,buf.length," %+td ",address); + sink(buf[0..len]); + if (file.length != 0 || line) { + sink(file); + len = snprintf(buf.ptr,buf.length,":%ld",line); + sink(buf[0..len]); + } } } interface TraceInfo @@ -947,7 +949,7 @@ class Exception : Object this( char[] msg, Exception next=null ) { - this(msg,"",0,next,rt_createTraceContext(null)); + this(msg,null,0,next,rt_createTraceContext(null)); } this( char[] msg, char[] file, long line, Exception next=null ) @@ -961,15 +963,15 @@ class Exception : Object } void writeOut(void delegate(char[])sink){ - if (file) + if (file.length != 0 || line) { char[25]buf; sink(this.classinfo.name); sink("@"); sink(file); sink("("); - sprintf(buf.ptr,"%ld",line); - sink(buf[0..strlen(buf.ptr)]); + auto len = snprintf(buf.ptr,buf.length,"%ld",line); + sink(buf[0..len]); sink("): "); sink(toString()); sink("\n"); @@ -1027,7 +1029,7 @@ extern (C) void rt_setTraceHandler( TraceHandler h ) * An object describing the current calling context or null if no handler is * supplied. */ -extern(C) Exception.TraceInfo rt_createTraceContext( void* ptr ){ +extern(C) Exception.TraceInfo rt_createTraceContext( void* ptr ) { if( traceHandler is null ) return null;