1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +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);
if (channels != NULL) {
if (channels) {
int fds;
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) {
execv(full_path, argv);
} else {
if (envp && envp[0]) {
execve(full_path, argv, envp);
} else {
execv(full_path, argv);
}
_exit(127);
@ -128,7 +128,7 @@ pid_t exec_pty(const char *path, char *const argv[], char *const envp[], const c
if (console) {
set_noecho(fdm);
}
if (channels != NULL) {
if (channels) {
channels[0] = fdm; /* Input Stream. */
channels[1] = fdm; /* Output Stream. */
if (console) {
@ -173,10 +173,10 @@ int main(int argc, char **argv, char **envp) {
} else {
fputs("foo\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);
}
while (fgets(buffer, sizeof buffer, app_stderr) != NULL) {
while (fgets(buffer, sizeof buffer, app_stderr)) {
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.
*/
if (channels != NULL) {
if (channels) {
if (pipe(pipe0) < 0 || pipe(pipe1) < 0 || pipe(pipe2) < 0) {
fprintf(stderr, "%s(%d): returning due to error.\n", __func__, __LINE__);
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 */
chdir(dirpath);
if (channels != NULL) {
if (channels) {
/* Close the write end of pipe0 */
if (close(pipe0[1]) == -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());
if (envp[0] == NULL) {
execv(full_path, argv);
} else {
if (envp && envp[0]) {
execve(full_path, argv, envp);
} else {
execv(full_path, argv);
}
_exit(127);
} else if (childpid != 0) { /* parent */
if (channels != NULL) {
if (channels) {
/* close the read end of pipe1 */
if (close(pipe0[0]) == -1) {
perror("close(pipe0[0])");

View file

@ -24,7 +24,7 @@
static void ThrowByName(JNIEnv *env, const char *name, const char *msg) {
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);
}

View file

@ -33,12 +33,11 @@
const int path_def_len = 5; /* strlen(PATH_DEF); */
char *path_val(char *const envp[]) {
int i;
if (envp == NULL || envp[0] == NULL) {
if (!envp || !envp[0]) {
return getenv("PATH");
}
for (i = 0; envp[i] != NULL; i++) {
for (int i = 0; envp[i]; i++) {
char *p = envp[i];
if (!strncmp(PATH_DEF, p, path_def_len)) {
return p + path_def_len;
@ -56,7 +55,7 @@ char *pfind(const char *name, char *const envp[]) {
struct stat sb;
/* Sanity check. */
if (name == NULL) {
if (!name) {
fprintf(stderr, "pfind(): Null argument.\n");
return NULL;
}
@ -72,7 +71,7 @@ char *pfind(const char *name, char *const envp[]) {
/* Search in the PATH environment. */
path = path_val(envp);
if (path == NULL || strlen(path) <= 0) {
if (!path || strlen(path) <= 0) {
fprintf(stderr, "Unable to get $PATH.\n");
return NULL;
}
@ -81,7 +80,7 @@ char *pfind(const char *name, char *const envp[]) {
path = strdup(path);
tok = strtok_r(path, ":", &sp);
while (tok != NULL) {
while (tok) {
snprintf(fullpath, sizeof(fullpath) - 1, "%s/%s", tok, name);
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
int main(int argc, char **argv) {
int i;
char *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 {
for (int i = 1; i < argc; i++) {
char *fullpath = pfind(argv[i], NULL);
if (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. */
fid = (*env)->GetFieldID(env, cls, "master", "I");
if (fid == NULL) {
if (!fid) {
return NULL;
}
(*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];
pid_t pid = -1;
if (jchannels == NULL) {
if (!jchannels) {
goto bail_out;
}
cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) {
if (!cmd) {
goto bail_out;
}
envp = alloc_c_array(env, jenv);
if (envp == NULL) {
if (!envp) {
goto bail_out;
}
@ -151,12 +151,12 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec1(JNIEnv *
pid_t pid = -1;
cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) {
if (!cmd) {
goto bail_out;
}
envp = alloc_c_array(env, jenv);
if (envp == NULL) {
if (!envp) {
goto bail_out;
}
@ -189,7 +189,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_spawner_Spawner_exec0(JNIEnv *
jclass channelClass = NULL;
jmethodID channelConstructor = NULL;
if (jchannels == NULL) {
if (!jchannels) {
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");
if (channelConstructor == 0) {
if (!channelConstructor) {
goto bail_out;
}
cmd = alloc_c_array(env, jcmd);
if (cmd == NULL) {
if (!cmd) {
goto bail_out;
}
envp = alloc_c_array(env, jenv);
if (envp == NULL) {
if (!envp) {
goto bail_out;
}

View file

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

View file

@ -30,19 +30,19 @@ void ThrowByName(JNIEnv *env, const char *name, const char *msg);
#define BUFF_SIZE (1024)
static HANDLE channelToHandle(JNIEnv *env, jobject channel) {
if (channel == 0) {
if (!channel) {
ThrowByName(env, "java/io/IOException", "Invalid channel object");
return NULL;
}
jclass cls = (*env)->GetObjectClass(env, channel);
if (cls == NULL) {
if (!cls) {
ThrowByName(env, "java/io/IOException", "Unable to get channel class");
return NULL;
}
jfieldID fid = (*env)->GetFieldID(env, cls, "handle", "J");
if (fid == NULL) {
if (!fid) {
ThrowByName(env, "java/io/IOException", "Unable to find handle");
return NULL;
}
@ -69,7 +69,7 @@ extern "C"
TRUE, // initial state = signaled
NULL); // unnamed event object
if (NULL == overlapped.hEvent) {
if (!overlapped.hEvent) {
char *lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
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 */
winpty_t *winpty = winpty_open(80, 40);
if (winpty == NULL) {
if (!winpty) {
return NULL;
}
@ -67,7 +67,7 @@ JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_pty_PTY_openMaster(JNIEnv *
/* Set the master fd. */
fid = env->GetFieldID(cls, "master", "I");
if (fid == NULL) {
if (!fid) {
return NULL;
}
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);
if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) {
if (winpty) {
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);
if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) {
if (winpty) {
/* Get the pipe handle */
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()) {
winpty_t *winpty = fd2pty_Iter->second;
fd2pty.erase(fd2pty_Iter);
if (winpty != NULL) {
if (winpty) {
winpty_close(winpty);
winpty = NULL;
}
@ -179,7 +179,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTYOutputStream_write0(JNI
fd2pty_Iter = fd2pty.find(fd);
if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) {
if (winpty) {
/* Get the pipe handle */
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()) {
winpty_t *winpty = fd2pty_Iter->second;
fd2pty.erase(fd2pty_Iter);
if (winpty != NULL) {
if (winpty) {
winpty_close(winpty);
winpty = NULL;
}
@ -253,7 +253,7 @@ static std::wstring argvToCommandLine(const std::vector<std::wstring> &argv) {
result.push_back(L' ');
}
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) {
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 i;
jint argc = env->GetArrayLength(jcmd);
jint envc = env->GetArrayLength(jenv);
if (jchannels == NULL || env->GetArrayLength(jchannels) != 3) {
if (!jchannels || env->GetArrayLength(jchannels) != 3) {
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);
if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) {
if (winpty) {
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);
const wchar_t *w_str = (const wchar_t *)env->GetStringChars(j_str, NULL);
if (i == 0) {
@ -321,7 +320,7 @@ JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_pty_PTY_exec2(JNIEnv *env, job
std::wstring envp;
for (i = 0; i < envc; i++) {
for (int i = 0; i < envc; i++) {
jstring j_str = (jstring)env->GetObjectArrayElement(jenv, i);
const wchar_t *w_str = (const wchar_t *)env->GetStringChars(j_str, NULL);
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);
if (fd2pty_Iter != fd2pty.end()) {
winpty_t *winpty = fd2pty_Iter->second;
if (winpty != NULL) {
if (winpty) {
HANDLE handle = winpty_get_data_pipe(winpty);
BOOL success;
do {

View file

@ -74,7 +74,7 @@ typedef BOOL(WINAPI *DebugBreakProcessFunc)(HANDLE);
int interruptProcess(int pid) {
// See if DebugBreakProcess is available (XP and beyond)
HMODULE hmod = LoadLibrary(L"Kernel32.dll");
if (hmod != NULL) {
if (hmod) {
BOOL success = FALSE;
FARPROC procaddr = GetProcAddress(hmod, "DebugBreakProcess");
if (procaddr != NULL) {
@ -103,7 +103,7 @@ int interruptProcess(int pid) {
// Find console
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,
// send Ctrl-C and then restore focus
BYTE control_scan_code = (BYTE)MapVirtualKey(VK_CONTROL, 0);

View file

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

View file

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