Skip to content

Commit 4d6cc08

Browse files
Fix the types of config values for match_throttle_metadata_keys. (#180)
We're dealing with values in multiple gigabytes, which would overflow an int.
1 parent 6fd9250 commit 4d6cc08

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/match_throttle_metadata_keys.c

+38-17
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@ typedef struct {
7777

7878
// When 'server_memory_in_use' is less than this value, throttling is turned
7979
// off.
80-
int low_water_mark_bytes;
80+
size_t low_water_mark_bytes;
8181

8282
// When 'server_memory_in_use' is greater than this value, throttling is
8383
// turned on.
84-
int high_water_mark_bytes;
84+
size_t high_water_mark_bytes;
8585

8686
// How long to keep adding hashes to the same chunk before making a new chunk
8787
// (typically 1/2 hour).
88-
int chunk_interval_secs;
88+
cdtime_t chunk_interval_secs;
8989

9090
// How long to keep wg_key_history_s chunks before purging them (typically
9191
// 24 hours).
92-
int purge_interval_secs;
92+
cdtime_t purge_interval_secs;
9393
} mtg_key_tracker_t;
9494

9595
typedef struct {
@@ -242,30 +242,52 @@ static int mtg_create(const oconfig_item_t *ci, void **user_data)
242242
} else {
243243
int k;
244244
mtg_key_tracker_t *kt = ctx->key_tracker; // Alias
245-
static const char *int_keys[4] = {
245+
static const char *size_t_keys[] = {
246246
"LowWaterMark",
247247
"HighWaterMark",
248-
"ChunkInterval",
249-
"PurgeInterval",
250248
};
251-
int *int_locations[] = {
249+
size_t *size_t_locations[] = {
252250
&kt->low_water_mark_bytes,
253251
&kt->high_water_mark_bytes,
252+
};
253+
static const char *cdtime_keys[] = {
254+
"ChunkInterval",
255+
"PurgeInterval",
256+
};
257+
cdtime_t *cdtime_locations[] = {
254258
&kt->chunk_interval_secs,
255259
&kt->purge_interval_secs,
256260
};
257-
assert(STATIC_ARRAY_SIZE(int_keys) == STATIC_ARRAY_SIZE(int_locations));
258-
for (k = 0; k < STATIC_ARRAY_SIZE(int_keys); ++k) {
259-
if (strcasecmp(int_keys[k], child->key) == 0) {
260-
if (cf_util_get_int(child, int_locations[k]) != 0) {
261-
ERROR("%s: cf_util_get_int failed for key %s",
261+
assert(STATIC_ARRAY_SIZE(size_t_keys)
262+
== STATIC_ARRAY_SIZE(size_t_locations));
263+
for (k = 0; k < STATIC_ARRAY_SIZE(size_t_keys); ++k) {
264+
if (strcasecmp(size_t_keys[k], child->key) == 0) {
265+
double result;
266+
if (cf_util_get_double(child, &result) != 0) {
267+
ERROR("%s: cf_util_get_double failed for key %s",
268+
this_plugin_name, child->key);
269+
++parse_errors;
270+
}
271+
*size_t_locations[k] = (size_t)result;
272+
break;
273+
}
274+
}
275+
if (k < STATIC_ARRAY_SIZE(size_t_keys)) {
276+
continue;
277+
}
278+
assert(STATIC_ARRAY_SIZE(cdtime_keys)
279+
== STATIC_ARRAY_SIZE(cdtime_locations));
280+
for (k = 0; k < STATIC_ARRAY_SIZE(cdtime_keys); ++k) {
281+
if (strcasecmp(cdtime_keys[k], child->key) == 0) {
282+
if (cf_util_get_cdtime(child, cdtime_locations[k]) != 0) {
283+
ERROR("%s: cf_util_get_cdtime failed for key %s",
262284
this_plugin_name, child->key);
263285
++parse_errors;
264286
}
265287
break;
266288
}
267289
}
268-
if (k < STATIC_ARRAY_SIZE(int_keys)) {
290+
if (k < STATIC_ARRAY_SIZE(cdtime_keys)) {
269291
continue;
270292
}
271293
ERROR("%s: Unknown configuration option %s",
@@ -483,7 +505,7 @@ static int mtg_update_stats(size_t server_memory_in_use, _Bool is_throttling)
483505
static int mtg_retire_old_entries(mtg_key_tracker_t *tracker, cdtime_t now)
484506
{
485507
// Trim the key history (removing entries older than 'purge_time')
486-
cdtime_t purge_time = now - TIME_T_TO_CDTIME_T(tracker->purge_interval_secs);
508+
cdtime_t purge_time = now - tracker->purge_interval_secs;
487509

488510
while (tracker->key_history_head != NULL &&
489511
tracker->key_history_head->last_append_time < purge_time) {
@@ -567,8 +589,7 @@ static int mtg_add_new_entries(const mtg_context_t *ctx, cdtime_t now,
567589
// 1. There is no current history node.
568590
// 2. The current history node is full.
569591
// 3. The current history node was created prior to 'chunk_time'.
570-
cdtime_t chunk_time = now -
571-
TIME_T_TO_CDTIME_T(tracker->chunk_interval_secs);
592+
cdtime_t chunk_time = now - tracker->chunk_interval_secs;
572593
mtg_key_history_t *tail = tracker->key_history_tail; // alias
573594
if (tail == NULL ||
574595
tail->num_hashes == STATIC_ARRAY_SIZE(tail->hashes) ||

0 commit comments

Comments
 (0)