In terms of Thread Safety and general security, would the following code have anything wrong with it?
std::string & toHexString( const uint8_t *buf, uint32_t size, std::string &out )
{
// modify 'out'
return out;
}
#ifndef TOHEXSTR
#define TOHEXSTR( x, y, ) ( toHexString( x, y, std::string() ) ).c_str()
#endif
The way this will be used is to print debug statements:
printf( "Byte buffer contents: [%s].", TOHEXSTR( buf, buf_size ) );
If there is a problem with this implementation, what should be changed?
Thanks.
Do not use a reference parameter to store output.
Simply create a local std::string inside the function and return it by value.
Due to compiler techniques such as Return Value Optimization, this should have similar performance, but much better semantics(no need for dummy extra parameter).
As far as thread safety goes the function is probably fine. You only need to worry about thread safety when data is shared between threads, and this function should not share any data.