Skip to content

Commit 127e170

Browse files
committed
code: address SonarQube warnings in src/cache/*.c
Signed-off-by: Hans Zandbelt <[email protected]>
1 parent ef58a94 commit 127e170

File tree

7 files changed

+61
-49
lines changed

7 files changed

+61
-49
lines changed

ChangeLog

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
12/17/2024
2+
- code: address SonarQube warnings in src/cache/*.c
3+
14
12/16/2024
25
- http: report errors when curl_easy_setopt fails and improve macro usage
3-
- code: declare enum members as int so they can be set to OIDC_CONFIG_POS_INT_UNSET without warning
4-
- code: declare memcache members as int so they can be set to OIDC_CONFIG_POS_INT_UNSET without warning
5-
- code: declare introspection_endpoint_method member as int so it can be set to OIDC_CONFIG_POS_INT_UNSET without warning
6-
- code: check return value of oidc_get_provider_from_session and oidc_refresh_token_grant in logout.c
7-
- code: avoid potential crash on non-conformant literal IPv6 adresses in oidc_util_current_url_host
8-
- code: apply boundary checks on oidc_metrics_shm_size and use a global static for performance reasons
6+
- address warnings from static code analysis tool Coverity
7+
- code: declare enum members as int so they can be set to OIDC_CONFIG_POS_INT_UNSET without warning
8+
- code: declare memcache members as int so they can be set to OIDC_CONFIG_POS_INT_UNSET without warning
9+
- code: declare introspection_endpoint_method member as int so it can be set to OIDC_CONFIG_POS_INT_UNSET without warning
10+
- code: check return value of oidc_get_provider_from_session and oidc_refresh_token_grant in logout.c
11+
- code: avoid potential crash on non-conformant literal IPv6 adresses in oidc_util_current_url_host
12+
- code: apply boundary checks on oidc_metrics_shm_size and use a global static for performance reasons
913

1014
12/15/2024
1115
- add Coverity Github action
@@ -22,16 +26,18 @@
2226
- code: correct check for *static_template_content in oidc_util_html_send_in_template in util.c
2327

2428
12/11/2024
25-
- code: loop over authz arrays with index instead of pointer
26-
- code: avoid embedding defines in macro arguments
27-
- code: avoid cast warnings
28-
- code: add comment to empty functions
29-
- code: remove any side effects from right hand operands of logical && operator
29+
- address warnings from static code analysis tool SonarQube
30+
- code: loop over authz arrays with index instead of pointer
31+
- code: avoid embedding defines in macro arguments
32+
- code: avoid cast warnings
33+
- code: add comment to empty functions
34+
- code: remove any side effects from right hand operands of logical && operator
3035

3136
12/10/2024
3237
- github: add SonarQube analysis to Github workflows
33-
- code: use snprintf instead of sprintf
34-
- code: move _snprintf define to const.h
38+
- address warnings from static code analysis tool SonarQube
39+
- code: use snprintf instead of sprintf
40+
- code: move _snprintf define to const.h
3541
- bump to 2.4.16.7dev
3642

3743
12/09/2024

src/cache/common.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ apr_byte_t oidc_cache_mutex_destroy(server_rec *s, oidc_cache_mutex_t *m) {
218218

219219
oidc_slog(s, APLOG_TRACE1, "init: %pp (m=%pp,s=%pp, p=%d)", m, m->gmutex ? m->gmutex : 0, s, m->is_parent);
220220

221-
if ((m) && (m->is_parent == TRUE)) {
221+
if (m && (m->is_parent == TRUE)) {
222222
if ((m->is_global) && (m->gmutex)) {
223223
rv = apr_global_mutex_destroy(m->gmutex);
224224
m->gmutex = NULL;
@@ -293,7 +293,8 @@ apr_byte_t oidc_cache_get(request_rec *r, const char *section, const char *key,
293293
apr_byte_t rc = FALSE;
294294
char *msg = NULL;
295295
const char *s_key = NULL;
296-
char *cache_value = NULL, *s_secret = NULL;
296+
char *cache_value = NULL;
297+
char *s_secret = NULL;
297298

298299
oidc_debug(r, "enter: %s (section=%s, decrypt=%d, type=%s)", key, section, encrypted, cfg->cache.impl->name);
299300

src/cache/file.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ static apr_status_t oidc_cache_file_clean(request_rec *r) {
307307
/* skip non-cache entries, cq. the ".", ".." and the metadata file */
308308
if ((fi.name[0] == OIDC_CHAR_DOT) ||
309309
(_oidc_strstr(fi.name, OIDC_CACHE_FILE_PREFIX) != fi.name) ||
310-
((_oidc_strcmp(fi.name,
311-
oidc_cache_file_name(r, "cache-file", OIDC_CACHE_FILE_LAST_CLEANED)) == 0)))
310+
(_oidc_strcmp(fi.name,
311+
oidc_cache_file_name(r, "cache-file", OIDC_CACHE_FILE_LAST_CLEANED)) == 0))
312312
continue;
313313

