summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlavioJS <FlavioJS@54d463be-8e91-2dee-dedb-b68131a5f0ec>2009-04-20 17:28:48 +0000
committerFlavioJS <FlavioJS@54d463be-8e91-2dee-dedb-b68131a5f0ec>2009-04-20 17:28:48 +0000
commit40c4dff74d460624e3de1cc0d49ce6df3ccce613 (patch)
treedf99dbbcf0bd7c1d13d541590523250f534e8308
parent3341f0175012e8f96bea68355f5323b25a7e6ce8 (diff)
downloadhercules-40c4dff74d460624e3de1cc0d49ce6df3ccce613.tar.gz
hercules-40c4dff74d460624e3de1cc0d49ce6df3ccce613.tar.bz2
hercules-40c4dff74d460624e3de1cc0d49ce6df3ccce613.tar.xz
hercules-40c4dff74d460624e3de1cc0d49ce6df3ccce613.zip
* Fixed safestrncpy trashing the memory before dst when n == 0. (since r10667, bugreport:2996)
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@13681 54d463be-8e91-2dee-dedb-b68131a5f0ec
-rw-r--r--Changelog-Trunk.txt2
-rw-r--r--src/common/strlib.c23
-rw-r--r--src/common/strlib.h2
3 files changed, 20 insertions, 7 deletions
diff --git a/Changelog-Trunk.txt b/Changelog-Trunk.txt
index 4c01252e0..ff85e5425 100644
--- a/Changelog-Trunk.txt
+++ b/Changelog-Trunk.txt
@@ -3,6 +3,8 @@ Date Added
AS OF SVN REV. 5091, WE ARE NOW USING TRUNK. ALL UNTESTED BUGFIXES/FEATURES GO INTO TRUNK.
IF YOU HAVE A WORKING AND TESTED BUGFIX PUT IT INTO STABLE AS WELL AS TRUNK.
+2009/04/20
+ * Fixed safestrncpy trashing the memory before dst when n == 0. (since r10667) [FlavioJS]
2009/04/15
* Monster Spiral Pierce is now a ranged misc attack [Playtester]
- it always gets blocked by Pneuma, but never by Safety Wall
diff --git a/src/common/strlib.c b/src/common/strlib.c
index 9b97aabba..c1d26622a 100644
--- a/src/common/strlib.c
+++ b/src/common/strlib.c
@@ -301,14 +301,25 @@ int config_switch(const char* str)
return (int)strtol(str, NULL, 0);
}
-/// always nul-terminates the string
+/// strncpy that always nul-terminates the string
char* safestrncpy(char* dst, const char* src, size_t n)
{
- char* ret;
- ret = strncpy(dst, src, n);
- if( ret != NULL )
- ret[n - 1] = '\0';
- return ret;
+ if( n > 0 )
+ {
+ char* d = dst;
+ const char* s = src;
+ d[--n] = '\0';/* nul-terminate string */
+ for( ; n > 0; --n )
+ {
+ if( (*d++ = *s++) == '\0' )
+ {/* nul-pad remaining bytes */
+ while( --n > 0 )
+ *d++ = '\0';
+ break;
+ }
+ }
+ }
+ return dst;
}
/// doesn't crash on null pointer
diff --git a/src/common/strlib.h b/src/common/strlib.h
index 1ba26ca4c..42034e4c1 100644
--- a/src/common/strlib.h
+++ b/src/common/strlib.h
@@ -31,7 +31,7 @@ size_t strnlen (const char* string, size_t maxlen);
int e_mail_check(char* email);
int config_switch(const char* str);
-/// always nul-terminates the string
+/// strncpy that always nul-terminates the string
char* safestrncpy(char* dst, const char* src, size_t n);
/// doesn't crash on null pointer