Skip to content

Instantly share code, notes, and snippets.

@fstamour
Created May 7, 2014 03:19
Show Gist options
  • Save fstamour/0a22e9f0c99d6182675e to your computer and use it in GitHub Desktop.
Save fstamour/0a22e9f0c99d6182675e to your computer and use it in GitHub Desktop.
Printing null-terminated string without buffer-overflows
// @todo I'm sure it could be more efficient if we used a "safe" strlen and
// write the string into the stream using "ostream.write( const char*, size_t size)"
std::ostream& safePrint( std::ostream& out, const char* szStr, size_t maxSize ) {
for( size_t i = 0; i < maxSize && szStr[i] != '\0'; ++i) {
out.put(szStr[i]);
}
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment