From b826bc2c54dbe6a2ad0f204f36f82bf116d8e663 Mon Sep 17 00:00:00 2001 From: ultramage Date: Sat, 22 Sep 2007 11:02:26 +0000 Subject: * Added 'safestrnlen' to prevent null pointer crashes * Fixed global chat logging always crashing on a null pointer * Applied changes to clif_parse_globalmessage() from my WiP code - clearer processing of the individual packet components - proper code ordering, some more integrity checks - fixes to some poorly chosen ShowWarning() format strings - global chat logging no longer logs the entire string (w/ player name) git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@11271 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/mapindex.c | 2 +- src/common/strlib.c | 6 ++++++ src/common/strlib.h | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/mapindex.c b/src/common/mapindex.c index f2adc833c..2fa8c9fb8 100644 --- a/src/common/mapindex.c +++ b/src/common/mapindex.c @@ -52,7 +52,7 @@ const char* mapindex_getmapname_ext(const char* string, char* output) static char buf[MAP_NAME_LENGTH_EXT]; char* dest = (output != NULL) ? output : buf; - size_t len = strnlen(string, MAP_NAME_LENGTH); + size_t len = safestrnlen(string, MAP_NAME_LENGTH); if (len == MAP_NAME_LENGTH) { ShowWarning("(mapindex_normalize_name) Map name '%*s' is too long!", 2*MAP_NAME_LENGTH, string); len--; diff --git a/src/common/strlib.c b/src/common/strlib.c index 4f204a768..c0a6238f9 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -310,6 +310,12 @@ char* safestrncpy(char* dst, const char* src, size_t n) return ret; } +/// doesn't crash on null pointer +size_t safestrnlen(const char* string, size_t maxlen) +{ + return ( string != NULL ) ? strnlen(string, maxlen) : 0; +} + ///////////////////////////////////////////////////////////////////// // StringBuf - dynamic string diff --git a/src/common/strlib.h b/src/common/strlib.h index ee473f466..fd24b4b24 100644 --- a/src/common/strlib.h +++ b/src/common/strlib.h @@ -34,6 +34,9 @@ int config_switch(const char* str); /// always nul-terminates the string char* safestrncpy(char* dst, const char* src, size_t n); +/// doesn't crash on null pointer +size_t safestrnlen(const char* string, size_t maxlen); + /// StringBuf - dynamic string struct StringBuf { -- cgit v1.2.3-60-g2f50