1
0
mirror of https://github.com/coturn/coturn.git synced 2025-10-25 13:00:59 +02:00

Address clang-tidy warnings in db files (#1405)

The general approach here was:

- Always declare variables as close to where they are defined as
possible.
- Check for pre-conditions of functions before doing work (e.g. ensure
we can connect to the DB before doing a bunch of string formatting)
- Keep the scope of mutexes as reasonably small as practical.
- Use idiomatic C11, such as for-loops over the thing being iterated,
not while() loops over constants, or variables that aren't modified.
- Prefer if(fail){return} function-body after over `if(not fail){
function-body inside if} return;

Clang-tidy returns a clean bill of health, but while going through this
file i noticed a lot of things that raise questions.

Lack of checking column counts. Lack of handling the possibility of
multiple return values. Questionably handling of strings. Complete lack
of checking function inputs for invalid values (e.g. nullptr).

I'm not going to fix those, my organization doesn't USE the DB drivers,
so i have little interest in re-working the logic beyond addressing
clang-tidy warnings for my own sanity, but i did add TODO comments for
someone else to look at in the future.



Additional note: While the changes look very invasive.... they aren't.

I don't think there is a way to get github to ignore whitespace in the
filediff, but if someone were to compare the commit locally, they'll see
that almost all of the changes are just adjusting indentation.
This commit is contained in:
Michael Jones 2024-05-29 22:44:23 -05:00 committed by GitHub
parent 99777bd585
commit 66a85ef09e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 905 additions and 896 deletions

View File

@ -225,11 +225,8 @@ static int mongo_get_user_key(uint8_t *usname, uint8_t *realm, hmackey_t key) {
char kval[sizeof(hmackey_t) + sizeof(hmackey_t) + 1];
memcpy(kval, value, sz);
kval[sz] = 0;
if (convert_string_key_to_binary(kval, key, sz / 2) < 0) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s, user %s\n", kval, usname);
} else {
ret = 0;
}
convert_string_key_to_binary(kval, key, sz / 2);
ret = 0;
}
}
}

View File

@ -403,11 +403,8 @@ static int mysql_get_user_key(uint8_t *usname, uint8_t *realm, hmackey_t key) {
char kval[sizeof(hmackey_t) + sizeof(hmackey_t) + 1];
memcpy(kval, row[0], sz);
kval[sz] = 0;
if (convert_string_key_to_binary(kval, key, sz / 2) < 0) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s, user %s\n", kval, usname);
} else {
ret = 0;
}
convert_string_key_to_binary(kval, key, sz / 2);
ret = 0;
}
}
}

View File

@ -145,9 +145,8 @@ static int pgsql_get_user_key(uint8_t *usname, uint8_t *realm, hmackey_t key) {
size_t sz = get_hmackey_size(SHATYPE_DEFAULT);
if (((size_t)len < sz * 2) || (strlen(kval) < sz * 2)) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key format: %s, user %s\n", kval, usname);
} else if (convert_string_key_to_binary(kval, key, sz) < 0) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s, user %s\n", kval, usname);
} else {
convert_string_key_to_binary(kval, key, sz);
ret = 0;
}
} else {

View File

@ -472,9 +472,8 @@ static int redis_get_user_key(uint8_t *usname, uint8_t *realm, hmackey_t key) {
size_t sz = get_hmackey_size(SHATYPE_DEFAULT);
if (strlen(rget->str) < sz * 2) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key format: %s, user %s\n", rget->str, usname);
} else if (convert_string_key_to_binary(rget->str, key, sz) < 0) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s, user %s\n", rget->str, usname);
} else {
convert_string_key_to_binary(rget->str, key, sz);
ret = 0;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -45,7 +45,7 @@ static void make_connection_key(void) { (void)pthread_key_create(&connection_key
pthread_key_t connection_key;
pthread_once_t connection_key_once = PTHREAD_ONCE_INIT;
int convert_string_key_to_binary(char *keysource, hmackey_t key, size_t sz) {
void convert_string_key_to_binary(char const *keysource, hmackey_t key, size_t sz) {
char is[3];
size_t i;
unsigned int v;
@ -56,7 +56,6 @@ int convert_string_key_to_binary(char *keysource, hmackey_t key, size_t sz) {
sscanf(is, "%02x", &v);
key[i] = (unsigned char)v;
}
return 0;
}
persistent_users_db_t *get_persistent_users_db(void) { return &(turn_params.default_users_db.persistent_users_db); }

View File

@ -79,7 +79,7 @@ typedef struct _turn_dbdriver_t {
/////////// USER DB CHECK //////////////////
int convert_string_key_to_binary(char *keysource, hmackey_t key, size_t sz);
void convert_string_key_to_binary(char const *keysource, hmackey_t key, size_t sz);
persistent_users_db_t *get_persistent_users_db(void);
const turn_dbdriver_t *get_dbdriver(void);
char *sanitize_userdb_string(char *udb);

View File

@ -759,12 +759,7 @@ int add_static_user_account(char *user) {
if (strlen(keysource) < sz * 2) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key format: %s\n", s);
}
if (convert_string_key_to_binary(keysource, *key, sz) < 0) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Wrong key: %s\n", s);
free(usname);
free(key);
return -1;
}
convert_string_key_to_binary(keysource, *key, sz);
} else {
// this is only for default realm
stun_produce_integrity_key_str((uint8_t *)usname, (uint8_t *)get_realm(NULL)->options.name, (uint8_t *)s, *key,

View File

@ -184,6 +184,7 @@ typedef uint32_t turn_time_t;
#error WRONG BYTE_ORDER SETTING
#endif
// NOLINTBEGIN(clang-diagnostic-string-compare)
#define STRCPY(dst, src) \
do { \
if ((const char *)(dst) != (const char *)(src)) { \
@ -196,6 +197,7 @@ typedef uint32_t turn_time_t;
} \
} \
} while (0)
// NOLINTEND(clang-diagnostic-string-compare)
//////////////// Bufferevents /////////////////////