diff options
| author | frosty <frostinator33@airmail.cc> | 2026-10-27 20:31:28 -0400 |
|---|---|---|
| committer | frosty <frostinator33@airmail.cc> | 2026-10-27 20:33:50 -0400 |
| commit | 250898c638f07ace6bc7167faede93113c8fd0db (patch) | |
| tree | 310addbedcaaf76da98df22434a4f4711c8ba963 /src | |
| parent | bd3da2232540ea4c78e7ac35e2c90c38c8a3c410 (diff) | |
refactor: replace potentially unsafe string functions with snprintf
Diffstat (limited to 'src')
| -rw-r--r-- | src/Cache/Cache.c | 8 | ||||
| -rw-r--r-- | src/Infobox/Dictionary.c | 3 | ||||
| -rw-r--r-- | src/Infobox/UnitConversion.c | 22 | ||||
| -rw-r--r-- | src/Infobox/Wikipedia.c | 16 | ||||
| -rw-r--r-- | src/Proxy/Proxy.c | 6 | ||||
| -rw-r--r-- | src/Routes/ImageProxy.c | 23 | ||||
| -rw-r--r-- | src/Routes/Images.c | 6 | ||||
| -rw-r--r-- | src/Routes/Search.c | 21 | ||||
| -rw-r--r-- | src/Routes/SettingsSave.c | 32 | ||||
| -rw-r--r-- | src/Utility/Utility.c | 4 |
10 files changed, 78 insertions, 63 deletions
diff --git a/src/Cache/Cache.c b/src/Cache/Cache.c index 22f2553..bf71974 100644 --- a/src/Cache/Cache.c +++ b/src/Cache/Cache.c @@ -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; diff --git a/src/Infobox/Dictionary.c b/src/Infobox/Dictionary.c index 58d0dfa..4ccabdc 100644 --- a/src/Infobox/Dictionary.c +++ b/src/Infobox/Dictionary.c @@ -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); diff --git a/src/Infobox/UnitConversion.c b/src/Infobox/UnitConversion.c index 4269f3c..cf068fd 100644 --- a/src/Infobox/UnitConversion.c +++ b/src/Infobox/UnitConversion.c @@ -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; } diff --git a/src/Infobox/Wikipedia.c b/src/Infobox/Wikipedia.c index 6108bce..7f76e8b 100644 --- a/src/Infobox/Wikipedia.c +++ b/src/Infobox/Wikipedia.c @@ -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); diff --git a/src/Proxy/Proxy.c b/src/Proxy/Proxy.c index 5205bac..15e5b1c 100644 --- a/src/Proxy/Proxy.c +++ b/src/Proxy/Proxy.c @@ -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); diff --git a/src/Routes/ImageProxy.c b/src/Routes/ImageProxy.c index 2d6d3a9..9ce79d9 100644 --- a/src/Routes/ImageProxy.c +++ b/src/Routes/ImageProxy.c @@ -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"); diff --git a/src/Routes/Images.c b/src/Routes/Images.c index 1ab875e..3840587 100644 --- a/src/Routes/Images.c +++ b/src/Routes/Images.c @@ -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; } diff --git a/src/Routes/Search.c b/src/Routes/Search.c index 0aa146a..8526cfa 100644 --- a/src/Routes/Search.c +++ b/src/Routes/Search.c @@ -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; diff --git a/src/Routes/SettingsSave.c b/src/Routes/SettingsSave.c index 3cbb1d6..498f627 100644 --- a/src/Routes/SettingsSave.c +++ b/src/Routes/SettingsSave.c @@ -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); diff --git a/src/Utility/Utility.c b/src/Utility/Utility.c index 35fc491..ed1ff13 100644 --- a/src/Utility/Utility.c +++ b/src/Utility/Utility.c @@ -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); } } |
