tool_operate: fix memory mixups

Switch to plain getenv() from curl_getenv() to avoid the allocation and
having to keep track of which free() or curl_free() that need to be
used.

Coverity found issues and a memory leak.

Follow-up to 65b563a96a

Closes #11959
This commit is contained in:
Daniel Stenberg 2023-09-27 09:51:46 +02:00
parent a7f8d04ee0
commit 93885cf3a8
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -704,28 +704,31 @@ static char *ipfs_gateway(void)
char *gateway_composed_file_path = NULL; char *gateway_composed_file_path = NULL;
FILE *gateway_file = NULL; FILE *gateway_file = NULL;
gateway = curlx_getenv("IPFS_GATEWAY"); gateway = getenv("IPFS_GATEWAY");
/* Gateway is found from environment variable. */ /* Gateway is found from environment variable. */
if(gateway && strlen(gateway)) { if(gateway && *gateway) {
char *composed_gateway = NULL; char *composed_gateway = NULL;
bool add_slash = (gateway[strlen(gateway) - 1] == '/') ? FALSE : TRUE; bool add_slash = (gateway[strlen(gateway) - 1] != '/');
composed_gateway = aprintf("%s%s", gateway, (add_slash) ? "/" : ""); composed_gateway = aprintf("%s%s", gateway, (add_slash) ? "/" : "");
Curl_safefree(gateway); if(composed_gateway) {
gateway = aprintf("%s", composed_gateway); gateway = aprintf("%s", composed_gateway);
Curl_safefree(composed_gateway); Curl_safefree(composed_gateway);
}
return gateway; return gateway;
} }
else
/* a blank string does not count */
gateway = NULL;
/* Try to find the gateway in the IPFS data folder. */ /* Try to find the gateway in the IPFS data folder. */
ipfs_path = curlx_getenv("IPFS_PATH"); ipfs_path = getenv("IPFS_PATH");
if(!ipfs_path) { if(!ipfs_path) {
char *home = NULL; char *home = getenv("HOME");
home = curlx_getenv("HOME"); if(home && *home)
/* Empty path, fallback to "~/.ipfs", as that's the default location. */ ipfs_path = aprintf("%s/.ipfs/", home);
ipfs_path = aprintf("%s/.ipfs/", home); /* fallback to "~/.ipfs", as that's the default location. */
Curl_safefree(home);
} }
if(!ipfs_path) { if(!ipfs_path) {
@ -750,10 +753,7 @@ static char *ipfs_gateway(void)
if((PARAM_OK == file2string(&gateway_buffer, gateway_file)) && if((PARAM_OK == file2string(&gateway_buffer, gateway_file)) &&
gateway_buffer) { gateway_buffer) {
bool add_slash = (gateway_buffer[strlen(gateway_buffer) - 1] == '/') bool add_slash = (gateway_buffer[strlen(gateway_buffer) - 1] != '/');
? FALSE
: TRUE;
gateway = aprintf("%s%s", gateway_buffer, (add_slash) ? "/" : ""); gateway = aprintf("%s%s", gateway_buffer, (add_slash) ? "/" : "");
Curl_safefree(gateway_buffer); Curl_safefree(gateway_buffer);
} }