1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-30 20:35:38 +02:00

Checkinf for overflow and allocate dynamic buffer for environment.

This commit is contained in:
Alain Magloire 2003-01-17 19:29:58 +00:00
parent 0ad2ba9f60
commit 11304c78ff
2 changed files with 46 additions and 23 deletions

View file

@ -22,7 +22,7 @@
#include "jni.h"
#include "io.h"
//#define DEBUG_MONITOR
// #define DEBUG_MONITOR
#define PIPE_SIZE 512
#define MAX_CMD_SIZE 1024
@ -85,7 +85,8 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
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;
@ -150,6 +151,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
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 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
{
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 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
szCmdLine[nPos] = '\0';
// Prepare environment block
if (nEnvVars > 0)
{
nPos = 0;
@ -182,6 +185,21 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
const char * str = (*env) -> GetStringUTFChars(env, item, 0);
if(NULL != str)
{
while((nBlkSize - nPos) <= (len + 2)) // +2 for two '\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
}
strncpy(szEnvBlock + nPos, str, len);
nPos += len;
szEnvBlock[nPos] = '\0';
@ -244,7 +262,8 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
if(NULL != cwd)
free(cwd);
free(szEnvBlock);
CloseHandle(hread[0]);
CloseHandle(hwrite[1]);
@ -340,7 +359,8 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
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);
@ -353,6 +373,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
nPos = 0;
// Prepare command line
for(i = 0; i < nCmdTokens; ++i)
{
jobject item = (*env) -> GetObjectArrayElement(env, cmdarray, i);
@ -363,7 +384,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
{
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;
@ -375,6 +396,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
szCmdLine[nPos] = '\0';
// Prepare environment block
if (nEnvVars > 0)
{
nPos = 0;
@ -385,6 +407,16 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
const char * str = (*env) -> GetStringUTFChars(env, item, 0);
if(NULL != str)
{
while((nBlkSize - nPos) <= (len + 2)) // +2 for two '\0'
{
nBlkSize += MAX_ENV_SIZE;
szEnvBlock = (char *)realloc(szEnvBlock, nBlkSize);
if(NULL == szEnvBlock)
{
ThrowByName(env, "java/lang/Exception", "Not enough memory");
return 0;
}
}
strncpy(szEnvBlock + nPos, str, len);
nPos += len;
szEnvBlock[nPos] = '\0';
@ -433,6 +465,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
if(NULL != cwd)
free(cwd);
free(szEnvBlock);
if (!ret)
{
@ -708,7 +741,12 @@ int copyTo(char * target, const char * source, int cpyLength, int availSpace)
int totCpyLength = cpyLength;
BOOL bQoutedTerm = FALSE;
if(availSpace < cpyLength)
#ifdef DEBUG_MONITOR
sprintf(buffer, "copyTo start: %s %d %d\n", source, cpyLength, availSpace);
OutputDebugString(buffer);
#endif
if(availSpace <= cpyLength) // = to reserve space for final '\0'
return -1;
//strncpy(target, source, cpyLength);
//return cpyLength;

View file

@ -83,19 +83,6 @@ extern "C" int _tmain(int argc, TCHAR* argv[]) {
++nPos;
}
szCmdLine[nPos] = _T('\0');
/*
for (int i = 4; i < argc; i++) {
if(sizeof(szCmdLine) > (_tcslen(szCmdLine) + _tcslen(argv[i])))
{
_tcscat(szCmdLine, argv[i]);
_tcscat(szCmdLine, __TEXT(" "));
}
#ifdef DEBUG_MONITOR
else
OutputDebugString("Command line is too long\n");
#endif
}
*/
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = { 0 };
@ -202,10 +189,8 @@ int copyTo(LPTSTR target, LPCTSTR source, int cpyLength, int availSpace)
int totCpyLength = cpyLength;
BOOL bQoutedTerm = FALSE;
if(availSpace < cpyLength)
if(availSpace <= cpyLength) // = to reserve space for '\0'
return -1;
// strncpy(target, source, cpyLength);
// return cpyLength;
if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength)))
bQoutedTerm = TRUE; // Already quoted