diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/str.cpp | 7 | ||||
-rw-r--r-- | common/str.h | 11 |
2 files changed, 18 insertions, 0 deletions
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. diff --git a/common/str.h b/common/str.h index d55ba072a9..ba1e0b8341 100644 --- a/common/str.h +++ b/common/str.h @@ -445,6 +445,17 @@ size_t strlcpy(char *dst, const char *src, size_t size); size_t strlcat(char *dst, const char *src, size_t size); /** + * Determine the length of a string up to a maximum of `maxSize` characters. + * This should be used instead of `strlen` when reading the length of a C string + * from potentially unsafe or corrupt sources, like game assets. + * + * @param src The source string. + * @param maxSize The maximum size of the string. + * @return The length of the string. + */ +size_t strnlen(const char *src, size_t maxSize); + +/** * Convenience wrapper for tag2string which "returns" a C string. * Note: It is *NOT* safe to do anything with the return value other than directly * copying or printing it. |