Created
May 7, 2014 03:19
-
-
Save fstamour/0a22e9f0c99d6182675e to your computer and use it in GitHub Desktop.
Printing null-terminated string without buffer-overflows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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