Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-patch] 1_0_1 branch fix for spawner in core.win32

Index: ChangeLog
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.win32/ChangeLog,v
retrieving revision 1.4
diff -u -r1.4 ChangeLog
--- ChangeLog	19 Nov 2002 18:53:48 -0000	1.4
+++ ChangeLog	11 Mar 2003 20:05:44 -0000
@@ -1,3 +1,46 @@
+2003-02-25 Alex Chapiro
+
+	Fix for this type of problem:
+		make CFLAGS="-D jek "
+	
+	* os/win32/x86/spawner.dll: Rebuild
+	* os/win32/x86/starter.exe: Rebuild
+	* src/library/starter/starter.cpp:
+	Fix problem with embedded quotation
+	* src/library/Win32ProcessEx.c:
+	Fix problem with embedded quotation.
+
+2003-01-27 Alex Chapiro
+
+	* os/win32/x86/spawner.dll: Rebuild
+	* src/library/Win32ProcessEx.c: Synchronization error fix.
+
+2003-01-23 Alex Chapiro
+
+	* os/win32/x86/spawner.dll: Rebuild
+	* os/win32/x86/starter.exe: Rebuild
+	* src/library/starter/starter.cpp:
+	Correct quotation escaped.
+	* src/library/Win32ProcessEx.c:
+	Correct quotation escaped.
+
+2003-01-17 Alex Chapiro
+
+	* os/win32/x86/spawner.dll: Rebuild
+	* os/win32/x86/starter.exe: Rebuild
+	* src/library/starter/starter.cpp (copyTo):
+	Check for overflow.
+	* src/library/Win32ProcessEx.c (..exec1 exec0):
+	Dynamically allocate environment buffer to avoid overflow. 
+	Check for overflow.
+
+2003-01-13 Alex Chapiro
+
+	* os/win32/x86/spawner.dll: Rebuild.
+	* os/win32/x86/starter.exe: Rebuild.
+	* library/Win32ProcessEx.c: Quoting of spaces when calling starter.
+	* library/starter/starter.cpp: Quoting of spaces when calling the executable.
+
 2002-11-06 Alex Chapiro
 
 	* library/Win32ProcessEx.c:
Index: build.properties
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.win32/build.properties,v
retrieving revision 1.4
diff -u -r1.4 build.properties
--- build.properties	27 Nov 2002 17:15:58 -0000	1.4
+++ build.properties	11 Mar 2003 20:05:44 -0000
@@ -1,4 +1,9 @@
 bin.includes = fragment.xml,\
-               os/,\
-               fragment.properties
+               fragment.properties,\
+               about.html,\
+			   os/ 
+src.includes = fragment.xml,\
+               fragment.properties,\
+               about.html,\
+			   os/               
 source.cdt_win32.jar = src/
Index: library/Win32ProcessEx.c
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.win32/library/Win32ProcessEx.c,v
retrieving revision 1.5
diff -u -r1.5 Win32ProcessEx.c
--- library/Win32ProcessEx.c	19 Nov 2002 18:53:30 -0000	1.5
+++ library/Win32ProcessEx.c	11 Mar 2003 20:05:45 -0000
@@ -85,7 +85,8 @@
 	LPVOID envBlk = NULL;
     int ret = 0;
 	char  szCmdLine[MAX_CMD_SIZE];
-	char  szEnvBlock[MAX_ENV_SIZE];
+	int nBlkSize = MAX_ENV_SIZE; 
+	char  * szEnvBlock = (char *)malloc(nBlkSize);
 	jsize nCmdTokens = 0;
 	jsize nEnvVars = 0;
 	int i;
@@ -96,7 +97,7 @@
 	char eventWaitName[20];
 	char eventTerminateName[20];
 #ifdef DEBUG_MONITOR
-	char buffer[100];
+	char buffer[1000];
 #endif
 
 
@@ -150,6 +151,7 @@
 
 	nPos = sprintf(szCmdLine, "%sstarter.exe %s %s %s ", path, eventBreakName, eventWaitName, eventTerminateName);
 
