Skip to content

Commit

Permalink
Bug 568079: Cleanup of native code
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
T-Svensson committed Nov 3, 2020
1 parent 2857a7a commit c598eed
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 145 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified core/org.eclipse.cdt.core.macosx/os/macosx/x86/libspawner.jnilib
Binary file not shown.
Binary file not shown.
14 changes: 7 additions & 7 deletions core/org.eclipse.cdt.core.native/native_src/unix/exec_pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
12 changes: 6 additions & 6 deletions core/org.eclipse.cdt.core.native/native_src/unix/exec_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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])");
Expand Down Expand Up @@ -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])");
Expand Down
2 changes: 1 addition & 1 deletion core/org.eclipse.cdt.core.native/native_src/unix/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
24 changes: 10 additions & 14 deletions core/org.eclipse.cdt.core.native/native_src/unix/pfind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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 */
Expand All @@ -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]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/org.eclipse.cdt.core.native/native_src/unix/pty.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions core/org.eclipse.cdt.core.native/native_src/unix/spawner.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit c598eed

Please sign in to comment.