314314
/* get the fully qualified path to the cache file and open it */

src/cache/memcache.c

+13-8
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,19 @@ static int oidc_cache_memcache_post_config(server_rec *s) {
7878
cfg->cache.cfg = context;
7979

8080
apr_status_t rv = APR_SUCCESS;
81-
int nservers = 0;
81+
apr_uint16_t nservers = 0;
8282
char *split;
8383
char *tok;
8484
apr_pool_t *p = s->process->pool;
8585
APR_OPTIONAL_FN_TYPE(http2_get_num_workers) * get_h2_num_workers;
86-
int max_threads, minw, maxw;
87-
apr_uint32_t min, smax, hmax, ttl;
86+
int max_threads = 0;
87+
int minw = 0;
88+
int maxw = 0;
89+
apr_uint32_t min = 0;
90+
apr_uint32_t smax = 0;
91+
apr_uint32_t hmax = 0;
92+
apr_uint32_t ttl = 0;
93+
;
8894

8995
if (oidc_cfg_cache_memcache_servers_get(cfg) == NULL) {
9096
oidc_serror(s, "cache type is set to \"memcache\", but no valid " OIDCMemCacheServers
@@ -222,9 +228,8 @@ static char *oidc_cache_memcache_get_key(apr_pool_t *pool, const char *section,
222228
/*
223229
* check dead/alive status for all servers
224230
*/
225-
static apr_byte_t oidc_cache_memcache_status(request_rec *r, oidc_cache_cfg_memcache_t *context) {
226-
int i = 0;
227-
for (i = 0; i < context->cache_memcache->ntotal; i++) {
231+
static apr_byte_t oidc_cache_memcache_status(const oidc_cache_cfg_memcache_t *context) {
232+
for (int i = 0; i < context->cache_memcache->ntotal; i++) {
228233
if (context->cache_memcache->live_servers[i]->status != APR_MC_SERVER_DEAD)
229234
return TRUE;
230235
}
@@ -250,7 +255,7 @@ static apr_byte_t oidc_cache_memcache_get(request_rec *r, const char *section, c
250255
/*
251256
* NB: workaround the fact that the apr_memcache returns APR_NOTFOUND if a server has been marked dead
252257
*/
253-
if (oidc_cache_memcache_status(r, context) == FALSE) {
258+
if (oidc_cache_memcache_status(context) == FALSE) {
254259

255260
oidc_cache_memcache_log_status_error(r, "apr_memcache_getp", rv);
256261

@@ -309,7 +314,7 @@ static apr_byte_t oidc_cache_memcache_set(request_rec *r, const char *section, c
309314
} else {
310315

311316
/* calculate the timeout as a Unix timestamp which allows values > 30 days */
312-
apr_uint32_t timeout = apr_time_sec(expiry);
317+
apr_uint32_t timeout = (apr_uint32_t)apr_time_sec(expiry);
313318

314319
/* store it */
315320
rv = apr_memcache_set(context->cache_memcache, oidc_cache_memcache_get_key(r->pool, section, key),

src/cache/redis.c

+9-11
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,9 @@ static apr_status_t oidc_cache_redis_connect(request_rec *r, oidc_cache_cfg_redi
112112
* free resources allocated for the per-process Redis connection context
113113
*/
114114
apr_status_t oidc_cache_redis_disconnect(oidc_cache_cfg_redis_t *context) {
115-
if (context != NULL) {
116-
if (context->rctx != NULL) {
117-
redisFree(context->rctx);
118-
context->rctx = NULL;
119-
}
115+
if ((context != NULL) && (context->rctx != NULL)) {
116+
redisFree(context->rctx);
117+
context->rctx = NULL;
120118
}
121119
return APR_SUCCESS;
122120
}
@@ -367,10 +365,11 @@ static int oidc_cache_redis_env2int(request_rec *r, const char *env_var_name, co
367365
#define OIDC_REDIS_RETRY_INTERVAL_DEFAULT 300
368366

369367
#define OIDC_REDIS_WARN_OR_ERROR(cond, r, ...) \
370-
if (cond) \
368+
if (cond) { \
371369
oidc_warn(r, ##__VA_ARGS__); \
372-
else \
373-
oidc_error(r, ##__VA_ARGS__);
370+
} else { \
371+
oidc_error(r, ##__VA_ARGS__); \
372+
}
374373

375374
/*
376375
* execute Redis command and deal with return value
@@ -379,14 +378,13 @@ static redisReply *oidc_cache_redis_exec(request_rec *r, oidc_cache_cfg_redis_t
379378

380379
redisReply *reply = NULL;
381380
char *errstr = NULL;
382-
int i = 0;
383381
va_list ap;
384382
int retries = oidc_cache_redis_env2int(r, OIDC_REDIS_MAX_TRIES_ENV_VAR, OIDC_REDIS_MAX_TRIES_DEFAULT);
385383
apr_time_t interval = apr_time_from_msec(
386384
oidc_cache_redis_env2int(r, OIDC_REDIS_RETRY_INTERVAL_ENV_VAR, OIDC_REDIS_RETRY_INTERVAL_DEFAULT));
387385

388386
/* try to execute a command at max n times while reconnecting */
389-
for (i = 1; i <= retries; i++) {
387+
for (int i = 1; i <= retries; i++) {
390388

391389
/* connect */
392390
if (context->connect(r, context) != APR_SUCCESS) {
@@ -507,7 +505,7 @@ apr_byte_t oidc_cache_redis_set(request_rec *r, const char *section, const char
507505
} else {
508506

509507
/* calculate the timeout from now */
510-
timeout = apr_time_sec(expiry - apr_time_now());
508+
timeout = (apr_uint32_t)apr_time_sec(expiry - apr_time_now());
511509

512510
/* store it */
513511
reply = oidc_cache_redis_exec(r, context, "SETEX %s %d %s",

src/cache/shm.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,8 @@ int oidc_cache_shm_post_config(server_rec *s) {
103103
}
104104

105105
/* initialize the whole segment to '/0' */
106-
int i;
107106
oidc_cache_shm_entry_t *t = apr_shm_baseaddr_get(context->shm);
108-
for (i = 0; i < cfg->cache.shm_size_max; i++, OIDC_CACHE_SHM_ADD_OFFSET(t, cfg->cache.shm_entry_size_max)) {
107+
for (int i = 0; i < cfg->cache.shm_size_max; i++, OIDC_CACHE_SHM_ADD_OFFSET(t, cfg->cache.shm_entry_size_max)) {
109108
t->section_key[0] = '\0';
110109
t->access = 0;
111110
}
@@ -220,11 +219,12 @@ static apr_byte_t oidc_cache_shm_set(request_rec *r, const char *section, const
220219
oidc_cfg_t *cfg = ap_get_module_config(r->server->module_config, &auth_openidc_module);
221220
oidc_cache_cfg_shm_t *context = (oidc_cache_cfg_shm_t *)cfg->cache.cfg;
222221

223-
oidc_cache_shm_entry_t *match, *free, *lru;
224-
oidc_cache_shm_entry_t *t;
225-
apr_time_t current_time;
226-
int i;
227-
apr_time_t age;
222+
oidc_cache_shm_entry_t *match = NULL;
223+
oidc_cache_shm_entry_t *free = NULL;
224+
oidc_cache_shm_entry_t *lru = NULL;
225+
oidc_cache_shm_entry_t *t = NULL;
226+
apr_time_t current_time = 0;
227+
apr_time_t age = 0;
228228

229229
const char *section_key = oidc_cache_shm_get_key(r, section, key);
230230
if (section_key == NULL)
@@ -255,7 +255,7 @@ static apr_byte_t oidc_cache_shm_set(request_rec *r, const char *section, const
255255
match = NULL;
256256
free = NULL;
257257
lru = t;
258-
for (i = 0; i < cfg->cache.shm_size_max; i++, OIDC_CACHE_SHM_ADD_OFFSET(t, cfg->cache.shm_entry_size_max)) {
258+
for (int i = 0; i < cfg->cache.shm_size_max; i++, OIDC_CACHE_SHM_ADD_OFFSET(t, cfg->cache.shm_entry_size_max)) {
259259

260260
/* see if this slot is free */
261261
if (t->section_key[0] == '\0') {
@@ -327,15 +327,15 @@ static int oidc_cache_shm_destroy(server_rec *s) {
327327
oidc_slog(s, APLOG_TRACE1, "destroy: %pp (shm=%pp,s=%pp, p=%d)", context, context ? context->shm : 0, s,
328328
context ? context->is_parent : -1);
329329

330-
if ((context) && (context->is_parent == TRUE) && (context->shm) && (context->mutex)) {
330+
if (context && (context->is_parent == TRUE) && (context->shm) && (context->mutex)) {
331331
oidc_cache_mutex_lock(s->process->pool, s, context->mutex);
332332
rv = apr_shm_destroy(context->shm);
333333
oidc_sdebug(s, "apr_shm_destroy returned: %d", rv);
334334
context->shm = NULL;
335335
oidc_cache_mutex_unlock(s->process->pool, s, context->mutex);
336336
}
337337

338-
if ((context) && (context->mutex)) {
338+
if (context && (context->mutex)) {
339339
if (oidc_cache_mutex_destroy(s, context->mutex) != TRUE)
340340
rv = APR_EGENERAL;
341341
context->mutex = NULL;

src/metrics.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,12 @@ extern const oidc_metrics_counter_info_t _oidc_metrics_counters_info[];
197197
void oidc_metrics_counter_inc(request_rec *r, oidc_metrics_counter_type_t type, const char *spec);
198198

199199
#define OIDC_METRICS_COUNTER_INC_SPEC(r, cfg, type, spec) \
200-
if (oidc_cfg_metrics_hook_data_get(cfg) != NULL) \
200+
if (oidc_cfg_metrics_hook_data_get(cfg) != NULL) { \
201201
if (apr_hash_get(oidc_cfg_metrics_hook_data_get(cfg), _oidc_metrics_counters_info[type].class_name, \
202-
APR_HASH_KEY_STRING) != NULL) \
203-
oidc_metrics_counter_inc(r, type, spec);
202+
APR_HASH_KEY_STRING) != NULL) { \
203+
oidc_metrics_counter_inc(r, type, spec); \
204+
} \
205+
}
204206

205207
#define OIDC_METRICS_COUNTER_INC(r, cfg, type) OIDC_METRICS_COUNTER_INC_SPEC(r, cfg, type, NULL);
206208

0 commit comments

Comments
 (0)