【发布时间】:2015-11-21 10:39:42
【问题描述】:
我正在使用libconfig 库从文件中读取一些配置数据。我无法提取用于解析信息和事后清理的功能。
运行strcpy(*hostname, tmp) 会导致核心转储。
hostname、port 和 ip 被初始化为 NULL。
int parseConfig(char **hostname, char **port, char **ip) {
config_t cfg, *cf;
const char *tmp;
cf = &cfg;
config_init(cf);
if(!config_read_file(cf, CONFIG)) {
fprintf(stderr, "%s:%d - %s\n",
config_error_file(cf),
config_error_line(cf),
config_error_text(cf));
config_destroy(cf);
return(EXIT_FAILURE);
}
config_lookup_string(cf, "hostname", &tmp);
strcpy(*hostname, tmp);
config_lookup_string(cf, "ip", &tmp);
strcpy(*ip, tmp);
config_lookup_string(cf, "port", &tmp);
strcpy(*port, tmp);
config_destroy(cf);
return(EXIT_SUCCESS);
}
【问题讨论】:
-
parseConfig怎么称呼?您编译时是否包含所有警告和调试信息 (gcc -Wall -Wextra -g)?您是否使用了调试器 (gdb)? valgrind ? -
最好为主机名而不是**主机名提供一个最大大小的数组,这样当你的函数用户提供一个太小的缓冲区时,他不会感到惊讶。例如字符主机名[100];
parseConfig(char* hostName, int maxLenHostName, ...);