1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Bug 568079: Cleanup of native code

* Unify pointer checkes
* Avoid using negated conditions.
* Reduce scope of local variables when possible

Change-Id: Ibacd13126351019af544f3e22513654d5ffee342
Signed-off-by: Torbjörn Svensson <azoff@svenskalinuxforeningen.se>
This commit is contained in:
Torbjörn Svensson 2020-11-01 14:46:59 +01:00
parent 2857a7a0b3
commit c598eedffa
20 changed files with 125 additions and 145 deletions

View file

@ -65,7 +65,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
chdir(dirpath); chdir(dirpath);
if (channels != NULL) { if (channels) {
int fds; int fds;
if (!console && setsid() < 0) { if (!console && setsid() < 0) {
@ -116,10 +116,10 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
} }
} }
if (envp[0] == NULL) { if (envp && envp[0]) {
execv(full_path, argv);
} else {
execve(full_path, argv, envp); execve(full_path, argv, envp);
} else {
execv(full_path, argv);
} }
_exit(127); _exit(127);
@ -128,7 +128,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
if (console) { if (console) {
set_noecho(fdm); set_noecho(fdm);
} }
if (channels != NULL) { if (channels) {
channels[0] = fdm; /* Input Stream. */ channels[0] = fdm; /* Input Stream. */
channels[1] = fdm; /* Output Stream. */ channels[1] = fdm; /* Output Stream. */
if (console) { if (console) {
@ -173,10 +173,10 @@ int main(int argc, char **argv, char **envp) {
} else { } else {
fputs("foo\n", app_stdin); fputs("foo\n", app_stdin);
fputs("bar\n", app_stdin); fputs("bar\n", app_stdin);
while (fgets(buffer, sizeof buffer, app_stdout) != NULL) { while (fgets(buffer, sizeof buffer, app_stdout)) {
fprintf(stdout, "STDOUT: %s\n", buffer); fprintf(stdout, "STDOUT: %s\n", buffer);
} }
while (fgets(buffer, sizeof buffer, app_stderr) != NULL) { while (fgets(buffer, sizeof buffer, app_stderr)) {
fprintf(stdout, "STDERR: %s\n", buffer); fprintf(stdout, "STDERR: %s\n", buffer);
} }
} }

View file

@ -43,7 +43,7 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
/* /*
* Make sure we can create our pipes before forking. * Make sure we can create our pipes before forking.
*/ */
if (channels != NULL) { if (channels) {
if (pipe(pipe0) < 0 || pipe(pipe1) < 0 || pipe(pipe2) < 0) { if (pipe(pipe0) < 0 || pipe(pipe1) < 0 || pipe(pipe2) < 0) {
fprintf(stderr, "%s(%d): returning due to error.\n", __func__, __LINE__); fprintf(stderr, "%s(%d): returning due to error.\n", __func__, __LINE__);
free(full_path); free(full_path);
@ -60,7 +60,7 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
} else if (childpid == 0) { /* child */ } else if (childpid == 0) { /* child */
chdir(dirpath); chdir(dirpath);
if (channels != NULL) { if (channels) {
/* Close the write end of pipe0 */ /* Close the write end of pipe0 */
if (close(pipe0[1]) == -1) { if (close(pipe0[1]) == -1) {
perror("close(pipe0[1])"); perror("close(pipe0[1])");
@ -94,16 +94,16 @@ pid_t exec0(const char *path, char *const argv[], char *const envp[], const char
setpgid(getpid(), getpid()); setpgid(getpid(), getpid());
if (envp[0] == NULL) { if (envp && envp[0]) {
execv(full_path, argv);
} else {
execve(full_path, argv, envp); execve(full_path, argv, envp);
} else {
execv(full_path, argv);
} }
_exit(127); _exit(127);
} else if (childpid != 0) { /* parent */ } else if (childpid != 0) { /* parent */
if (channels != NULL) { if (channels) {
/* close the read end of pipe1 */ /* close the read end of pipe1 */
if (close(pipe0[0]) == -1) { if (close(pipe0[0]) == -1) {
perror("close(pipe0[0])"); perror("close(pipe0[0])");

View file

@ -24,7 +24,7 @@
static void ThrowByName(JNIEnv *env, const char *name, const char *msg) { static void ThrowByName(JNIEnv *env, const char *name, const char *msg) {
jclass cls = (*env)->FindClass(env, name); jclass cls = (*env)->FindClass(env, name);
if (cls != 0) { /* Otherwise an exception has already been thrown */ if (cls) { /* Otherwise an exception has already been thrown */
(*env)->ThrowNew(env, cls, msg); (*env)->ThrowNew(env, cls, msg);
} }

View file

@ -33,12 +33,11 @@
const int path_def_len = 5; /* strlen(PATH_DEF); */ const int path_def_len = 5; /* strlen(PATH_DEF); */
char *path_val(char *const envp[]) { char *path_val(char *const envp[]) {
int i; if (!envp || !envp[0]) {
if (envp == NULL || envp[0] == NULL) {
return getenv("PATH"); return getenv("PATH");
} }
for (i = 0; envp[i] != NULL; i++) { for (int i = 0; envp[i]; i++) {
char *p = envp[i]; char *p = envp[i];
if (!strncmp(PATH_DEF, p, path_def_len)) { if (!strncmp(PATH_DEF, p, path_def_len)) {
return p + path_def_len; return p + path_def_len;
@ -56,7 +55,7 @@ char *pfind(const char *name, char *const envp[]) {
struct stat sb; struct stat sb;
/* Sanity check. */ /* Sanity check. */
if (name == NULL) { if (!name) {
fprintf(stderr, "pfind(): Null argument.\n"); fprintf(stderr, "pfind(): Null argument.\n");
return NULL; return NULL;
} }
@ -72,7 +71,7 @@ char *pfind(const char *name, char *const envp[]) {
/* Search in the PATH environment. */ /* Search in the PATH environment. */
path = path_val(envp); path = path_val(envp);
if (path == NULL || strlen(path) <= 0) { if (!path || strlen(path) <= 0) {
fprintf(stderr, "Unable to get $PATH.\n"); fprintf(stderr, "Unable to get $PATH.\n");
return NULL; return NULL;
} }
@ -81,7 +80,7 @@ char *pfind(const char *name, char *const envp[]) {
path = strdup(path); path = strdup(path);
tok = strtok_r(path, ":", &sp); tok = strtok_r(path, ":", &sp);
while (tok != NULL) { while (tok) {
snprintf(fullpath, sizeof(fullpath) - 1, "%s/%s", tok, name); snprintf(fullpath, sizeof(fullpath) - 1, "%s/%s", tok, name);
if (stat(fullpath, &sb) == 0 && S_ISREG(sb.st_mode)) { /* fullpath is a file */ if (stat(fullpath, &sb) == 0 && S_ISREG(sb.st_mode)) { /* fullpath is a file */
@ -100,15 +99,12 @@ char *pfind(const char *name, char *const envp[]) {
#ifdef BUILD_WITH_MAIN #ifdef BUILD_WITH_MAIN
int main(int argc, char **argv) { int main(int argc, char **argv) {
int i; for (int i = 1; i < argc; i++) {
char *fullpath; char *fullpath = pfind(argv[i], NULL);
if (fullpath) {
for (i = 1; i < argc; i++) {
fullpath = pfind(argv[i], NULL);
if (fullpath == NULL) {
printf("Unable to find %s in $PATH.\n", argv[i]);
} else {
printf("Found %s @ %s.\n", argv[i], fullpath); printf("Found %s @ %s.\n", argv[i], fullpath);
} else {
printf("Unable to find %s in $PATH.\n", argv[i]);
} }
} }
} }

View file

@ -42,7 +42,7 @@ JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_pty_PTY_openMaster(JNIEnv *
/* Set the master fd. */ /* Set the master fd. */
fid = (*env)->GetFieldID(env, cls, "master", "I"); fid = (*env)->GetFieldID(env, cls, "master", "I");
if (fid == NULL) { if (!fid) {
return NULL; return NULL;
} }
(*env)->SetIntField(env, jobj, fid, (jint)master); (*env)->SetIntField(env, jobj, fid, (jint)master);

View file

@ -102,17 +102,17 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec2(JNIEnv *
int fd[3]; int fd[3];
pid_t pid = -1; pid_t pid = -1;
if (jchannels == NULL) { if (!jchannels) {
goto bail_out; goto bail_out;
} }
cmd = alloc_c_array(env, jcmd); cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) { if (!cmd) {
goto bail_out; goto bail_out;
} }
envp = alloc_c_array(env, jenv); envp = alloc_c_array(env, jenv);
if (envp == NULL) { if (!envp) {
goto bail_out; goto bail_out;
} }
@ -151,12 +151,12 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *
pid_t pid = -1; pid_t pid = -1;
cmd = alloc_c_array(env, jcmd); cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) { if (!cmd) {
goto bail_out; goto bail_out;
} }
envp = alloc_c_array(env, jenv); envp = alloc_c_array(env, jenv);
if (envp == NULL) { if (!envp) {
goto bail_out; goto bail_out;
} }
@ -189,7 +189,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
jclass channelClass = NULL; jclass channelClass = NULL;
jmethodID channelConstructor = NULL; jmethodID channelConstructor = NULL;
if (jchannels == NULL) { if (!jchannels) {
goto bail_out; goto bail_out;
} }
@ -199,17 +199,17 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
} }
channelConstructor = (*env)->GetMethodID(env, channelClass, "<init>", "(I)V"); channelConstructor = (*env)->GetMethodID(env, channelClass, "<init>", "(I)V");
if (channelConstructor == 0) { if (!channelConstructor) {
goto bail_out; goto bail_out;
} }
cmd = alloc_c_array(env, jcmd); cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) { if (!cmd) {
goto bail_out; goto bail_out;
} }
envp = alloc_c_array(env, jenv); envp = alloc_c_array(env, jenv);
if (envp == NULL) { if (!envp) {
goto bail_out; goto bail_out;
} }

View file

@ -121,10 +121,10 @@ void ensureSize(wchar_t **ptr, int *psize, int requiredLength) {
size = requiredLength; size = requiredLength;
} }
*ptr = (wchar_t *)realloc(*ptr, size * sizeof(wchar_t)); *ptr = (wchar_t *)realloc(*ptr, size * sizeof(wchar_t));
if (NULL == *ptr) { if (*ptr) {
*psize = 0;
} else {
*psize = size; *psize = size;
} else {
*psize = 0;
} }
} }
} }
@ -147,7 +147,6 @@ extern "C"
wchar_t *szEnvBlock = NULL; wchar_t *szEnvBlock = NULL;
jsize nCmdTokens = 0; jsize nCmdTokens = 0;
jsize nEnvVars = 0; jsize nEnvVars = 0;
int i;
DWORD pid = GetCurrentProcessId(); DWORD pid = GetCurrentProcessId();
int nPos; int nPos;
pProcInfo_t pCurProcInfo; pProcInfo_t pCurProcInfo;
@ -168,19 +167,19 @@ extern "C"
jclass channelClass = NULL; jclass channelClass = NULL;
jmethodID channelConstructor = NULL; jmethodID channelConstructor = NULL;
if (channels == NULL) { if (!channels) {
ThrowByName(env, "java/io/IOException", "Channels can't be null"); ThrowByName(env, "java/io/IOException", "Channels can't be null");
return 0; return 0;
} }
channelClass = (*env)->FindClass(env, "org/eclipse/cdt/utils/spawner/Spawner$WinChannel"); channelClass = (*env)->FindClass(env, "org/eclipse/cdt/utils/spawner/Spawner$WinChannel");
if (channelClass == 0) { if (!channelClass) {
ThrowByName(env, "java/io/IOException", "Unable to find channel class"); ThrowByName(env, "java/io/IOException", "Unable to find channel class");
return 0; return 0;
} }
channelConstructor = (*env)->GetMethodID(env, channelClass, "<init>", "(J)V"); channelConstructor = (*env)->GetMethodID(env, channelClass, "<init>", "(J)V");
if (channelConstructor == 0) { if (!channelConstructor) {
ThrowByName(env, "java/io/IOException", "Unable to find channel constructor"); ThrowByName(env, "java/io/IOException", "Unable to find channel constructor");
return 0; return 0;
} }
@ -236,7 +235,7 @@ extern "C"
pCurProcInfo = createProcInfo(); pCurProcInfo = createProcInfo();
if (NULL == pCurProcInfo) { if (!pCurProcInfo) {
ThrowByName(env, "java/io/IOException", "Too many processes"); ThrowByName(env, "java/io/IOException", "Too many processes");
return 0; return 0;
} }
@ -252,7 +251,7 @@ extern "C"
nLocalCounter); nLocalCounter);
pCurProcInfo->eventBreak = CreateEventW(NULL, FALSE, FALSE, eventBreakName); pCurProcInfo->eventBreak = CreateEventW(NULL, FALSE, FALSE, eventBreakName);
if (NULL == pCurProcInfo->eventBreak || GetLastError() == ERROR_ALREADY_EXISTS) { if (!pCurProcInfo->eventBreak || GetLastError() == ERROR_ALREADY_EXISTS) {
ThrowByName(env, "java/io/IOException", "Cannot create event"); ThrowByName(env, "java/io/IOException", "Cannot create event");
return 0; return 0;
} }
@ -266,19 +265,19 @@ extern "C"
nPos = wcslen(szCmdLine); nPos = wcslen(szCmdLine);
// Prepare command line // Prepare command line
for (i = 0; i < nCmdTokens; ++i) { for (int i = 0; i < nCmdTokens; ++i) {
jstring item = (jstring)(*env)->GetObjectArrayElement(env, cmdarray, i); jstring item = (jstring)(*env)->GetObjectArrayElement(env, cmdarray, i);
jsize len = (*env)->GetStringLength(env, item); jsize len = (*env)->GetStringLength(env, item);
int nCpyLen; int nCpyLen;
const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, item, 0); const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, item, 0);
if (NULL != str) { if (str) {
int requiredSize = nPos + len + 2; int requiredSize = nPos + len + 2;
if (requiredSize > 32 * 1024) { if (requiredSize > 32 * 1024) {
ThrowByName(env, "java/io/IOException", "Command line too long"); ThrowByName(env, "java/io/IOException", "Command line too long");
return 0; return 0;
} }
ensureSize(&szCmdLine, &nCmdLineLength, requiredSize); ensureSize(&szCmdLine, &nCmdLineLength, requiredSize);
if (NULL == szCmdLine) { if (!szCmdLine) {
ThrowByName(env, "java/io/IOException", "Not enough memory"); ThrowByName(env, "java/io/IOException", "Not enough memory");
return 0; return 0;
} }
@ -303,15 +302,15 @@ extern "C"
if (nEnvVars > 0) { if (nEnvVars > 0) {
nPos = 0; nPos = 0;
szEnvBlock = (wchar_t *)malloc(nBlkSize * sizeof(wchar_t)); szEnvBlock = (wchar_t *)malloc(nBlkSize * sizeof(wchar_t));
for (i = 0; i < nEnvVars; ++i) { for (int i = 0; i < nEnvVars; ++i) {
jstring item = (jstring)(*env)->GetObjectArrayElement(env, envp, i); jstring item = (jstring)(*env)->GetObjectArrayElement(env, envp, i);
jsize len = (*env)->GetStringLength(env, item); jsize len = (*env)->GetStringLength(env, item);
const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, item, 0); const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, item, 0);
if (NULL != str) { if (str) {
while ((nBlkSize - nPos) <= (len + 2)) { // +2 for two '\0' while ((nBlkSize - nPos) <= (len + 2)) { // +2 for two '\0'
nBlkSize += MAX_ENV_SIZE; nBlkSize += MAX_ENV_SIZE;
szEnvBlock = (wchar_t *)realloc(szEnvBlock, nBlkSize * sizeof(wchar_t)); szEnvBlock = (wchar_t *)realloc(szEnvBlock, nBlkSize * sizeof(wchar_t));
if (NULL == szEnvBlock) { if (!szEnvBlock) {
ThrowByName(env, "java/io/IOException", "Not enough memory"); ThrowByName(env, "java/io/IOException", "Not enough memory");
return 0; return 0;
} }
@ -332,11 +331,11 @@ extern "C"
szEnvBlock[nPos] = _T('\0'); szEnvBlock[nPos] = _T('\0');
} }
if (dir != 0) { if (dir) {
const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, dir, 0); const jchar *str = (*env)->GetStringChars(env, dir, NULL);
if (NULL != str) { if (str) {
cwd = wcsdup(str); cwd = wcsdup((const wchar_t *)str);
(*env)->ReleaseStringChars(env, dir, (const jchar *)str); (*env)->ReleaseStringChars(env, dir, str);
} }
} }
@ -482,14 +481,14 @@ extern "C"
jsize len = (*env)->GetStringLength(env, item); jsize len = (*env)->GetStringLength(env, item);
int nCpyLen; int nCpyLen;
const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, item, 0); const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, item, 0);
if (NULL != str) { if (str) {
int requiredSize = nPos + len + 2; int requiredSize = nPos + len + 2;
if (requiredSize > 32 * 1024) { if (requiredSize > 32 * 1024) {
ThrowByName(env, "java/io/IOException", "Command line too long"); ThrowByName(env, "java/io/IOException", "Command line too long");
return 0; return 0;
} }
ensureSize(&szCmdLine, &nCmdLineLength, requiredSize); ensureSize(&szCmdLine, &nCmdLineLength, requiredSize);
if (NULL == szCmdLine) { if (!szCmdLine) {
ThrowByName(env, "java/io/IOException", "Not enough memory"); ThrowByName(env, "java/io/IOException", "Not enough memory");
return 0; return 0;
} }
@ -515,11 +514,11 @@ extern "C"
jstring item = (jstring)(*env)->GetObjectArrayElement(env, envp, i); jstring item = (jstring)(*env)->GetObjectArrayElement(env, envp, i);
jsize len = (*env)->GetStringLength(env, item); jsize len = (*env)->GetStringLength(env, item);
const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, item, 0); const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, item, 0);
if (NULL != str) { if (str) {
while ((nBlkSize - nPos) <= (len + 2)) { // +2 for two '\0' while ((nBlkSize - nPos) <= (len + 2)) { // +2 for two '\0'
nBlkSize += MAX_ENV_SIZE; nBlkSize += MAX_ENV_SIZE;
szEnvBlock = (wchar_t *)realloc(szEnvBlock, nBlkSize * sizeof(wchar_t)); szEnvBlock = (wchar_t *)realloc(szEnvBlock, nBlkSize * sizeof(wchar_t));
if (NULL == szEnvBlock) { if (!szEnvBlock) {
ThrowByName(env, "java/io/Exception", "Not enough memory"); ThrowByName(env, "java/io/Exception", "Not enough memory");
return 0; return 0;
} }
@ -535,11 +534,11 @@ extern "C"
envBlk = szEnvBlock; envBlk = szEnvBlock;
} }
if (dir != 0) { if (dir) {
const wchar_t *str = (const wchar_t *)(*env)->GetStringChars(env, dir, 0); const jchar *str = (*env)->GetStringChars(env, dir, NULL);
if (NULL != str) { if (str) {
cwd = wcsdup(str); cwd = wcsdup((const wchar_t *)str);
(*env)->ReleaseStringChars(env, dir, (const jchar *)str); (*env)->ReleaseStringChars(env, dir, str);
} }
} }
@ -599,7 +598,7 @@ extern "C"
HANDLE hProc; HANDLE hProc;
pProcInfo_t pCurProcInfo = findProcInfo(uid); pProcInfo_t pCurProcInfo = findProcInfo(uid);
if (NULL == pCurProcInfo) { if (!pCurProcInfo) {
if (SIG_INT == signal) { // Try another way if (SIG_INT == signal) { // Try another way
return interruptProcess(uid); return interruptProcess(uid);
} }
@ -612,7 +611,7 @@ extern "C"
hProc = OpenProcess(SYNCHRONIZE, 0, pCurProcInfo->pid); hProc = OpenProcess(SYNCHRONIZE, 0, pCurProcInfo->pid);
if (NULL == hProc) { if (!hProc) {
return -1; return -1;
} }
@ -679,13 +678,13 @@ extern "C"
HANDLE hProc; HANDLE hProc;
pProcInfo_t pCurProcInfo = findProcInfo(uid); pProcInfo_t pCurProcInfo = findProcInfo(uid);
if (NULL == pCurProcInfo) { if (!pCurProcInfo) {
return -1; return -1;
} }
hProc = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, 0, pCurProcInfo->pid); hProc = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, 0, pCurProcInfo->pid);
if (NULL == hProc) { if (!hProc) {
return -1; return -1;
} }
@ -713,7 +712,7 @@ extern "C"
void ThrowByName(JNIEnv *env, const char *name, const char *msg) { void ThrowByName(JNIEnv *env, const char *name, const char *msg) {
jclass cls = (*env)->FindClass(env, name); jclass cls = (*env)->FindClass(env, name);
if (cls != 0) { /* Otherwise an exception has already been thrown */ if (cls) { /* Otherwise an exception has already been thrown */
(*env)->ThrowNew(env, cls, msg); (*env)->ThrowNew(env, cls, msg);
} }
@ -727,17 +726,16 @@ void ThrowByName(JNIEnv *env, const char *name, const char *msg) {
// Return : pointer to the process descriptor // Return : pointer to the process descriptor
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
pProcInfo_t createProcInfo() { pProcInfo_t createProcInfo() {
int i;
pProcInfo_t p = NULL; pProcInfo_t p = NULL;
EnterCriticalSection(&cs); EnterCriticalSection(&cs);
if (NULL == pInfo) { if (!pInfo) {
pInfo = (pProcInfo_t)malloc(sizeof(procInfo_t) * MAX_PROCS); pInfo = (pProcInfo_t)malloc(sizeof(procInfo_t) * MAX_PROCS);
ZeroMemory(pInfo, sizeof(procInfo_t) * MAX_PROCS); ZeroMemory(pInfo, sizeof(procInfo_t) * MAX_PROCS);
} }
for (i = 0; i < MAX_PROCS; ++i) { for (int i = 0; i < MAX_PROCS; ++i) {
if (pInfo[i].pid == 0) { if (pInfo[i].pid == 0) {
pInfo[i].pid = -1; pInfo[i].pid = -1;
pInfo[i].uid = ++procCounter; pInfo[i].uid = ++procCounter;
@ -757,20 +755,15 @@ pProcInfo_t createProcInfo() {
// Return : pointer to the process descriptor // Return : pointer to the process descriptor
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
pProcInfo_t findProcInfo(int uid) { pProcInfo_t findProcInfo(int uid) {
int i; if (pInfo) {
pProcInfo_t p = NULL; for (int i = 0; i < MAX_PROCS; ++i) {
if (NULL == pInfo) { if (pInfo[i].uid == uid) {
return NULL; return pInfo + i;
} }
for (i = 0; i < MAX_PROCS; ++i) {
if (pInfo[i].uid == uid) {
p = pInfo + i;
break;
} }
} }
return p; return NULL;
} }
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
@ -813,12 +806,11 @@ void cleanUpProcBlock(pProcInfo_t pCurProcInfo) {
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
void _cdecl waitProcTermination(void *pv) { void _cdecl waitProcTermination(void *pv) {
PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)pv; PROCESS_INFORMATION *pi = (PROCESS_INFORMATION *)pv;
int i;
// wait for process termination // wait for process termination
WaitForSingleObject(pi->hProcess, INFINITE); WaitForSingleObject(pi->hProcess, INFINITE);
for (i = 0; i < MAX_PROCS; ++i) { for (int i = 0; i < MAX_PROCS; i++) {
if (pInfo[i].pid == pi->dwProcessId) { if (pInfo[i].pid == pi->dwProcessId) {
cleanUpProcBlock(pInfo + i); cleanUpProcBlock(pInfo + i);
if (isTraceEnabled(CDT_TRACE_MONITOR)) { if (isTraceEnabled(CDT_TRACE_MONITOR)) {
@ -841,14 +833,10 @@ void _cdecl waitProcTermination(void *pv) {
// Return :number of bytes used in target, or -1 in case of error // Return :number of bytes used in target, or -1 in case of error
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace) { int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace) {
BOOL bSlash = FALSE; bool bSlash = false;
int i = 0, j = 0; int i = 0, j = 0;
#define QUOTATION_DO 0 enum { QUOTATION_DO, QUOTATION_DONE, QUOTATION_NONE } nQuotationMode = QUOTATION_DO;
#define QUOTATION_DONE 1
#define QUOTATION_NONE 2
int nQuotationMode = 0;
if (availSpace <= cpyLength) { // = to reserve space for final '\0' if (availSpace <= cpyLength) { // = to reserve space for final '\0'
return -1; return -1;
@ -856,19 +844,19 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) { if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) {
nQuotationMode = QUOTATION_DONE; nQuotationMode = QUOTATION_DONE;
} else if (wcschr(source, _T(' ')) == NULL) { } else if (wcschr(source, _T(' '))) {
// No reason to quote term because it doesn't have embedded spaces
nQuotationMode = QUOTATION_NONE;
} else {
// Needs to be quoted // Needs to be quoted
nQuotationMode = QUOTATION_DO; nQuotationMode = QUOTATION_DO;
*target = _T('\"'); *target = _T('\"');
++j; ++j;
} else {
// No reason to quote term because it doesn't have embedded spaces
nQuotationMode = QUOTATION_NONE;
} }
for (; i < cpyLength; ++i, ++j) { for (; i < cpyLength; ++i, ++j) {
if (source[i] == _T('\\')) { if (source[i] == _T('\\')) {
bSlash = TRUE; bSlash = true;
} else { } else {
// Don't escape embracing quotation marks // Don't escape embracing quotation marks
if ((source[i] == _T('\"')) && if ((source[i] == _T('\"')) &&
@ -881,7 +869,7 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
++j; ++j;
} }
} }
bSlash = FALSE; bSlash = false;
} }
if (j == availSpace) { if (j == availSpace) {

View file

@ -30,19 +30,19 @@ void ThrowByName(JNIEnv *env, const char *name, const char *msg);
#define BUFF_SIZE (1024) #define BUFF_SIZE (1024)
static HANDLE channelToHandle(JNIEnv *env, jobject channel) { static HANDLE channelToHandle(JNIEnv *env, jobject channel) {
if (channel == 0) { if (!channel) {
ThrowByName(env, "java/io/IOException", "Invalid channel object"); ThrowByName(env, "java/io/IOException", "Invalid channel object");
return NULL; return NULL;
} }
jclass cls = (*env)->GetObjectClass(env, channel); jclass cls = (*env)->GetObjectClass(env, channel);
if (cls == NULL) { if (!cls) {
ThrowByName(env, "java/io/IOException", "Unable to get channel class"); ThrowByName(env, "java/io/IOException", "Unable to get channel class");
return NULL; return NULL;
} }
jfieldID fid = (*env)->GetFieldID(env, cls, "handle", "J"); jfieldID fid = (*env)->GetFieldID(env, cls, "handle", "J");
if (fid == NULL) { if (!fid) {
ThrowByName(env, "java/io/IOException", "Unable to find handle"); ThrowByName(env, "java/io/IOException", "Unable to find handle");
return NULL; return NULL;
} }
@ -69,7 +69,7 @@ extern "C"
TRUE, // initial state = signaled TRUE, // initial state = signaled
NULL); // unnamed event object NULL); // unnamed event object
if (NULL == overlapped.hEvent) { if (!overlapped.hEvent) {
char *lpMsgBuf; char *lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language

View file

@ -39,7 +39,7 @@ JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_pty_PTY_openMaster(JNIEnv *
/* Open new winpty handle */ /* Open new winpty handle */
winpty_t *winpty = winpty_open(80, 40); winpty_t *winpty = winpty_open(80, 40);
if (winpty == NULL) { if (!winpty) {
return NULL; return NULL;
} }
@ -67,7 +67,7 @@ JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_pty_PTY_openMaster(JNIEnv *
/* Set the master fd. */ /* Set the master fd. */
fid = env->GetFieldID(cls, "master", "I"); fid = env->GetFieldID(cls, "master", "I");
if (fid == NULL) { if (!fid) {
return NULL; return NULL;
} }
env->SetIntField(jobj, fid, (jint)master); env->SetIntField(jobj, fid, (jint)master);
@ -87,7 +87,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_change_1window_1size(J
fd2pty_Iter = fd2pty.find(fd); fd2pty_Iter = fd2pty.find(fd);
if (fd2pty_Iter != fd2pty.end()) { if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second; winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) { if (winpty) {
return winpty_set_size(winpty, width, height); return winpty_set_size(winpty, width, height);
} }
} }
@ -106,7 +106,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYInputStream_read0(JNIEn
fd2pty_Iter = fd2pty.find(fd); fd2pty_Iter = fd2pty.find(fd);
if (fd2pty_Iter != fd2pty.end()) { if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second; winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) { if (winpty) {
/* Get the pipe handle */ /* Get the pipe handle */
HANDLE handle = winpty_get_data_pipe(winpty); HANDLE handle = winpty_get_data_pipe(winpty);
@ -159,7 +159,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYInputStream_close0(JNIE
if (fd2pty_Iter != fd2pty.end()) { if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second; winpty_t *winpty = fd2pty_Iter->second;
fd2pty.erase(fd2pty_Iter); fd2pty.erase(fd2pty_Iter);
if (winpty != NULL) { if (winpty) {
winpty_close(winpty); winpty_close(winpty);
winpty = NULL; winpty = NULL;
} }
@ -179,7 +179,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYOutputStream_write0(JNI
fd2pty_Iter = fd2pty.find(fd); fd2pty_Iter = fd2pty.find(fd);
if (fd2pty_Iter != fd2pty.end()) { if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second; winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) { if (winpty) {
/* Get the pipe handle */ /* Get the pipe handle */
HANDLE handle = winpty_get_data_pipe(winpty); HANDLE handle = winpty_get_data_pipe(winpty);
@ -218,7 +218,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYOutputStream_close0(JNI
if (fd2pty_Iter != fd2pty.end()) { if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second; winpty_t *winpty = fd2pty_Iter->second;
fd2pty.erase(fd2pty_Iter); fd2pty.erase(fd2pty_Iter);
if (winpty != NULL) { if (winpty) {
winpty_close(winpty); winpty_close(winpty);
winpty = NULL; winpty = NULL;
} }
@ -253,7 +253,7 @@ static std::wstring argvToCommandLine(const std::vector<std::wstring> &argv) {
result.push_back(L' '); result.push_back(L' ');
} }
const wchar_t *arg = argv[argIndex].c_str(); const wchar_t *arg = argv[argIndex].c_str();
const bool quote = wcschr(arg, L' ') != NULL || wcschr(arg, L'\t') != NULL || *arg == L'\0'; const bool quote = wcschr(arg, L' ') || wcschr(arg, L'\t') || *arg == L'\0';
if (quote) { if (quote) {
result.push_back(L'\"'); result.push_back(L'\"');
} }
@ -292,11 +292,10 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_exec2(JNIEnv *env, job
int pid = -1; int pid = -1;
int i;
jint argc = env->GetArrayLength(jcmd); jint argc = env->GetArrayLength(jcmd);
jint envc = env->GetArrayLength(jenv); jint envc = env->GetArrayLength(jenv);
if (jchannels == NULL || env->GetArrayLength(jchannels) != 3) { if (!jchannels || env->GetArrayLength(jchannels) != 3) {
goto bail_out; goto bail_out;
} }
@ -304,10 +303,10 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_exec2(JNIEnv *env, job
fd2pty_Iter = fd2pty.find(fd); fd2pty_Iter = fd2pty.find(fd);
if (fd2pty_Iter != fd2pty.end()) { if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second; winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) { if (winpty) {
std::vector<std::wstring> argVector; std::vector<std::wstring> argVector;
for (i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
jstring j_str = (jstring)env->GetObjectArrayElement(jcmd, i); jstring j_str = (jstring)env->GetObjectArrayElement(jcmd, i);
const wchar_t *w_str = (const wchar_t *)env->GetStringChars(j_str, NULL); const wchar_t *w_str = (const wchar_t *)env->GetStringChars(j_str, NULL);
if (i == 0) { if (i == 0) {
@ -321,7 +320,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_exec2(JNIEnv *env, job
std::wstring envp; std::wstring envp;
for (i = 0; i < envc; i++) { for (int i = 0; i < envc; i++) {
jstring j_str = (jstring)env->GetObjectArrayElement(jenv, i); jstring j_str = (jstring)env->GetObjectArrayElement(jenv, i);
const wchar_t *w_str = (const wchar_t *)env->GetStringChars(j_str, NULL); const wchar_t *w_str = (const wchar_t *)env->GetStringChars(j_str, NULL);
envp.append(w_str); envp.append(w_str);
@ -361,7 +360,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_waitFor(JNIEnv *env, j
fd2pty_Iter = fd2pty.find(fd); fd2pty_Iter = fd2pty.find(fd);
if (fd2pty_Iter != fd2pty.end()) { if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second; winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) { if (winpty) {
HANDLE handle = winpty_get_data_pipe(winpty); HANDLE handle = winpty_get_data_pipe(winpty);
BOOL success; BOOL success;
do { do {

View file

@ -74,7 +74,7 @@ typedef BOOL(WINAPI *DebugBreakProcessFunc)(HANDLE);
int interruptProcess(int pid) { int interruptProcess(int pid) {
// See if DebugBreakProcess is available (XP and beyond) // See if DebugBreakProcess is available (XP and beyond)
HMODULE hmod = LoadLibrary(L"Kernel32.dll"); HMODULE hmod = LoadLibrary(L"Kernel32.dll");
if (hmod != NULL) { if (hmod) {
BOOL success = FALSE; BOOL success = FALSE;
FARPROC procaddr = GetProcAddress(hmod, "DebugBreakProcess"); FARPROC procaddr = GetProcAddress(hmod, "DebugBreakProcess");
if (procaddr != NULL) { if (procaddr != NULL) {
@ -103,7 +103,7 @@ int interruptProcess(int pid) {
// Find console // Find console
EnumWindows(find_child_console, (LPARAM)pid); EnumWindows(find_child_console, (LPARAM)pid);
if (NULL != consoleHWND) { // Yes, we found out it if (consoleHWND) { // Yes, we found out it
// We are going to switch focus to console, // We are going to switch focus to console,
// send Ctrl-C and then restore focus // send Ctrl-C and then restore focus
BYTE control_scan_code = (BYTE)MapVirtualKey(VK_CONTROL, 0); BYTE control_scan_code = (BYTE)MapVirtualKey(VK_CONTROL, 0);

View file

@ -36,7 +36,7 @@ extern "C"
InitializeCriticalSection(&cs); InitializeCriticalSection(&cs);
GetModuleFileNameW(hModule, path, MAX_PATH); GetModuleFileNameW(hModule, path, MAX_PATH);
p = wcsrchr(path, _T('\\')); p = wcsrchr(path, _T('\\'));
if (NULL != p) { if (p) {
*(p + 1) = _T('\0'); *(p + 1) = _T('\0');
} else { } else {
wcscat(path, L"\\"); wcscat(path, L"\\");

View file

@ -66,7 +66,7 @@ bool _isCygwin = true;
bool isCygwin(HANDLE process) { bool isCygwin(HANDLE process) {
// Have we checked before? // Have we checked before?
if (cygwinBin != NULL || !_isCygwin) { if (cygwinBin || !_isCygwin) {
return _isCygwin; return _isCygwin;
} }
@ -128,10 +128,10 @@ void ensureSize(wchar_t **ptr, int *psize, int requiredLength) {
size = requiredLength; size = requiredLength;
} }
*ptr = (wchar_t *)realloc(*ptr, size * sizeof(wchar_t)); *ptr = (wchar_t *)realloc(*ptr, size * sizeof(wchar_t));
if (NULL == *ptr) { if (*ptr) {
*psize = 0;
} else {
*psize = size; *psize = size;
} else {
*psize = 0;
} }
} }
} }
@ -164,7 +164,7 @@ int main() {
return 0; return 0;
} }
ensureSize(&szCmdLine, &nCmdLineLength, requiredSize); ensureSize(&szCmdLine, &nCmdLineLength, requiredSize);
if (NULL == szCmdLine) { if (!szCmdLine) {
if (isTraceEnabled(CDT_TRACE_MONITOR)) { if (isTraceEnabled(CDT_TRACE_MONITOR)) {
cdtTrace(L"Not enough memory to build cmd line!\n"); cdtTrace(L"Not enough memory to build cmd line!\n");
} }
@ -255,10 +255,7 @@ int main() {
if (isTraceEnabled(CDT_TRACE_MONITOR_DETAILS)) { if (isTraceEnabled(CDT_TRACE_MONITOR_DETAILS)) {
wchar_t *lpvEnv = GetEnvironmentStringsW(); wchar_t *lpvEnv = GetEnvironmentStringsW();
// If the returned pointer is NULL, exit. if (lpvEnv) {
if (lpvEnv == NULL) {
cdtTrace(L"Cannot Read Environment\n");
} else {
// Variable strings are separated by NULL byte, and the block is // Variable strings are separated by NULL byte, and the block is
// terminated by a NULL byte. // terminated by a NULL byte.
@ -268,6 +265,9 @@ int main() {
} }
FreeEnvironmentStringsW(lpvEnv); FreeEnvironmentStringsW(lpvEnv);
} else {
// If the returned pointer is NULL, exit.
cdtTrace(L"Cannot Read Environment\n");
} }
} }
if (isTraceEnabled(CDT_TRACE_MONITOR)) { if (isTraceEnabled(CDT_TRACE_MONITOR)) {
@ -275,7 +275,7 @@ int main() {
} }
// Create job object // Create job object
HANDLE hJob = CreateJobObject(NULL, NULL); HANDLE hJob = CreateJobObject(NULL, NULL);
if (hJob != NULL) { if (hJob) {
// Configure job to // Configure job to
// - terminate all associated processes when the last handle to it is closed // - terminate all associated processes when the last handle to it is closed
// - allow child processes to break away from the job. // - allow child processes to break away from the job.
@ -315,7 +315,7 @@ int main() {
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
h[1] = pi.hProcess; h[1] = pi.hProcess;
if (NULL != hJob) { if (hJob) {
if (!AssignProcessToJobObject(hJob, pi.hProcess)) { if (!AssignProcessToJobObject(hJob, pi.hProcess)) {
if (isTraceEnabled(CDT_TRACE_MONITOR)) { if (isTraceEnabled(CDT_TRACE_MONITOR)) {
cdtTrace(L"Cannot assign process %i to a job\n", pi.dwProcessId); cdtTrace(L"Cannot assign process %i to a job\n", pi.dwProcessId);
@ -383,7 +383,7 @@ int main() {
SetEvent(waitEvent); SetEvent(waitEvent);
if (NULL != hJob) { if (hJob) {
if (!TerminateJobObject(hJob, (DWORD)-1)) { if (!TerminateJobObject(hJob, (DWORD)-1)) {
if (isTraceEnabled(CDT_TRACE_MONITOR)) { if (isTraceEnabled(CDT_TRACE_MONITOR)) {
cdtTrace(L"Cannot terminate job\n"); cdtTrace(L"Cannot terminate job\n");
@ -433,14 +433,11 @@ int main() {
// Return :number of bytes used in target, or -1 in case of error // Return :number of bytes used in target, or -1 in case of error
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace) { int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace) {
BOOL bSlash = FALSE; bool bSlash = false;
int i = 0, j = 0; int i = 0, j = 0;
#define QUOTATION_DO 0 enum { QUOTATION_DO, QUOTATION_DONE, QUOTATION_NONE } nQuotationMode = QUOTATION_DO;
#define QUOTATION_DONE 1
#define QUOTATION_NONE 2
int nQuotationMode = 0;
if (availSpace <= cpyLength) { // = to reserve space for '\0' if (availSpace <= cpyLength) { // = to reserve space for '\0'
return -1; return -1;
} }
@ -448,19 +445,19 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) { if ((_T('\"') == *source) && (_T('\"') == *(source + cpyLength - 1))) {
// Already done // Already done
nQuotationMode = QUOTATION_DONE; nQuotationMode = QUOTATION_DONE;
} else if (wcschr(source, _T(' ')) == NULL) { } else if (wcschr(source, _T(' '))) {
// No reason to quotate term becase it doesn't have embedded spaces
nQuotationMode = QUOTATION_NONE;
} else {
// Needs to be quotated // Needs to be quotated
nQuotationMode = QUOTATION_DO; nQuotationMode = QUOTATION_DO;
*target = _T('\"'); *target = _T('\"');
++j; ++j;
} else {
// No reason to quotate term because it doesn't have embedded spaces
nQuotationMode = QUOTATION_NONE;
} }
for (; i < cpyLength; ++i, ++j) { for (; i < cpyLength; ++i, ++j) {
if (source[i] == _T('\\')) { if (source[i] == _T('\\')) {
bSlash = TRUE; bSlash = true;
} else { } else {
// Don't escape embracing quotation marks // Don't escape embracing quotation marks
if ((source[i] == _T('\"')) && if ((source[i] == _T('\"')) &&
@ -472,9 +469,9 @@ int copyTo(wchar_t *target, const wchar_t *source, int cpyLength, int availSpace
target[j] = _T('\\'); target[j] = _T('\\');
++j; ++j;
} }
bSlash = FALSE; bSlash = false;
} else { } else {
bSlash = FALSE; bSlash = false;
} }
} }