mirror of
https://github.com/transmission/transmission
synced 2025-01-30 19:03:04 +00:00
chore: fix more sonarcloud warnings (#1507)
* refactor: don't let realpath() alloc return memory * chore: silence array-bounds-read warning * chore: tidy memcmp of union
This commit is contained in:
parent
46cb0b5e65
commit
e74b3bad8b
4 changed files with 52 additions and 48 deletions
|
@ -329,7 +329,6 @@ char* tr_sys_path_resolve(char const* path, tr_error** error)
|
|||
TR_ASSERT(path != NULL);
|
||||
|
||||
char* ret = NULL;
|
||||
char* tmp = NULL;
|
||||
|
||||
#if defined(HAVE_CANONICALIZE_FILE_NAME)
|
||||
|
||||
|
@ -337,21 +336,9 @@ char* tr_sys_path_resolve(char const* path, tr_error** error)
|
|||
|
||||
#endif
|
||||
|
||||
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L
|
||||
|
||||
/* Better safe than sorry: realpath() officially supports NULL as destination
|
||||
starting off POSIX.1-2008. */
|
||||
|
||||
if (ret == NULL)
|
||||
{
|
||||
ret = realpath(path, NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
if (ret == NULL)
|
||||
{
|
||||
tmp = tr_new(char, PATH_MAX);
|
||||
char tmp[PATH_MAX];
|
||||
ret = realpath(path, tmp);
|
||||
|
||||
if (ret != NULL)
|
||||
|
@ -365,8 +352,6 @@ char* tr_sys_path_resolve(char const* path, tr_error** error)
|
|||
set_system_error(error, errno);
|
||||
}
|
||||
|
||||
tr_free(tmp);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -147,15 +147,15 @@ bool tr_address_from_string(tr_address* dst, char const* src)
|
|||
*/
|
||||
int tr_address_compare(tr_address const* a, tr_address const* b)
|
||||
{
|
||||
static int const sizes[2] = { sizeof(struct in_addr), sizeof(struct in6_addr) };
|
||||
|
||||
/* IPv6 addresses are always "greater than" IPv4 */
|
||||
// IPv6 addresses are always "greater than" IPv4
|
||||
if (a->type != b->type)
|
||||
{
|
||||
return a->type == TR_AF_INET ? 1 : -1;
|
||||
}
|
||||
|
||||
return memcmp(&a->addr, &b->addr, sizes[a->type]);
|
||||
return a->type == TR_AF_INET ?
|
||||
memcmp(&a->addr.addr4, &b->addr.addr4, sizeof(a->addr.addr4)) :
|
||||
memcmp(&a->addr.addr6.s6_addr, &b->addr.addr6.s6_addr, sizeof(a->addr.addr6.s6_addr));
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
|
|
|
@ -188,6 +188,11 @@ int tr_variantParseBenc(void const* buf_in, void const* bufend_in, tr_variant* t
|
|||
tr_ptrArray stack = TR_PTR_ARRAY_INIT;
|
||||
tr_quark key = 0;
|
||||
|
||||
if ((buf_in == NULL) || (bufend_in == NULL) || (top == NULL))
|
||||
{
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
tr_variantInit(top, 0);
|
||||
|
||||
while (buf != bufend)
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <assert.h>
|
||||
#include <ctype.h> /* isspace */
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -1707,15 +1708,13 @@ static void printTrackers(tr_variant* top)
|
|||
static void printSession(tr_variant* top)
|
||||
{
|
||||
tr_variant* args;
|
||||
char buf[128];
|
||||
char buf2[128];
|
||||
char buf3[128];
|
||||
|
||||
if (tr_variantDictFindDict(top, TR_KEY_arguments, &args))
|
||||
{
|
||||
int64_t i;
|
||||
bool boolVal;
|
||||
char const* str;
|
||||
char buf[128];
|
||||
|
||||
printf("VERSION\n");
|
||||
|
||||
|
@ -1821,6 +1820,9 @@ static void printSession(tr_variant* top)
|
|||
tr_variantDictFindReal(args, TR_KEY_seedRatioLimit, &seedRatioLimit) &&
|
||||
tr_variantDictFindBool(args, TR_KEY_seedRatioLimited, &seedRatioLimited))
|
||||
{
|
||||
char buf2[128];
|
||||
char buf3[128];
|
||||
|
||||
printf("LIMITS\n");
|
||||
printf(" Peer limit: %" PRId64 "\n", peerLimit);
|
||||
|
||||
|
@ -2992,6 +2994,23 @@ static int processArgs(char const* rpcurl, int argc, char const* const* argv)
|
|||
return status;
|
||||
}
|
||||
|
||||
static bool parsePortString(char const* s, int* port)
|
||||
{
|
||||
int const errno_stack = errno;
|
||||
errno = 0;
|
||||
|
||||
char* end = NULL;
|
||||
int const i = (int)strtol(s, &end, 10);
|
||||
bool const ok = (end != NULL) && (*end == '\0') && (errno == 0);
|
||||
if (ok)
|
||||
{
|
||||
*port = i;
|
||||
}
|
||||
|
||||
errno = errno_stack;
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* [host:port] or [host] or [port] or [http(s?)://host:port/transmission/] */
|
||||
static void getHostAndPortAndRpcUrl(int* argc, char** argv, char** host, int* port, char** rpcurl)
|
||||
{
|
||||
|
@ -3001,6 +3020,7 @@ static void getHostAndPortAndRpcUrl(int* argc, char** argv, char** host, int* po
|
|||
}
|
||||
|
||||
char const* const s = argv[1];
|
||||
char const* const last_colon = strrchr(s, ':');
|
||||
|
||||
if (strncmp(s, "http://", 7) == 0) /* user passed in http rpc url */
|
||||
{
|
||||
|
@ -3011,39 +3031,33 @@ static void getHostAndPortAndRpcUrl(int* argc, char** argv, char** host, int* po
|
|||
UseSSL = true;
|
||||
*rpcurl = tr_strdup_printf("%s/rpc/", s + 8);
|
||||
}
|
||||
else if (parsePortString(s, port))
|
||||
{
|
||||
// it was just a port
|
||||
}
|
||||
else if (last_colon == NULL)
|
||||
{
|
||||
// it was a non-ipv6 host with no port
|
||||
*host = tr_strdup(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
char const* const first_colon = strchr(s, ':');
|
||||
char const* const last_colon = strrchr(s, ':');
|
||||
char const* hend;
|
||||
|
||||
if (last_colon != NULL && ((*s == '[' && *(last_colon - 1) == ']') || first_colon == last_colon))
|
||||
// if only one colon, it's probably "$host:$port"
|
||||
if ((strchr(s, ':') == last_colon) && parsePortString(last_colon + 1, port))
|
||||
{
|
||||
/* user passed in both host and port */
|
||||
*host = tr_strndup(s, last_colon - s);
|
||||
*port = atoi(last_colon + 1);
|
||||
hend = last_colon;
|
||||
}
|
||||
else
|
||||
{
|
||||
char* end;
|
||||
int const i = strtol(s, &end, 10);
|
||||
|
||||
if (*end == '\0') /* user passed in a port */
|
||||
{
|
||||
*port = i;
|
||||
}
|
||||
else /* user passed in a host */
|
||||
{
|
||||
if (last_colon != NULL && first_colon != last_colon && (*s != '[' || *(s + strlen(s) - 1) != ']'))
|
||||
{
|
||||
/* looks like IPv6; let's add brackets as we'll be appending the port later on */
|
||||
*host = tr_strdup_printf("[%s]", s);
|
||||
}
|
||||
else
|
||||
{
|
||||
*host = tr_strdup(s);
|
||||
}
|
||||
}
|
||||
hend = s + strlen(s);
|
||||
}
|
||||
|
||||
bool const is_unbracketed_ipv6 = (*s != '[') && (memchr(s, ':', hend - s) != NULL);
|
||||
|
||||
*host = is_unbracketed_ipv6 ? tr_strdup_printf("[%*s]", (int)(hend - s), s) :
|
||||
tr_strndup(s, hend - s);
|
||||
}
|
||||
|
||||
*argc -= 1;
|
||||
|
|
Loading…
Reference in a new issue