Skip to content

Commit b448cd1

Browse files
Avoid Duplicattion
Move pclose to the end of function.
1 parent 02a7184 commit b448cd1

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

kubernetes/config/exec_provider.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,8 @@ int kube_exec_and_get_result(ExecCredential_t * exec_credential, const kubeconfi
7575
result_string = calloc(1, KUBECONFIG_EXEC_RESULT_BUFFER_SIZE);
7676
if (!result_string) {
7777
fprintf(stderr, "%s: Cannot allocate memory for command result.[%s]\n", fname, strerror(errno));
78-
#ifndef _WIN32
79-
pclose(fp);
80-
#else
81-
_pclose(fp);
82-
#endif
83-
return -1;
78+
rc = -1;
79+
goto end;
8480
}
8581
int result_string_remaining_size = KUBECONFIG_EXEC_RESULT_BUFFER_SIZE - 1;
8682
char string_buf[KUBECONFIG_STRING_BUFFER_SIZE];
@@ -90,21 +86,11 @@ int kube_exec_and_get_result(ExecCredential_t * exec_credential, const kubeconfi
9086
if (result_string_remaining_size <= 0) {
9187
fprintf(stderr, "%s: The buffer for exec result is not sufficient.\n", fname);
9288
rc = -1;
93-
#ifndef _WIN32
94-
pclose(fp);
95-
#else
96-
_pclose(fp);
97-
#endif
9889
goto end;
9990
}
10091
strncat(result_string, string_buf, strlen(string_buf));
10192
memset(string_buf, 0, sizeof(string_buf));
10293
}
103-
#ifndef _WIN32
104-
pclose(fp);
105-
#else
106-
_pclose(fp);
107-
#endif
10894
} else {
10995
fprintf(stderr, "%s: Cannot open pipe to run command.[%s]\n", fname, strerror(errno));
11096
return -1;
@@ -115,7 +101,16 @@ int kube_exec_and_get_result(ExecCredential_t * exec_credential, const kubeconfi
115101
end:
116102
if (result_string) {
117103
free(result_string);
104+
result_string = NULL;
118105
}
119106

107+
if (fp) {
108+
#ifndef _WIN32
109+
pclose(fp);
110+
#else
111+
_pclose(fp);
112+
#endif
113+
fp = NULL;
114+
}
120115
return rc;
121116
}

0 commit comments

Comments
 (0)