fix: config_file_read handle file error before hashmap_new
This commit is contained in:
+10
-4
@@ -68,16 +68,18 @@ void config_file_read(ConfigFile *config, const char *path) {
|
||||
char *line;
|
||||
char *rest;
|
||||
|
||||
file_read(&file, path);
|
||||
config->map = NULL;
|
||||
|
||||
config->map = hashmap_new(sizeof(ConfigFileItem), 0, 0, 0, item_hash,
|
||||
item_compare, NULL, NULL);
|
||||
file_read(&file, path);
|
||||
|
||||
if (file.error) {
|
||||
config->error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
config->map = hashmap_new(sizeof(ConfigFileItem), 0, 0, 0, item_hash,
|
||||
item_compare, NULL, NULL);
|
||||
|
||||
config->error = false;
|
||||
|
||||
line = strtok_r(file.content, "\n", &rest);
|
||||
@@ -138,4 +140,8 @@ unsigned int config_file_get_int(const ConfigFile *config, const char *key,
|
||||
return (unsigned int)atoi(item->value);
|
||||
}
|
||||
|
||||
void config_file_free(const ConfigFile *config) { hashmap_free(config->map); }
|
||||
void config_file_free(const ConfigFile *config) {
|
||||
if (config->map != NULL) {
|
||||
hashmap_free(config->map);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user