+	// Prepare command line
 	for(i = 0; i < nCmdTokens; ++i) 
 		{
 		jobject item = (*env) -> GetObjectArrayElement(env, cmdarray, i);
@@ -160,7 +162,7 @@
 			{
 			if(0 > (nCpyLen = copyTo(szCmdLine + nPos, str, len, MAX_CMD_SIZE - nPos)))
 				{
-				ThrowByName(env, "java/Exception", "Too long command line");
+				ThrowByName(env, "java/lang/Exception", "Too long command line");
 				return 0;
 				}
 			nPos += nCpyLen;
@@ -172,6 +174,7 @@
 
 	szCmdLine[nPos] = '\0';
 
+	// Prepare environment block
     if (nEnvVars > 0) 
 		{
 		nPos = 0;
@@ -179,16 +182,26 @@
 			{
 			jobject item = (*env) -> GetObjectArrayElement(env, envp, i);
 			jsize    len = (*env) -> GetStringUTFLength(env, item);
-			int nCpyLen;
 			const char *  str = (*env) -> GetStringUTFChars(env, item, 0);	
 			if(NULL != str)
 				{
-				if(0 > (nCpyLen = copyTo(szEnvBlock + nPos, str, len, MAX_ENV_SIZE - nPos - 1)))
+				while((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0'
 					{
-					ThrowByName(env, "java/Exception", "Too many environment variables");
-					return 0;
+					nBlkSize += MAX_ENV_SIZE;
+					szEnvBlock = (char *)realloc(szEnvBlock, nBlkSize);
+					if(NULL == szEnvBlock) 
+						{
+						ThrowByName(env, "java/lang/Exception", "Not enough memory");
+						return 0;
+						}
+#ifdef DEBUG_MONITOR
+					sprintf(buffer, "Realloc environment block; new length is  %i \n", nBlkSize);
+					OutputDebugString(buffer);
+#endif
+
 					}
-				nPos += nCpyLen;
+				strncpy(szEnvBlock + nPos, str, len);
+				nPos += len;
 				szEnvBlock[nPos] = '\0';
 				++nPos;
 				(*env) -> ReleaseStringUTFChars(env, item, str);
@@ -249,7 +262,8 @@
 
 	if(NULL != cwd)
 		free(cwd);
-
+	
+	free(szEnvBlock);
 
     CloseHandle(hread[0]);
     CloseHandle(hwrite[1]);
@@ -294,7 +308,7 @@
 		pCurProcInfo -> pid = pi.dwProcessId;
         h[0] = pCurProcInfo -> eventWait;
 		h[1] = (HANDLE)_beginthreadex(NULL, 0, waitProcTermination, 
-			(void *) &(pi.dwProcessId), 0, (UINT*) &dwThreadId);
+			(void *) pi.dwProcessId, 0, (UINT*) &dwThreadId);
 		
 		what = WaitForMultipleObjects(2, h, FALSE, INFINITE); 
 		if((what != WAIT_OBJECT_0) && (pCurProcInfo -> pid > 0)) // CreateProcess failed
@@ -345,7 +359,8 @@
 	int i;
 	int nPos;
 	char  szCmdLine[MAX_CMD_SIZE];
-	char  szEnvBlock[MAX_ENV_SIZE];
+	int nBlkSize = MAX_ENV_SIZE; 
+	char * szEnvBlock = (char *)malloc(nBlkSize);
 
 
     sa.nLength = sizeof(sa);
@@ -358,6 +373,7 @@
 
 	nPos = 0;
 
+	// Prepare command line
 	for(i = 0; i < nCmdTokens; ++i) 
 		{
 		jobject item = (*env) -> GetObjectArrayElement(env, cmdarray, i);
@@ -368,7 +384,7 @@
 			{
 			if(0 > (nCpyLen = copyTo(szCmdLine + nPos, str, len, MAX_CMD_SIZE - nPos)))
 				{
-				ThrowByName(env, "java/Exception", "Too long command line");
+				ThrowByName(env, "java/lang/Exception", "Too long command line");
 				return 0;
 				}
 			nPos += nCpyLen;
@@ -380,6 +396,7 @@
 
 	szCmdLine[nPos] = '\0';
 
+	// Prepare environment block
     if (nEnvVars > 0) 
 		{
 		nPos = 0;
@@ -387,16 +404,21 @@
 			{
 			jobject item = (*env) -> GetObjectArrayElement(env, envp, i);
 			jsize    len = (*env) -> GetStringUTFLength(env, item);
-			int nCpyLen;
 			const char *  str = (*env) -> GetStringUTFChars(env, item, 0);	
 			if(NULL != str)
 				{
-				if(0 > (nCpyLen = copyTo(szEnvBlock + nPos, str, len, MAX_ENV_SIZE - nPos - 1)))
+				while((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0'
 					{
-					ThrowByName(env, "java/Exception", "Too many environment variables");
-					return 0;
+					nBlkSize += MAX_ENV_SIZE;
+					szEnvBlock = (char *)realloc(szEnvBlock, nBlkSize);
+					if(NULL == szEnvBlock) 
+						{
+						ThrowByName(env, "java/lang/Exception", "Not enough memory");
+						return 0;
+						}
 					}
-				nPos += nCpyLen;
+				strncpy(szEnvBlock + nPos, str, len);
+				nPos += len;
 				szEnvBlock[nPos] = '\0';
 				++nPos;
 				(*env) -> ReleaseStringUTFChars(env, item, str);
@@ -443,6 +465,7 @@
 
 	if(NULL != cwd)
 		free(cwd);
+	free(szEnvBlock);
 
     if (!ret) 
 		{
@@ -486,12 +509,15 @@
 	pProcInfo_t pCurProcInfo = findProcInfo(uid);
 #ifdef DEBUG_MONITOR
 	char buffer[100];
-	sprintf(buffer, "Spawner received signal %i for process %i\n", signal, pCurProcInfo -> pid);
-	OutputDebugString(buffer);
 #endif
 	
 	if(NULL == pCurProcInfo)
 		return -1;
+
+#ifdef DEBUG_MONITOR
+	sprintf(buffer, "Spawner received signal %i for process %i\n", signal, pCurProcInfo -> pid);
+	OutputDebugString(buffer);
+#endif
 	
 	hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pCurProcInfo -> pid);
 
@@ -661,10 +687,10 @@
 unsigned int _stdcall waitProcTermination(void* pv) 
 {
 	int i;
-	int pid = *(int *)pv;
+	int pid = (int)pv;
 	DWORD rc = 0;
 #ifdef DEBUG_MONITOR
-	char buffer[100];
+	char buffer[1000];
 #endif
 
 	HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
@@ -711,42 +737,70 @@
 int copyTo(char * target, const char * source, int cpyLength, int availSpace)
 {
 	BOOL bSlash = FALSE;
-	int i, j;
+	int i = 0, j = 0;
 	int totCpyLength = cpyLength;
 
-	if(availSpace < cpyLength)
+#define QUOTATION_DO   0
+#define QUOTATION_DONE 1
+#define QUOTATION_NONE 2
+
+	int nQuotationMode = 0;
+
+
+
+	if(availSpace <= cpyLength) // = to reserve space for final '\0'
 		return -1;
-	strncpy(target, source, cpyLength);
-	return cpyLength;
 
-	// Don't open this feature for a while
+	if(('\"' == *source) && ('\"' == *(source + cpyLength - 1)))
+		{
+		nQuotationMode = QUOTATION_DONE;
+		}
+	else
+	if(strchr(source, ' ') == NULL)
+		{
+		// No reason to quotate term becase it doesn't have embedded spaces
+		nQuotationMode = QUOTATION_NONE;
+		}
+	else
+		{
+		// Needs to be quotated
+		nQuotationMode = QUOTATION_DO;
+		*target = '\"';
+		++j;
+		}
 
-	for(i = 0, j = 0; i < cpyLength; ++i, ++j) 
+
+	for(; i < cpyLength; ++i, ++j) 
 		{
 		if(source[i] == '\\')
 			bSlash = TRUE;
 		else
-		if(source[i] == '"') 
 			{
-			if(bSlash)
+			// Don't escape embracing quotation marks
+			if((source[i] == '\"') && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))) ) )
 				{
-				if(j == availSpace)
-					return -1;
-				target[j] = '\\';
-				++j;
-				bSlash = FALSE;
+				if(!bSlash) // If still not escaped
+					{
+					if(j == availSpace)
+						return -1;
+					target[j] = '\\';
+					++j;
+					}
 				}
-			if(j == availSpace)
-				return -1;
-			target[j] = '\\';
-			++j;
-			}
-		else
 			bSlash = FALSE;
+			}
 
 		if(j == availSpace)
 			return -1;
 		target[j] = source[i];
+		}
+
+	if(nQuotationMode == QUOTATION_DO)
+		{
+		if(j == availSpace)
+			return -1;
+		target[j] = '\"';
+		++j;
 		}
 
 	return j;
Index: library/starter/starter.cpp
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.win32/library/starter/starter.cpp,v
retrieving revision 1.1
diff -u -r1.1 starter.cpp
--- library/starter/starter.cpp	3 Sep 2002 16:16:20 -0000	1.1
+++ library/starter/starter.cpp	11 Mar 2003 20:05:45 -0000
@@ -26,6 +26,8 @@
 // #define DEBUG_MONITOR
 #define MAX_CMD_LINE_LENGTH (1024)
 
+int copyTo(char * target, const char * source, int cpyLength, int availSpace);
+
 ///////////////////////////////////////////////////////////////////////////////
 BOOL WINAPI HandlerRoutine(  DWORD dwCtrlType)   //  control signal type
 {
@@ -64,17 +66,23 @@
 
    // Construct the full command line
    TCHAR szCmdLine[MAX_CMD_LINE_LENGTH] = { 0 };
-   for (int i = 4; i < argc; i++) {
-	  if(sizeof(szCmdLine) > (_tcslen(szCmdLine) + _tcslen(argv[i]))) 
+   int nPos = 0;
+
+   for(int i = 4; i < argc; ++i) 
 		{
-		_tcscat(szCmdLine, argv[i]); 
-		_tcscat(szCmdLine, __TEXT(" ")); 
-		}
+		int nCpyLen;
+		if(0 > (nCpyLen = copyTo(szCmdLine + nPos, argv[i], _tcslen(argv[i]), MAX_CMD_LINE_LENGTH - nPos)))
+			{
 #ifdef DEBUG_MONITOR
-	  else
-		OutputDebugString("Command line is too long\n");
+			OutputDebugString("Not enough space to build command line\n");
 #endif
-   }
+			return 0;
+			}
+		nPos += nCpyLen;
+		szCmdLine[nPos] = _T(' ');
+		++nPos;
+		}   
+   szCmdLine[nPos] = _T('\0');
 
    STARTUPINFO         si = { sizeof(si) };
    PROCESS_INFORMATION pi = { 0 };
@@ -172,6 +180,76 @@
  
    return(dwExitCode);
 }
+
+// Return number of bytes in target or -1 in case of error
+int copyTo(LPTSTR target, LPCTSTR source, int cpyLength, int availSpace)
+{
+	BOOL bSlash = FALSE;
+	int i = 0, j = 0;
+	int totCpyLength = cpyLength;
+
+#define QUOTATION_DO   0
+#define QUOTATION_DONE 1
+#define QUOTATION_NONE 2
+
+	int nQuotationMode = 0;
+	if(availSpace <= cpyLength)  // = to reserve space for '\0'
+		return -1;
+
+	if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1)))
+		{
+		// Already done
+		nQuotationMode = QUOTATION_DONE;
+		}
+	else
+	if(_tcschr(source, _T(' ')) == NULL)
+		{
+		// No reason to quotate term becase it doesn't have embedded spaces
+		nQuotationMode = QUOTATION_NONE;
+		}
+	else
+		{
+		// Needs to be quotated
+		nQuotationMode = QUOTATION_DO;
+		*target = _T('\"');
+		++j;
+		}
+
+	for(; i < cpyLength; ++i, ++j) 
+		{
+		if(source[i] == _T('\\'))
+			bSlash = TRUE;
+		else
+		// Don't escape embracing quotation marks
+		if((source[i] == _T('\"')) && !((nQuotationMode == QUOTATION_DONE) && ((i == 0) || (i == (cpyLength - 1))) ) )
+			{
+			if(!bSlash)
+				{
+				if(j == availSpace)
+					return -1;
+				target[j] = _T('\\');
+				++j;
+				}
+			bSlash = FALSE;
+			}
+		else
+			bSlash = FALSE;
+
+		if(j == availSpace)
+			return -1;
+		target[j] = source[i];
+		}
+
+	if(nQuotationMode == QUOTATION_DO)
+		{
+		if(j == availSpace)
+			return -1;
+		target[j] = _T('\"');
+		++j;
+		}
+	return j;
+}
+
 
 
 //////////////////////////////// End of File //////////////////////////////////
Index: os/win32/x86/spawner.dll
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.win32/os/win32/x86/spawner.dll,v
retrieving revision 1.7
diff -u -r1.7 spawner.dll
Binary files /tmp/cvsr4uOq2 and spawner.dll differ
Index: os/win32/x86/starter.exe
===================================================================
RCS file: /home/tools/org.eclipse.cdt.core.win32/os/win32/x86/starter.exe,v
retrieving revision 1.3
diff -u -r1.3 starter.exe
Binary files /tmp/cvs0TP8Bh and starter.exe differ



Back to the top