[ttssh2-commit] [10494] _CopySerialList() と _AddValueToList() の文字列長の制限をなくした

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2023年 1月 16日 (月) 00:06:30 JST


Revision: 10494
          https://osdn.net/projects/ttssh2/scm/svn/commits/10494
Author:   zmatsuo
Date:     2023-01-16 00:06:29 +0900 (Mon, 16 Jan 2023)
Log Message:
-----------
_CopySerialList() と _AddValueToList() の文字列長の制限をなくした

- ログイン履歴の保存などに使用している関数

Modified Paths:
--------------
    trunk/teraterm/ttpset/ttset.c

-------------- next part --------------
Modified: trunk/teraterm/ttpset/ttset.c
===================================================================
--- trunk/teraterm/ttpset/ttset.c	2023-01-15 06:31:15 UTC (rev 10493)
+++ trunk/teraterm/ttpset/ttset.c	2023-01-15 15:06:29 UTC (rev 10494)
@@ -3256,8 +3256,6 @@
 							const wchar_t *key, int MaxList)
 {
 	int i, j;
-	wchar_t EntName[10], EntName2[10];
-	wchar_t TempHost[HostNameMaxLength + 1];
 
 	if (_wcsicmp(IniSrc, IniDest) == 0)
 		return;
@@ -3266,17 +3264,26 @@
 
 	i = j = 1;
 	do {
-		_snwprintf_s(EntName, _countof(EntName), _TRUNCATE, L"%s%i", key, i);
-		_snwprintf_s(EntName2, _countof(EntName2), _TRUNCATE, L"%s%i", key, j);
+		wchar_t *TempHost;
+		wchar_t *EntName;
+		wchar_t *EntName2;
 
+		aswprintf(&EntName, L"%s%i", key, i);
+		aswprintf(&EntName2, L"%s%i", key, j);
+
 		/* Get one hostname from file IniSrc */
-		GetPrivateProfileStringW(section, EntName, L"",
-		                        TempHost, _countof(TempHost), IniSrc);
+		hGetPrivateProfileStringW(section, EntName, L"", IniSrc, &TempHost);
+
 		/* Copy it to the file IniDest */
-		if (TempHost[0] != 0) {
+		if (TempHost != NULL && TempHost[0] != 0) {
 			WritePrivateProfileStringW(section, EntName2, TempHost, IniDest);
 			j++;
 		}
+
+		free(EntName);
+		free(EntName2);
+		free(TempHost);
+
 		i++;
 	}
 	while (i <= MaxList);
@@ -3288,56 +3295,88 @@
 void PASCAL _AddValueToList(const wchar_t *FName, const wchar_t *Host, const wchar_t *section,
 							const wchar_t *key, int MaxList)
 {
-	wchar_t *MemP;
-	wchar_t EntName[13];
-	int i, j, Len;
+	int ent_no;
+	int host_index;
 	BOOL Update;
+	wchar_t **hostnames;
 
 	if ((FName[0] == 0) || (Host[0] == 0))
 		return;
-	MemP = malloc((HostNameMaxLength + 1) * sizeof(wchar_t) * MaxList);
-	if (MemP != NULL) {
-		wcsncpy_s(MemP, (HostNameMaxLength + 1) * MaxList, Host, _TRUNCATE);
-		j = wcslen(Host) + 1;
-		i = 1;
-		Update = TRUE;
+
+	hostnames = (wchar_t **)calloc(sizeof(wchar_t), MaxList);
+	if (hostnames == NULL) {
+		return;
+	}
+
+	hostnames[0] = _wcsdup(Host);
+	ent_no = 1;
+	host_index = 1;
+	Update = TRUE;
+	do {
+		wchar_t *EntName;
+		wchar_t *hostname;
+
+		aswprintf(&EntName, L"%s%i", key, ent_no);
+		/* Get a hostname */
+		hGetPrivateProfileStringW(section, EntName, L"", FName, &hostname);
+		free(EntName);
+
+		if (hostname == NULL || hostname[0] == 0) {
+			// \x82\xA9\x82\xE7
+			free(hostname);
+			break;
+		}
+		else if (_wcsicmp(hostname, Host) == 0) {
+			// \x93\xAF\x82\xB6\x82̂\xAA\x82\xA0\x82\xE9\x82ƃ\x8A\x83X\x83g\x82ɉ\xC1\x82\xA6\x82Ȃ\xA2
+			if (host_index == 1) {
+				// \x90擪\x82\xAA\x93\xAF\x88ꂾ\x82\xC1\x82\xBD\x82\xE7\x8DX\x90V\x95s\x97v
+				Update = FALSE;
+			}
+			free(hostname);
+		}
+		else {
+			hostnames[host_index] = hostname;
+			host_index++;
+		}
+		ent_no++;
+	} while ((ent_no <= MaxList) && Update);
+
+	if (Update) {
+		// section\x82\xF0\x91S\x95\x94\x8F\xC1\x82\xB7
+		WritePrivateProfileStringW(section, NULL, NULL, FName);
+
+		ent_no = 1;
+		host_index = 0;
 		do {
-			_snwprintf_s(EntName, _countof(EntName), _TRUNCATE, L"%s%i", key, i);
+			wchar_t *EntName;
+			wchar_t *hostname;
 
-			/* Get a hostname */
-			GetPrivateProfileStringW(section, EntName, L"",
-									 &MemP[j], HostNameMaxLength + 1,
-									 FName);
-			Len = wcslen(&MemP[j]);
-			if (_wcsicmp(&MemP[j], Host) == 0) {
-				if (i == 1)
-					Update = FALSE;
+			hostname = hostnames[host_index];
+			if (hostname == NULL) {
+				break;
 			}
-			else if (Len > 0) {
-				j = j + Len + 1;
-			}
-			i++;
-		} while ((i <= MaxList) && Update);
+			aswprintf(&EntName, L"%s%i", key, ent_no);
+			WritePrivateProfileStringW(section, EntName, hostname, FName);
+			free(EntName);
+			host_index++;
+			ent_no++;
+		} while (ent_no <= MaxList);
 
-		if (Update) {
-			WritePrivateProfileStringW(section, NULL, NULL, FName);
+		/* update file */
+		WritePrivateProfileStringW(NULL, NULL, NULL, FName);
+	}
 
-			j = 0;
-			i = 1;
-			do {
-				_snwprintf_s(EntName, _countof(EntName), _TRUNCATE, L"%s%i", key, i);
-
-				if (MemP[j] != 0)
-					WritePrivateProfileStringW(section, EntName, &MemP[j],
-											   FName);
-				j = j + wcslen(&MemP[j]) + 1;
-				i++;
-			} while ((i <= MaxList) && (MemP[j] != 0));
-			/* update file */
-			WritePrivateProfileStringW(NULL, NULL, NULL, FName);
+	host_index = 0;
+	do {
+		wchar_t *hostname = hostnames[host_index];
+		if (hostname == NULL) {
+			break;
 		}
-		free(MemP);
-	}
+		free(hostname);
+		hostnames[host_index] = NULL;
+		host_index++;
+	} while (host_index < MaxList);
+	free(hostnames);
 }
 
  /* copy hostlist from source IniFile to dest IniFile */


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