mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-31 12:55:40 +02:00
Checkinf for overflow and allocate dynamic buffer for environment.
This commit is contained in:
parent
0ad2ba9f60
commit
11304c78ff
2 changed files with 46 additions and 23 deletions
|
@ -22,7 +22,7 @@
|
||||||
#include "jni.h"
|
#include "jni.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
|
||||||
//#define DEBUG_MONITOR
|
// #define DEBUG_MONITOR
|
||||||
|
|
||||||
#define PIPE_SIZE 512
|
#define PIPE_SIZE 512
|
||||||
#define MAX_CMD_SIZE 1024
|
#define MAX_CMD_SIZE 1024
|
||||||
|
@ -85,7 +85,8 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
|
||||||
LPVOID envBlk = NULL;
|
LPVOID envBlk = NULL;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
char szCmdLine[MAX_CMD_SIZE];
|
char szCmdLine[MAX_CMD_SIZE];
|
||||||
char szEnvBlock[MAX_ENV_SIZE];
|
int nBlkSize = MAX_ENV_SIZE;
|
||||||
|
char * szEnvBlock = (char *)malloc(nBlkSize);
|
||||||
jsize nCmdTokens = 0;
|
jsize nCmdTokens = 0;
|
||||||
jsize nEnvVars = 0;
|
jsize nEnvVars = 0;
|
||||||
int i;
|
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);
|
nPos = sprintf(szCmdLine, "%sstarter.exe %s %s %s ", path, eventBreakName, eventWaitName, eventTerminateName);
|
||||||
|
|
||||||
|
// Prepare command line
|
||||||
for(i = 0; i < nCmdTokens; ++i)
|
for(i = 0; i < nCmdTokens; ++i)
|
||||||
{
|
{
|
||||||
jobject item = (*env) -> GetObjectArrayElement(env, cmdarray, 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)))
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
nPos += nCpyLen;
|
nPos += nCpyLen;
|
||||||
|
@ -172,6 +174,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
|
||||||
|
|
||||||
szCmdLine[nPos] = '\0';
|
szCmdLine[nPos] = '\0';
|
||||||
|
|
||||||
|
// Prepare environment block
|
||||||
if (nEnvVars > 0)
|
if (nEnvVars > 0)
|
||||||
{
|
{
|
||||||
nPos = 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);
|
const char * str = (*env) -> GetStringUTFChars(env, item, 0);
|
||||||
if(NULL != str)
|
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);
|
strncpy(szEnvBlock + nPos, str, len);
|
||||||
nPos += len;
|
nPos += len;
|
||||||
szEnvBlock[nPos] = '\0';
|
szEnvBlock[nPos] = '\0';
|
||||||
|
@ -244,7 +262,8 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0
|
||||||
|
|
||||||
if(NULL != cwd)
|
if(NULL != cwd)
|
||||||
free(cwd);
|
free(cwd);
|
||||||
|
|
||||||
|
free(szEnvBlock);
|
||||||
|
|
||||||
CloseHandle(hread[0]);
|
CloseHandle(hread[0]);
|
||||||
CloseHandle(hwrite[1]);
|
CloseHandle(hwrite[1]);
|
||||||
|
@ -340,7 +359,8 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
|
||||||
int i;
|
int i;
|
||||||
int nPos;
|
int nPos;
|
||||||
char szCmdLine[MAX_CMD_SIZE];
|
char szCmdLine[MAX_CMD_SIZE];
|
||||||
char szEnvBlock[MAX_ENV_SIZE];
|
int nBlkSize = MAX_ENV_SIZE;
|
||||||
|
char * szEnvBlock = (char *)malloc(nBlkSize);
|
||||||
|
|
||||||
|
|
||||||
sa.nLength = sizeof(sa);
|
sa.nLength = sizeof(sa);
|
||||||
|
@ -353,6 +373,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
|
||||||
|
|
||||||
nPos = 0;
|
nPos = 0;
|
||||||
|
|
||||||
|
// Prepare command line
|
||||||
for(i = 0; i < nCmdTokens; ++i)
|
for(i = 0; i < nCmdTokens; ++i)
|
||||||
{
|
{
|
||||||
jobject item = (*env) -> GetObjectArrayElement(env, cmdarray, 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)))
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
nPos += nCpyLen;
|
nPos += nCpyLen;
|
||||||
|
@ -375,6 +396,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
|
||||||
|
|
||||||
szCmdLine[nPos] = '\0';
|
szCmdLine[nPos] = '\0';
|
||||||
|
|
||||||
|
// Prepare environment block
|
||||||
if (nEnvVars > 0)
|
if (nEnvVars > 0)
|
||||||
{
|
{
|
||||||
nPos = 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);
|
const char * str = (*env) -> GetStringUTFChars(env, item, 0);
|
||||||
if(NULL != str)
|
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);
|
strncpy(szEnvBlock + nPos, str, len);
|
||||||
nPos += len;
|
nPos += len;
|
||||||
szEnvBlock[nPos] = '\0';
|
szEnvBlock[nPos] = '\0';
|
||||||
|
@ -433,6 +465,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1
|
||||||
|
|
||||||
if(NULL != cwd)
|
if(NULL != cwd)
|
||||||
free(cwd);
|
free(cwd);
|
||||||
|
free(szEnvBlock);
|
||||||
|
|
||||||
if (!ret)
|
if (!ret)
|
||||||
{
|
{
|
||||||
|
@ -708,7 +741,12 @@ int copyTo(char * target, const char * source, int cpyLength, int availSpace)
|
||||||
int totCpyLength = cpyLength;
|
int totCpyLength = cpyLength;
|
||||||
BOOL bQoutedTerm = FALSE;
|
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;
|
return -1;
|
||||||
//strncpy(target, source, cpyLength);
|
//strncpy(target, source, cpyLength);
|
||||||
//return cpyLength;
|
//return cpyLength;
|
||||||
|
|
|
@ -83,19 +83,6 @@ extern "C" int _tmain(int argc, TCHAR* argv[]) {
|
||||||
++nPos;
|
++nPos;
|
||||||
}
|
}
|
||||||
szCmdLine[nPos] = _T('\0');
|
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) };
|
STARTUPINFO si = { sizeof(si) };
|
||||||
PROCESS_INFORMATION pi = { 0 };
|
PROCESS_INFORMATION pi = { 0 };
|
||||||
|
@ -202,10 +189,8 @@ int copyTo(LPTSTR target, LPCTSTR source, int cpyLength, int availSpace)
|
||||||
int totCpyLength = cpyLength;
|
int totCpyLength = cpyLength;
|
||||||
BOOL bQoutedTerm = FALSE;
|
BOOL bQoutedTerm = FALSE;
|
||||||
|
|
||||||
if(availSpace < cpyLength)
|
if(availSpace <= cpyLength) // = to reserve space for '\0'
|
||||||
return -1;
|
return -1;
|
||||||
// strncpy(target, source, cpyLength);
|
|
||||||
// return cpyLength;
|
|
||||||
|
|
||||||
if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength)))
|
if((_T('\"') == *source) && (_T('\"') == *(source + cpyLength)))
|
||||||
bQoutedTerm = TRUE; // Already quoted
|
bQoutedTerm = TRUE; // Already quoted
|
||||||
|
|
Loading…
Add table
Reference in a new issue