[Ttssh2-commit] [9200] DetectComPorts()でバッファオーバーランしないよう修正

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2021年 3月 19日 (金) 00:27:15 JST


Revision: 9200
          https://osdn.net/projects/ttssh2/scm/svn/commits/9200
Author:   zmatsuo
Date:     2021-03-19 00:27:14 +0900 (Fri, 19 Mar 2021)
Log Message:
-----------
DetectComPorts()でバッファオーバーランしないよう修正

- テーブルサイズ(4096)よりcomポート数が多い場合オーバーランしていた
- ttpcmn/ttcmn_cominfo.h を追加

Modified Paths:
--------------
    trunk/teraterm/common/comportinfo.cpp
    trunk/teraterm/common/ttcommon.h
    trunk/teraterm/ttpcmn/ttcmn_cominfo.c
    trunk/tools/lscom/CMakeLists.txt
    trunk/tools/lscom/main.cpp

Added Paths:
-----------
    trunk/teraterm/ttpcmn/ttcmn_cominfo.h

-------------- next part --------------
Modified: trunk/teraterm/common/comportinfo.cpp
===================================================================
--- trunk/teraterm/common/comportinfo.cpp	2021-03-17 14:36:47 UTC (rev 9199)
+++ trunk/teraterm/common/comportinfo.cpp	2021-03-18 15:27:14 UTC (rev 9200)
@@ -431,9 +431,9 @@
  */
 ComPortInfo_t *ComPortInfoGet(int *count, const char *lang)
 {
-	OSVERSIONINFO osvi;
+	OSVERSIONINFOA osvi;
 	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-	GetVersionEx(&osvi);
+	GetVersionExA(&osvi);
 	bool is_setupapi_supported = true;
 	if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) {
 		// Windows 95

Modified: trunk/teraterm/common/ttcommon.h
===================================================================
--- trunk/teraterm/common/ttcommon.h	2021-03-17 14:36:47 UTC (rev 9199)
+++ trunk/teraterm/common/ttcommon.h	2021-03-18 15:27:14 UTC (rev 9200)
@@ -38,8 +38,6 @@
 #define DllExport __declspec(dllimport)
 #endif
 
-DllExport int PASCAL DetectComPorts(LPWORD ComPortTable, int ComPortMax, char **ComPortDesc);
-DllExport int PASCAL CheckComPort(WORD ComPort);
 DllExport void PASCAL CopyShmemToTTSet(PTTSet ts);
 DllExport void PASCAL CopyTTSetToShmem(PTTSet ts);
 DllExport BOOL PASCAL StartTeraTerm(PTTSet ts);
@@ -102,9 +100,9 @@
 #define NotifyWarnMessageW(cv, msg, title) NotifyMessageW(cv, msg, title, 2)
 #define NotifyErrorMessageW(cv, msg, title) NotifyMessageW(cv, msg, title, 3)
 
-#include "../ttpcmn/language.h"
-
 #ifdef __cplusplus
 }
 #endif
 
+#include "../ttpcmn/language.h"
+#include "../ttpcmn/ttcmn_cominfo.h"

Modified: trunk/teraterm/ttpcmn/ttcmn_cominfo.c
===================================================================
--- trunk/teraterm/ttpcmn/ttcmn_cominfo.c	2021-03-17 14:36:47 UTC (rev 9199)
+++ trunk/teraterm/ttpcmn/ttcmn_cominfo.c	2021-03-18 15:27:14 UTC (rev 9200)
@@ -31,6 +31,9 @@
 #include "codeconv.h"
 #include "comportinfo.h"
 
+#define DllExport __declspec(dllexport)
+#include "ttcmn_cominfo.h"
+
 /**
  *	COM\x83|\x81[\x83g\x82\xF0\x97\xF1\x8B\x93
  *
@@ -49,6 +52,13 @@
 {
 	int count;
 	int i;
+
+	// \x88ȑO\x8Am\x95ۂ\xB5\x82\xBD\x95\xAA\x82\xF0\x8AJ\x95\xFA\x82\xB7\x82\xE9
+	for (i = 0; i < ComPortMax; i++) {
+		free(ComPortDesc[i]);
+		ComPortDesc[i] = NULL;
+	}
+
 	ComPortInfo_t *port_info = ComPortInfoGet(&count, NULL);
 	const ComPortInfo_t *p = port_info;
 	for (i = 0; i < count; i++) {
@@ -55,6 +65,10 @@
 		ComPortTable[i] = p->port_no;
 		ComPortDesc[i] = ToCharW(p->friendly_name);
 		p++;
+		if (i == ComPortMax - 1) {
+			// \x83e\x81[\x83u\x83\x8B\x82\xF0\x82\xA0\x82ӂ\xEA\x82\xE9
+			break;
+		}
 	}
 
 	ComPortInfoFree(port_info, count);

Copied: trunk/teraterm/ttpcmn/ttcmn_cominfo.h (from rev 9198, trunk/teraterm/ttpcmn/ttcmn_cominfo.c)
===================================================================
--- trunk/teraterm/ttpcmn/ttcmn_cominfo.h	                        (rev 0)
+++ trunk/teraterm/ttpcmn/ttcmn_cominfo.h	2021-03-18 15:27:14 UTC (rev 9200)
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2021- TeraTerm Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <windows.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if !defined(DllExport)
+#define DllExport __declspec(dllimport)
+#endif
+
+DllExport int WINAPI DetectComPorts(LPWORD ComPortTable, int ComPortMax, char **ComPortDesc);
+DllExport int WINAPI CheckComPort(WORD ComPort);
+
+#ifdef __cplusplus
+}
+#endif

Modified: trunk/tools/lscom/CMakeLists.txt
===================================================================
--- trunk/tools/lscom/CMakeLists.txt	2021-03-17 14:36:47 UTC (rev 9199)
+++ trunk/tools/lscom/CMakeLists.txt	2021-03-18 15:27:14 UTC (rev 9200)
@@ -9,6 +9,7 @@
   main.cpp
   iswindowsntkernel.c
   #
+  ../../teraterm/ttpcmn/ttcmn_cominfo.h
   ../../teraterm/ttpcmn/ttcmn_cominfo.c
   #
   ../libs/getopt_mb_uni_src/getopt.c
@@ -33,7 +34,6 @@
   ${PACKAGE_NAME}
   PRIVATE
   common_static
-  #ttpcmn
   #
   #setupapi
   )

Modified: trunk/tools/lscom/main.cpp
===================================================================
--- trunk/tools/lscom/main.cpp	2021-03-17 14:36:47 UTC (rev 9199)
+++ trunk/tools/lscom/main.cpp	2021-03-18 15:27:14 UTC (rev 9200)
@@ -32,11 +32,9 @@
 #define _CRTDBG_MAP_ALLOC
 #include <crtdbg.h>
 
-extern "C" {
-int WINAPI DetectComPorts(LPWORD ComPortTable, int ComPortMax, char **ComPortDesc);
-}
-
 #include "comportinfo.h"
+#define DllExport __declspec(dllexport)
+#include "ttcmn_cominfo.h"
 
 #include "getopt.h"
 
@@ -159,8 +157,6 @@
 #endif
 	setlocale(LC_ALL, "");
 
-	int no_newline = 0;
-	int multicast = 0;
 	bool querydosdevice = false;
 	bool flag_DetectComPorts = false;
 	bool flag_fopen = false;


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