cwText.cpp : Changed toText(char*,unsigned,const char*) to eliminate valgrind warnings.

This commit is contained in:
kevin 2023-05-25 16:06:23 -04:00
parent aefa0eb1d8
commit cee91b11a6

View File

@ -149,15 +149,19 @@ unsigned cw::toText( char* buf, unsigned bufN, double v )
unsigned cw::toText( char* buf, unsigned bufN, const char* v ) unsigned cw::toText( char* buf, unsigned bufN, const char* v )
{ {
assert( v != nullptr ); if( v == nullptr )
{
unsigned sn = strlen(v) + 1; cwLogError(kInvalidArgRC,"The source string in a call to 'toText()' was null.");
// bufN must be greater than the length of v[]
if( sn >= bufN )
return 0; return 0;
}
strncpy(buf,v,sn); unsigned i;
for(i=0; i<bufN; ++i)
{
buf[i] = v[i];
if(v[i]==0)
return i; // on success return the length of the string in buf[] and v[]
}
return sn-1; return 0; // if buf is too small return 0
} }