Browse Source

fix: `char *` size in realloc

master
Dnomd343 2 years ago
parent
commit
e0e9af1784
  1. 4
      src/common.c
  2. 2
      src/utils/dns.c

4
src/common.c

@ -10,7 +10,7 @@ char* new_string(char *str) {
char* int_to_string(int num) { // int -> string char* int_to_string(int num) { // int -> string
if (num < 0) { if (num < 0) {
log_fatal("number must be positive"); log_fatal("Number must be positive");
} }
int count = 0; int count = 0;
int temp = num; int temp = num;
@ -26,7 +26,7 @@ char* int_to_string(int num) { // int -> string
char** string_list_append(char **string_list, char *data) { char** string_list_append(char **string_list, char *data) {
int num = 0; int num = 0;
while(string_list[num++] != NULL); // get string list size while(string_list[num++] != NULL); // get string list size
string_list = (char**)realloc(string_list, sizeof(char**) * (num + 1)); string_list = (char**)realloc(string_list, sizeof(char*) * (num + 1));
string_list[num - 1] = new_string(data); string_list[num - 1] = new_string(data);
string_list[num] = NULL; // list end sign string_list[num] = NULL; // list end sign
return string_list; return string_list;

2
src/utils/dns.c

@ -22,7 +22,7 @@ char** init_dns_result() { // 初始化DNS解析存储结构
char** add_dns_result(char **dns_result, char *str) { // 添加DNS解析记录 char** add_dns_result(char **dns_result, char *str) { // 添加DNS解析记录
int num = 0; int num = 0;
while(dns_result[num++] != NULL); // 获取原存储个数 while(dns_result[num++] != NULL); // 获取原存储个数
dns_result = (char**)realloc(dns_result, sizeof(char**) * (num + 1)); dns_result = (char**)realloc(dns_result, sizeof(char*) * (num + 1));
dns_result[num - 1] = strcpy((char*)malloc(strlen(str) + 1), str); dns_result[num - 1] = strcpy((char*)malloc(strlen(str) + 1), str);
dns_result[num] = NULL; // 结束标志 dns_result[num] = NULL; // 结束标志
return dns_result; return dns_result;

Loading…
Cancel
Save