From e38f00cc342543973a5a1faaa2db067f63dcb9e4 Mon Sep 17 00:00:00 2001 From: ultramage Date: Thu, 1 Feb 2007 19:19:26 +0000 Subject: Added string function strnlen git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@9761 54d463be-8e91-2dee-dedb-b68131a5f0ec --- Changelog-Trunk.txt | 4 ++++ src/common/strlib.c | 10 ++++++++++ src/common/strlib.h | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/Changelog-Trunk.txt b/Changelog-Trunk.txt index 8897f4c72..0bbc4bea6 100644 --- a/Changelog-Trunk.txt +++ b/Changelog-Trunk.txt @@ -4,6 +4,10 @@ AS OF SVN REV. 5091, WE ARE NOW USING TRUNK. ALL UNTESTED BUGFIXES/FEATURES GO IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK. 2007/02/01 + * Added string function strnlen [ultramage] + - This thing is useful for removing potential out-of-bounds crashes. + Had to #ifdef it because some systems implement it, some don't provide + the header, and some don't have it at all. Adjust/improve as neccessary. * Corrected homunculus's aspd being halved after using some aspd adjusting skill. * Removed the log info message 'created homunc...', and the 'loaded homunc' diff --git a/src/common/strlib.c b/src/common/strlib.c index 60ea266db..440a07742 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -192,3 +192,13 @@ char *_strtok_r(char *s1, const char *s2, char **lasts) return ret; } #endif + +#if !defined(WIN32) || (defined(_MSC_VER) && _MSC_VER < 1400) +/* Find the length of STRING, but scan at most MAXLEN characters. + If no '\0' terminator is found in that many characters, return MAXLEN. */ +size_t strnlen (const char* string, size_t maxlen) +{ + const char* end = memchr (string, '\0', maxlen); + return end ? (size_t) (end - string) : maxlen; +} +#endif diff --git a/src/common/strlib.h b/src/common/strlib.h index d3b9a1dec..f242b5cc4 100644 --- a/src/common/strlib.h +++ b/src/common/strlib.h @@ -21,4 +21,9 @@ char *_strtok_r(char *s1, const char *s2, char **lasts); int remove_control_chars(unsigned char *); char *trim(char *str, const char *delim); const char *stristr(const char *haystack, const char *needle); + +#if !defined(WIN32) || (defined(_MSC_VER) && _MSC_VER < 1400) +size_t strnlen (const char* string, size_t maxlen); +#endif + #endif -- cgit v1.2.3-70-g09d2