Implement all min/max functions for (un)signed int types.

Also, use them in the `onion_client` module.
This commit is contained in:
iphydf 2019-01-12 13:12:39 +00:00
parent 3dd31b1fd0
commit 91ff39599d
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
4 changed files with 58 additions and 13 deletions

View File

@ -433,13 +433,13 @@ static void loglogdata(const Logger *log, const char *message, const uint8_t *bu
int error = net_error();
const char *strerror = net_new_strerror(error);
LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x",
buffer[0], message, (buflen < 999 ? buflen : 999), 'E',
buffer[0], message, min_u16(buflen, 999), 'E',
ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), error,
strerror, data_0(buflen, buffer), data_1(buflen, buffer));
net_kill_strerror(strerror);
} else if ((res > 0) && ((size_t)res <= buflen)) {
LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x",
buffer[0], message, (res < 999 ? res : 999), ((size_t)res < buflen ? '<' : '='),
buffer[0], message, min_u16(res, 999), ((size_t)res < buflen ? '<' : '='),
ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK",
data_0(buflen, buffer), data_1(buflen, buffer));
} else { /* empty or overwrite */

View File

@ -236,7 +236,7 @@ uint16_t onion_backup_nodes(const Onion_Client *onion_c, Node_format *nodes, uin
return 0;
}
const uint16_t num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES);
uint16_t i = 0;
while (i < max_num && i < num_nodes) {
@ -275,7 +275,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
return 0;
}
const uint32_t num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES);
// if (dht_non_lan_connected(onion_c->dht)) {
if (dht_isconnected(onion_c->dht)) {
@ -301,8 +301,7 @@ static uint16_t random_nodes_path_onion(const Onion_Client *onion_c, Node_format
nodes[i] = onion_c->path_nodes[random_u32() % num_nodes];
}
} else {
unsigned int num_nodes_bs = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs :
MAX_PATH_NODES;
const uint16_t num_nodes_bs = min_u16(onion_c->path_nodes_index_bs, MAX_PATH_NODES);
if (num_nodes_bs == 0) {
return 0;
@ -1619,9 +1618,8 @@ static void do_friend(Onion_Client *onion_c, uint16_t friendnum)
}
if (count != MAX_ONION_CLIENTS) {
unsigned int num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
unsigned int n = num_nodes;
const uint16_t num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES);
uint16_t n = num_nodes;
if (num_nodes > (MAX_ONION_CLIENTS / 2)) {
n = (MAX_ONION_CLIENTS / 2);
@ -1740,14 +1738,14 @@ static void do_announce(Onion_Client *onion_c)
}
if (count != MAX_ONION_CLIENTS_ANNOUNCE) {
unsigned int num_nodes;
uint16_t num_nodes;
Node_format *path_nodes;
if (random_u08() % 2 == 0 || onion_c->path_nodes_index == 0) {
num_nodes = (onion_c->path_nodes_index_bs < MAX_PATH_NODES) ? onion_c->path_nodes_index_bs : MAX_PATH_NODES;
num_nodes = min_u16(onion_c->path_nodes_index_bs, MAX_PATH_NODES);
path_nodes = onion_c->path_nodes_bs;
} else {
num_nodes = (onion_c->path_nodes_index < MAX_PATH_NODES) ? onion_c->path_nodes_index : MAX_PATH_NODES;
num_nodes = min_u16(onion_c->path_nodes_index, MAX_PATH_NODES);
path_nodes = onion_c->path_nodes;
}

View File

@ -95,21 +95,53 @@ int create_recursive_mutex(pthread_mutex_t *mutex)
return 0;
}
int16_t max_s16(int16_t a, int16_t b)
{
return a > b ? a : b;
}
int32_t max_s32(int32_t a, int32_t b)
{
return a > b ? a : b;
}
int64_t max_s64(int64_t a, int64_t b)
{
return a > b ? a : b;
}
int16_t min_s16(int16_t a, int16_t b)
{
return a < b ? a : b;
}
int32_t min_s32(int32_t a, int32_t b)
{
return a < b ? a : b;
}
int64_t min_s64(int64_t a, int64_t b)
{
return a < b ? a : b;
}
uint16_t max_u16(uint16_t a, uint16_t b)
{
return a > b ? a : b;
}
uint32_t max_u32(uint32_t a, uint32_t b)
{
return a > b ? a : b;
}
uint64_t max_u64(uint64_t a, uint64_t b)
{
return a > b ? a : b;
}
uint16_t min_u16(uint16_t a, uint16_t b)
{
return a < b ? a : b;
}
uint32_t min_u32(uint32_t a, uint32_t b)
{
return a < b ? a : b;
}
uint64_t min_u64(uint64_t a, uint64_t b)
{
return a < b ? a : b;

View File

@ -46,8 +46,23 @@ void net_to_host(uint8_t *num, uint16_t numbytes);
/* Returns -1 if failed or 0 if success */
int create_recursive_mutex(pthread_mutex_t *mutex);
// Safe min/max functions with specific types. This forces the conversion to the
// desired type before the comparison expression, giving the choice of
// conversion to the caller. Use these instead of inline comparisons or MIN/MAX
// macros (effectively inline comparisons).
int16_t max_s16(int16_t a, int16_t b);
int32_t max_s32(int32_t a, int32_t b);
int64_t max_s64(int64_t a, int64_t b);
int16_t min_s16(int16_t a, int16_t b);
int32_t min_s32(int32_t a, int32_t b);
int64_t min_s64(int64_t a, int64_t b);
uint16_t max_u16(uint16_t a, uint16_t b);
uint32_t max_u32(uint32_t a, uint32_t b);
uint64_t max_u64(uint64_t a, uint64_t b);
uint16_t min_u16(uint16_t a, uint16_t b);
uint32_t min_u32(uint32_t a, uint32_t b);
uint64_t min_u64(uint64_t a, uint64_t b);