refactor: replace potentially unsafe string functions with snprintf

This commit is contained in:
frosty
2026-10-27 20:31:28 -04:00
parent bd3da22325
commit 250898c638
10 changed files with 78 additions and 63 deletions

View File

@@ -36,7 +36,7 @@ static void md5_hash(const char *str, char *output) {
EVP_DigestFinal_ex(ctx, hash, &hash_len);
EVP_MD_CTX_free(ctx);
for (unsigned int i = 0; i < hash_len; i++) {
sprintf(output + (i * 2), "%02x", hash[i]);
snprintf(output + (i * 2), 3, "%02x", hash[i]);
}
}
@@ -50,11 +50,9 @@ static time_t get_file_mtime(const char *filepath) {
int cache_init(const char *dir) {
if (!dir || strlen(dir) == 0) {
strncpy(cache_dir, DEFAULT_CACHE_DIR, sizeof(cache_dir) - 1);
cache_dir[sizeof(cache_dir) - 1] = '\0';
snprintf(cache_dir, sizeof(cache_dir), "%s", DEFAULT_CACHE_DIR);
} else {
strncpy(cache_dir, dir, sizeof(cache_dir) - 1);
cache_dir[sizeof(cache_dir) - 1] = '\0';
snprintf(cache_dir, sizeof(cache_dir), "%s", dir);
}
struct stat st;

View File

@@ -207,8 +207,7 @@ char *construct_dictionary_url(const char *query) {
const char *base = "https://dictionary.cambridge.org/dictionary/english/";
char *url = malloc(strlen(base) + strlen(escaped) + 1);
if (url) {
strcpy(url, base);
strcat(url, escaped);
snprintf(url, strlen(base) + strlen(escaped) + 1, "%s%s", base, escaped);
}
curl_free(escaped);

View File

@@ -360,7 +360,7 @@ static int parse_conversion_query(const char *query, double *value,
while (try_len > 0) {
*to_unit = find_unit(try_buf);
if (*to_unit) {
strcpy(to_part, try_buf);
snprintf(to_part, sizeof(to_part), "%s", try_buf);
break;
}
char *last_space = strrchr(try_buf, ' ');
@@ -443,34 +443,34 @@ static const char *pluralize(const char *unit, double value, char *buf,
if (len == 0 || bufsize == 0)
return unit;
strncpy(buf, unit, bufsize - 1);
buf[bufsize - 1] = '\0';
snprintf(buf, bufsize, "%s", unit);
if (strcmp(unit, "foot") == 0 || strcmp(unit, "square foot") == 0) {
if (is_one)
strcpy(buf, unit);
snprintf(buf, bufsize, "%s", unit);
else
strcpy(buf, strcmp(unit, "square foot") == 0 ? "square feet" : "feet");
snprintf(buf, bufsize, "%s",
strcmp(unit, "square foot") == 0 ? "square feet" : "feet");
return buf;
}
if (strcmp(unit, "inch") == 0 || strcmp(unit, "square inch") == 0) {
if (is_one)
strcpy(buf, unit);
snprintf(buf, bufsize, "%s", unit);
else
strcpy(buf,
strcmp(unit, "square inch") == 0 ? "square inches" : "inches");
snprintf(buf, bufsize, "%s",
strcmp(unit, "square inch") == 0 ? "square inches" : "inches");
return buf;
}
if (strcmp(unit, "stone") == 0) {
if (is_one)
strcpy(buf, "stone");
snprintf(buf, bufsize, "%s", "stone");
else
strcpy(buf, "stones");
snprintf(buf, bufsize, "%s", "stones");
return buf;
}
if (strcmp(unit, "celsius") == 0 || strcmp(unit, "fahrenheit") == 0 ||
strcmp(unit, "kelvin") == 0) {
strcpy(buf, unit);
snprintf(buf, bufsize, "%s", unit);
return buf;
}

View File

@@ -33,7 +33,7 @@ static void shorten_summary(char **extract_ptr, int max_chars) {
if (new_text) {
strncpy(new_text, text, end_pos);
new_text[end_pos] = '\0';
strcat(new_text, "...");
strncat(new_text, "...", 3);
free(*extract_ptr);
*extract_ptr = new_text;
}
@@ -65,8 +65,8 @@ static void extract_wiki_info(xmlNode *node, InfoBox *info) {
info->url =
malloc(strlen(base_article_url) + strlen(formatted_title) + 1);
if (info->url) {
strcpy(info->url, base_article_url);
strcat(info->url, formatted_title);
snprintf(info->url, strlen(base_article_url) + strlen(formatted_title) + 1,
"%s%s", base_article_url, formatted_title);
}
free(formatted_title);
xmlFree(title);
@@ -177,9 +177,9 @@ static char *get_first_search_result(const char *search_term) {
return NULL;
}
strcpy(search_url, search_base);
strcat(search_url, escaped_term);
strcat(search_url, search_suffix);
snprintf(search_url, strlen(search_base) + strlen(escaped_term) +
strlen(search_suffix) + 1, "%s%s%s", search_base,
escaped_term, search_suffix);
curl_free(escaped_term);
@@ -226,8 +226,8 @@ char *construct_wiki_url(const char *search_term) {
char *full_url = malloc(strlen(base) + strlen(escaped_title) + 1);
if (full_url) {
strcpy(full_url, base);
strcat(full_url, escaped_title);
snprintf(full_url, strlen(base) + strlen(escaped_title) + 1, "%s%s", base,
escaped_title);
}
curl_free(escaped_title);

View File

@@ -25,8 +25,7 @@ static void generate_random_string(char *buf, size_t len) {
void set_proxy_config(const char *proxy_str, int rand_user, int rand_pass) {
if (proxy_str && proxy_str[0]) {
strncpy(proxy_url, proxy_str, sizeof(proxy_url) - 1);
proxy_url[sizeof(proxy_url) - 1] = '\0';
snprintf(proxy_url, sizeof(proxy_url), "%s", proxy_str);
}
randomize_username = rand_user;
randomize_password = rand_pass;
@@ -144,7 +143,8 @@ int load_proxy_list(const char *filename) {
if (temp_count >= temp_capacity) {
temp_capacity *= 2;
Proxy *new_list = (Proxy *)realloc(temp_list, temp_capacity * sizeof(Proxy));
Proxy *new_list =
(Proxy *)realloc(temp_list, temp_capacity * sizeof(Proxy));
if (!new_list) {
free(temp_list);
fclose(file);

View File

@@ -19,7 +19,7 @@
typedef struct DnsCacheEntry {
char hostname[256];
char ip_str[INET_ADDRSTRLEN];
char ip_str[INET6_ADDRSTRLEN];
time_t resolved_at;
struct DnsCacheEntry *next;
} DnsCacheEntry;
@@ -39,7 +39,7 @@ static int dns_cache_lookup(const char *hostname, char *out_ip) {
for (DnsCacheEntry *e = dns_cache; e; e = e->next) {
if (strcmp(e->hostname, hostname) == 0) {
if ((now - e->resolved_at) < DNS_CACHE_TTL) {
strcpy(out_ip, e->ip_str);
snprintf(out_ip, INET6_ADDRSTRLEN, "%s", e->ip_str);
pthread_mutex_unlock(&dns_cache_mutex);
return 0;
}
@@ -63,7 +63,7 @@ static void dns_cache_insert(const char *hostname, const char *ip_str) {
continue;
}
if (strcmp(entry->hostname, hostname) == 0) {
strcpy(entry->ip_str, ip_str);
snprintf(entry->ip_str, sizeof(entry->ip_str), "%s", ip_str);
entry->resolved_at = now;
pthread_mutex_unlock(&dns_cache_mutex);
return;
@@ -73,9 +73,8 @@ static void dns_cache_insert(const char *hostname, const char *ip_str) {
DnsCacheEntry *new_entry = malloc(sizeof(DnsCacheEntry));
if (new_entry) {
strncpy(new_entry->hostname, hostname, sizeof(new_entry->hostname) - 1);
new_entry->hostname[sizeof(new_entry->hostname) - 1] = '\0';
strcpy(new_entry->ip_str, ip_str);
snprintf(new_entry->hostname, sizeof(new_entry->hostname), "%s", hostname);
snprintf(new_entry->ip_str, sizeof(new_entry->ip_str), "%s", ip_str);
new_entry->resolved_at = now;
new_entry->next = dns_cache;
dns_cache = new_entry;
@@ -150,7 +149,7 @@ static const char *is_private_hostname(const char *hostname, char *out_ip) {
}
freeaddrinfo(res);
strcpy(out_ip, ip_str);
snprintf(out_ip, INET6_ADDRSTRLEN, "%s", ip_str);
dns_cache_insert(hostname, ip_str);
return out_ip;
}
@@ -172,7 +171,8 @@ static int is_allowed_domain(const char *url, char *resolved_ip) {
curl_url_get(h, CURLUPART_SCHEME, &scheme, 0);
int valid_scheme = 0;
if (scheme && (strcasecmp(scheme, "http") == 0 || strcasecmp(scheme, "https") == 0)) {
if (scheme &&
(strcasecmp(scheme, "http") == 0 || strcasecmp(scheme, "https") == 0)) {
valid_scheme = 1;
}
@@ -258,7 +258,7 @@ static char *url_encode_key(const char *url) {
EVP_MD_CTX_free(ctx);
for (int i = 0; i < 16; i++) {
sprintf(hash + (i * 2), "%02x", md5hash[i]);
snprintf(hash + (i * 2), 3, "%02x", md5hash[i]);
}
hash[32] = '\0';
@@ -279,7 +279,7 @@ int image_proxy_handler(UrlParams *params) {
return 0;
}
char resolved_ip[INET_ADDRSTRLEN] = {0};
char resolved_ip[INET6_ADDRSTRLEN] = {0};
int domain_check = is_allowed_domain(url, resolved_ip);
if (domain_check == -1) {
send_response("Invalid URL scheme");
@@ -420,8 +420,7 @@ int image_proxy_handler(UrlParams *params) {
return 0;
}
if (strlen(content_type) == 0 ||
strncmp(content_type, "image/", 6) != 0) {
if (strlen(content_type) == 0 || strncmp(content_type, "image/", 6) != 0) {
free(buf.data);
free(cache_key);
send_response("Invalid content type");

View File

@@ -27,12 +27,12 @@ static char *build_images_href(const char *query, int page) {
if (!href)
return NULL;
snprintf(href, needed, "/images?q=%s", safe_query);
if (page > 1) {
char page_buf[16];
snprintf(page_buf, sizeof(page_buf), "%d", page);
strcat(href, "&p=");
strcat(href, page_buf);
snprintf(href, needed, "/images?q=%s&p=%s", safe_query, page_buf);
} else {
snprintf(href, needed, "/images?q=%s", safe_query);
}
return href;
}

View File

@@ -376,18 +376,19 @@ static char *build_search_href(const char *query, const char *engine_id,
if (!href)
return NULL;
snprintf(href, needed, "/search?q=%s", safe_query);
if (use_engine) {
strcat(href, "&engine=");
strcat(href, engine_id);
}
if (page > 1) {
if (use_engine && page > 1) {
char page_buf[16];
snprintf(page_buf, sizeof(page_buf), "%d", page);
strcat(href, "&p=");
strcat(href, page_buf);
snprintf(href, needed, "/search?q=%s&engine=%s&p=%s", safe_query, engine_id,
page_buf);
} else if (use_engine) {
snprintf(href, needed, "/search?q=%s&engine=%s", safe_query, engine_id);
} else if (page > 1) {
char page_buf[16];
snprintf(page_buf, sizeof(page_buf), "%d", page);
snprintf(href, needed, "/search?q=%s&p=%s", safe_query, page_buf);
} else {
snprintf(href, needed, "/search?q=%s", safe_query);
}
return href;

View File

@@ -36,8 +36,8 @@ int settings_save_handler(UrlParams *params) {
selected_count < ENGINE_COUNT) {
strncpy(selected_ids[selected_count], engine_id,
sizeof(selected_ids[selected_count]) - 1);
selected_ids[selected_count][sizeof(selected_ids[selected_count]) - 1] =
'\0';
selected_ids[selected_count]
[sizeof(selected_ids[selected_count]) - 1] = '\0';
selected_count++;
}
}
@@ -45,19 +45,37 @@ int settings_save_handler(UrlParams *params) {
}
if (strlen(theme) > 0) {
set_cookie("theme", theme, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false, false);
set_cookie("theme", theme, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false,
false);
}
if (strlen(locale) > 0) {
set_cookie("locale", locale, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false, false);
set_cookie("locale", locale, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false,
false);
}
if (engines_present) {
char cookie_value[512];
cookie_value[0] = '\0';
size_t used = 0;
cookie_value[0] = '\0';
for (int i = 0; i < selected_count; i++) {
if (i > 0)
strcat(cookie_value, ",");
strcat(cookie_value, selected_ids[i]);
const char *id = selected_ids[i];
size_t idlen = strlen(id);
if (i > 0) {
if (used + 1 < sizeof(cookie_value)) {
cookie_value[used++] = ',';
cookie_value[used] = '\0';
} else {
break;
}
}
if (used + idlen < sizeof(cookie_value)) {
memcpy(cookie_value + used, id, idlen);
used += idlen;
cookie_value[used] = '\0';
} else {
break;
}
}
set_cookie("engines", cookie_value, "Fri, 31 Dec 2038 23:59:59 GMT", "/",
false, false);

View File

@@ -89,8 +89,8 @@ void get_available_themes(char ***out_themes, int *out_count) {
void set_default_locale(const char *locale) {
if (locale && strlen(locale) > 0) {
strncpy(global_default_locale, locale, sizeof(global_default_locale) - 1);
global_default_locale[sizeof(global_default_locale) - 1] = '\0';
snprintf(global_default_locale, sizeof(global_default_locale), "%s",
locale);
}
}