From 5e93cb4e03c6e3a6cddf28f5a263519e4cd6e7b1 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 27 Dec 2021 20:28:09 -0500 Subject: [PATCH] cwObject.cpp : Added use of _udiff() to avoid unsigned subtractions from wrapping around 0. --- cwObject.cpp | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/cwObject.cpp b/cwObject.cpp index aedb5a7..f0c671e 100644 --- a/cwObject.cpp +++ b/cwObject.cpp @@ -42,6 +42,11 @@ namespace cw { lex::kErrorLexTId,""} }; + unsigned _udiff( unsigned n, unsigned i ) + { + return i>=n ? 0 : n-i; + } + unsigned _lexSegmentedIdMatcher( const char* cp, unsigned cn ) { unsigned i = 0; @@ -75,22 +80,6 @@ namespace cw const char* _objTypeIdToLabel( objTypeId_t tid ); - - void _objTypeNullToString( object_t* o, char* buf, unsigned bufN ) { snprintf(buf,bufN,"%s","NULL"); } - void _objTypeCharToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.c,buf,bufN,"%c"); } - void _objTypeInt8ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i8,buf,bufN,"%i"); } - void _objTypeUInt8ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u8,buf,bufN,"%i"); } - void _objTypeInt16ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i16,buf,bufN,"%i"); } - void _objTypeUInt16ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u16,buf,bufN,"%i"); } - void _objTypeInt32ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i32,buf,bufN,"%i"); } - void _objTypeUInt32ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u32,buf,bufN,"%i"); } - void _objTypeInt64ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.i64,buf,bufN,"%i"); } - void _objTypeUInt64ToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.u64,buf,bufN,"%i"); } - void _objTypeBoolToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.b,buf,bufN,"%i"); } - void _objTypeFloatToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.f,buf,bufN,"%f"); } - void _objTypeDoubleToString( object_t* o, char* buf, unsigned bufN ) { number_to_string(o->u.d,buf,bufN,"%f"); } - void _objTypeStringToString( object_t* o, char* buf, unsigned bufN ) { snprintf(buf,bufN,"%s",o->u.str); } - rc_t _objTypeValueFromChar( const object_t* o, unsigned tid, void* dst ) { return getObjectValue(o->u.c,tid, dst,o->type->label); } rc_t _objTypeValueFromInt8( const object_t* o, unsigned tid, void* dst ) { return getObjectValue(o->u.i8,tid, dst,o->type->label); } @@ -263,17 +252,23 @@ namespace cw unsigned _objTypeToStringString( const object_t* o, char* buf, unsigned n ) { unsigned i = snprintf(buf,n,"\""); + const char* str = o->u.str; + + if( str == nullptr ) + { + cwLogWarning("Unexpected empty string while generating text output."); + } - i += toText(buf+i,n-i,o->u.str); + i += toText(buf+i,_udiff(n,i),str); - return i + snprintf(buf+i,n-i,"\""); + return i + snprintf(buf+i,_udiff(n,i),"\""); } unsigned _objTypeToStringPair( const object_t* o, char* buf, unsigned n ) { unsigned i = o->u.children->type->to_string(o->u.children,buf,n); - i += snprintf(buf+i,n-i," : "); - return i + o->u.children->sibling->type->to_string(o->u.children->sibling,buf+i,n-i); + i += snprintf(buf+i,_udiff(n,i)," : "); + return i + o->u.children->sibling->type->to_string(o->u.children->sibling,buf+i,_udiff(n,i)); } unsigned _objTypeToStringList( const object_t* o, char* buf, unsigned n ) @@ -283,14 +278,14 @@ namespace cw for(const object_t* ch=o->u.children; ch!=nullptr; ch=ch->sibling) { - i += ch->type->to_string(ch,buf+i,n-i); + i += ch->type->to_string(ch,buf+i,_udiff(n,i)); if( ch->sibling != nullptr ) - i += snprintf(buf+i,n-i,", "); + i += snprintf(buf+i,_udiff(n,i),", "); } - i += snprintf(buf+i,n-i," ] "); + i += snprintf(buf+i,_udiff(n,i)," ] "); return i; } @@ -302,13 +297,13 @@ namespace cw for(const object_t* ch=o->u.children; ch!=nullptr; ch=ch->sibling) { - i += ch->type->to_string(ch,buf+i,n-i); + i += ch->type->to_string(ch,buf+i,_udiff(n,i)); if( ch->sibling != nullptr ) - i += snprintf(buf+i,n-i,", "); + i += snprintf(buf+i,_udiff(n,i),", "); } - i += snprintf(buf+i,n-i," } "); + i += snprintf(buf+i,_udiff(n,i)," } "); return i; }