Avoid Duplicattion

Move pclose to the end of function.
This commit is contained in:
903d09f6-e821-4655-9e8b-7e6068b26f74
2024-07-23 19:22:56 +09:00
committed by GitHub
parent 02a71845ae
commit b448cd1147

View File

@@ -75,12 +75,8 @@ int kube_exec_and_get_result(ExecCredential_t * exec_credential, const kubeconfi
result_string = calloc(1, KUBECONFIG_EXEC_RESULT_BUFFER_SIZE); result_string = calloc(1, KUBECONFIG_EXEC_RESULT_BUFFER_SIZE);
if (!result_string) { if (!result_string) {
fprintf(stderr, "%s: Cannot allocate memory for command result.[%s]\n", fname, strerror(errno)); fprintf(stderr, "%s: Cannot allocate memory for command result.[%s]\n", fname, strerror(errno));
#ifndef _WIN32 rc = -1;
pclose(fp); goto end;
#else
_pclose(fp);
#endif
return -1;
} }
int result_string_remaining_size = KUBECONFIG_EXEC_RESULT_BUFFER_SIZE - 1; int result_string_remaining_size = KUBECONFIG_EXEC_RESULT_BUFFER_SIZE - 1;
char string_buf[KUBECONFIG_STRING_BUFFER_SIZE]; char string_buf[KUBECONFIG_STRING_BUFFER_SIZE];
@@ -90,21 +86,11 @@ int kube_exec_and_get_result(ExecCredential_t * exec_credential, const kubeconfi
if (result_string_remaining_size <= 0) { if (result_string_remaining_size <= 0) {
fprintf(stderr, "%s: The buffer for exec result is not sufficient.\n", fname); fprintf(stderr, "%s: The buffer for exec result is not sufficient.\n", fname);
rc = -1; rc = -1;
#ifndef _WIN32
pclose(fp);
#else
_pclose(fp);
#endif
goto end; goto end;
} }
strncat(result_string, string_buf, strlen(string_buf)); strncat(result_string, string_buf, strlen(string_buf));
memset(string_buf, 0, sizeof(string_buf)); memset(string_buf, 0, sizeof(string_buf));
} }
#ifndef _WIN32
pclose(fp);
#else
_pclose(fp);
#endif
} else { } else {
fprintf(stderr, "%s: Cannot open pipe to run command.[%s]\n", fname, strerror(errno)); fprintf(stderr, "%s: Cannot open pipe to run command.[%s]\n", fname, strerror(errno));
return -1; return -1;
@@ -115,7 +101,16 @@ int kube_exec_and_get_result(ExecCredential_t * exec_credential, const kubeconfi
end: end:
if (result_string) { if (result_string) {
free(result_string); free(result_string);
result_string = NULL;
} }
if (fp) {
#ifndef _WIN32
pclose(fp);
#else
_pclose(fp);
#endif
fp = NULL;
}
return rc; return rc;
} }