COMMON: Add strnlen for safer C string length reads

This API is intended for use in cases where C strings come
from untrusted sources like game files, where malformed data
missing the null terminator would cause strlen to read out of
bounds.
This commit is contained in:
Colin Snover 2016-11-26 12:56:25 -06:00 committed by Willem Jan Palenstijn
parent 28d2f1d0df
commit 9d3893459f
3 changed files with 41 additions and 0 deletions

View file

@ -942,6 +942,13 @@ size_t strlcat(char *dst, const char *src, size_t size) {
return dstLength + (src - srcStart);
}
size_t strnlen(const char *src, size_t maxSize) {
size_t counter = 0;
while (counter != maxSize && *src++)
++counter;
return counter;
}
} // End of namespace Common
// Portable implementation of stricmp / strcasecmp / strcmpi.