Skip to content

Commit

Permalink
Address clang-tidy warnings in db files (#1405)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonesmz committed May 30, 2024
1 parent 99777bd commit 66a85ef
Show file tree
Hide file tree
Showing 9 changed files with 797 additions and 788 deletions.
7 changes: 2 additions & 5 deletions src/apps/relay/dbdrivers/dbd_mongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/apps/relay/dbdrivers/dbd_mysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/apps/relay/dbdrivers/dbd_pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/apps/relay/dbdrivers/dbd_redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Loading

0 comments on commit 66a85ef

Please sign in to comment.