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/Infobox | |
| parent | bd3da2232540ea4c78e7ac35e2c90c38c8a3c410 (diff) | |
refactor: replace potentially unsafe string functions with snprintf
Diffstat (limited to 'src/Infobox')
| -rw-r--r-- | src/Infobox/Dictionary.c | 3 | ||||
| -rw-r--r-- | src/Infobox/UnitConversion.c | 22 | ||||
| -rw-r--r-- | src/Infobox/Wikipedia.c | 16 |
3 files changed, 20 insertions, 21 deletions
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); |
