aboutsummaryrefslogtreecommitdiff
path: root/common/str.h
diff options
context:
space:
mode:
authorColin Snover2016-11-26 12:56:25 -0600
committerWillem Jan Palenstijn2017-01-05 22:07:24 +0100
commit9d3893459f34b6ada2dad2d9d27216c774a7c4bd (patch)
treecf09a5960aa37cff29666211da50fd488c5f05e9 /common/str.h
parent28d2f1d0df4a62c622a714e82ec6a7ab04967730 (diff)
downloadscummvm-rg350-9d3893459f34b6ada2dad2d9d27216c774a7c4bd.tar.gz
scummvm-rg350-9d3893459f34b6ada2dad2d9d27216c774a7c4bd.tar.bz2
scummvm-rg350-9d3893459f34b6ada2dad2d9d27216c774a7c4bd.zip
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.
Diffstat (limited to 'common/str.h')
-rw-r--r--common/str.h11
1 files changed, 11 insertions, 0 deletions
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.