From 9d3893459f34b6ada2dad2d9d27216c774a7c4bd Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Sat, 26 Nov 2016 12:56:25 -0600 Subject: 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. --- common/str.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'common/str.cpp') diff --git a/common/str.cpp b/common/str.cpp index 90bd539790..3a0fd6a08e 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -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. -- cgit v1.2.3