Skip to content

Commit c12adcd

Browse files
committed
Add check for strlen
1 parent 49abf23 commit c12adcd

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

libCacheSim-python/src/cache_init.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,18 @@ static inline cache_t *create_sim_cache(const char *eviction_algo,
6969
} else {
7070
const char *window_size = strstr(eviction_params, "window-size=");
7171
if (window_size == NULL) {
72-
char *new_params = (char *)malloc(strlen(eviction_params) + 20);
73-
if (strlen(eviction_params) > 0) {
74-
sprintf(new_params, "%s,window-size=0.01", eviction_params);
72+
char dest[30];
73+
strncpy(dest, eviction_params, 30);
74+
dest[29] = '\0';
75+
size_t param_len = strlen(dest);
76+
char *new_params = (char *)malloc(param_len + 20);
77+
if (param_len > 0) {
78+
snprintf(new_params, param_len + 20, "%s,window-size=0.01", eviction_params);
7579
} else { // if eviction_params is "", no comma is needed
76-
sprintf(new_params, "window-size=0.01");
80+
snprintf(new_params, 20, "window-size=0.01");
7781
}
7882
cache = WTinyLFU_init(cc_params, new_params);
83+
free(new_params);
7984
} else {
8085
cache = WTinyLFU_init(cc_params, eviction_params);
8186
}

0 commit comments

Comments
 (0)