[Ttssh2-commit] [5069] strreplaceで、置き換え対象にmatchstr , grpmatchstr[1-9] を指定したり、indexに負の値を指定すると落ちる場合があるのを修正。

Back to archive index

svnno****@sourc***** svnno****@sourc*****
2012年 11月 21日 (水) 03:45:38 JST


Revision: 5069
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/5069
Author:   doda
Date:     2012-11-21 03:45:38 +0900 (Wed, 21 Nov 2012)
Log Message:
-----------
strreplaceで、置き換え対象にmatchstr, grpmatchstr[1-9] を指定したり、indexに負の値を指定すると落ちる場合があるのを修正。

Modified Paths:
--------------
    trunk/teraterm/ttpmacro/ttl.c

-------------- next part --------------
Modified: trunk/teraterm/ttpmacro/ttl.c
===================================================================
--- trunk/teraterm/ttpmacro/ttl.c	2012-11-19 07:42:36 UTC (rev 5068)
+++ trunk/teraterm/ttpmacro/ttl.c	2012-11-20 18:45:38 UTC (rev 5069)
@@ -4031,15 +4031,13 @@
 	TVarId VarId;
 	TStrVal oldstr;
 	TStrVal newstr;
+	TStrVal tmpstr;
 	char *srcptr, *matchptr;
 	char *p;
 	int srclen, oldlen, matchlen;
 	int pos, ret;
 	int result = 0;
 
-	memset(oldstr, 0, MaxStrLen);
-	memset(newstr, 0, MaxStrLen);
-
 	Err = 0;
 	GetStrVar(&VarId,&Err);
 	GetIntVal(&pos,&Err);
@@ -4052,29 +4050,18 @@
 	srcptr = StrVarPtr(VarId);
 	srclen = strlen(srcptr);
 
-	if (pos > srclen) {
+	if (pos > srclen || pos <= 0) {
 		result = 0;
 		goto error;
 	}
+	pos--;
 
+	strncpy_s(tmpstr, MaxStrLen, srcptr, _TRUNCATE);
+
 	oldlen = strlen(oldstr);
 
 	// strptr\x95\xB6\x8E\x9A\x97\xF1\x82\xCC pos \x95\xB6\x8E\x9A\x96ڈȍ~\x82ɂ\xA8\x82\xA2\x82āAoldstr \x82\xF0\x92T\x82\xB7\x81B
-#if 0
-	p = strstr(srcptr + (pos - 1), oldstr);
-	if (p == NULL) {
-		// \x8C\xA9\x82‚\xA9\x82\xE7\x82Ȃ\xA9\x82\xC1\x82\xBD\x8Fꍇ\x82́A"0"\x82Ŗ߂\xE9\x81B
-		result = 0;
-		goto error;
-	}
-
-	// \x82܂\xB8\x82\xCD oldstr \x82\xF0\x8D폜\x82\xB7\x82\xE9
-	remove_string(srcptr, p - srcptr + 1, oldlen);
-
-	// newstr \x82\xF0\x91}\x93\xFC\x82\xB7\x82\xE9
-	insert_string(srcptr, p - srcptr + 1, newstr);
-#else
-	p = srcptr + (pos - 1);
+	p = tmpstr + pos;
 	ret = FindRegexStringOne(oldstr, oldlen, p, strlen(p));
 	// FindRegexStringOne\x82̒\x86\x82\xC5UnlockVar()\x82\xB3\x82\xEA\x82Ă\xB5\x82܂\xA4\x82̂ŁALockVar()\x82\xB5\x82Ȃ\xA8\x82\xB7\x81B
 	LockVar();
@@ -4083,6 +4070,7 @@
 		result = 0;
 		goto error;
 	}
+	ret--;
 
 	if (CheckVar("matchstr",&VarType,&VarId) &&
 		(VarType==TypString)) {
@@ -4093,13 +4081,10 @@
 		goto error;
 	}
 
-	// \x82܂\xB8\x82\xCD oldstr \x82\xF0\x8D폜\x82\xB7\x82\xE9
-	remove_string(srcptr, (pos - 1) + ret, matchlen);
+	strncpy_s(srcptr, MaxStrLen, tmpstr, pos + ret);
+	strncat_s(srcptr, MaxStrLen, newstr, _TRUNCATE);
+	strncat_s(srcptr, MaxStrLen, tmpstr + pos + ret + matchlen, _TRUNCATE);
 
-	// newstr \x82\xF0\x91}\x93\xFC\x82\xB7\x82\xE9
-	insert_string(srcptr, (pos - 1) + ret, newstr);
-#endif
-
 	result = 1;
 
 error:



Ttssh2-commit メーリングリストの案内
Back to archive index