diff --git a/library/Tries/base_itries.c b/library/Tries/base_itries.c index 133fa57f9..b337c253c 100644 --- a/library/Tries/base_itries.c +++ b/library/Tries/base_itries.c @@ -369,12 +369,15 @@ TrEntry itrie_load(FILE *file) { TrNode node; new_itrie_entry(itrie, NULL); + CURRENT_ITRIE = itrie; + if (!(node = core_trie_load(ITRIE_ENGINE, file, &itrie_data_load))) { + free_itrie_entry(itrie); + return NULL; + } + TrEntry_trie(itrie) = node; if (FIRST_ITRIE) TrEntry_previous(FIRST_ITRIE) = itrie; FIRST_ITRIE = itrie; - CURRENT_ITRIE = itrie; - node = core_trie_load(ITRIE_ENGINE, file, &itrie_data_load); - TrEntry_trie(itrie) = node; return itrie; } diff --git a/library/Tries/base_tries.c b/library/Tries/base_tries.c index 8ed21dfa7..b17978174 100644 --- a/library/Tries/base_tries.c +++ b/library/Tries/base_tries.c @@ -240,12 +240,15 @@ TrEntry trie_load(FILE *file) { TrNode node; new_trie_entry(trie, NULL); + CURRENT_TRIE = trie; + if (!(node = core_trie_load(TRIE_ENGINE, file, &trie_data_load))) { + free_trie_entry(trie); + return NULL; + } + TrEntry_trie(trie) = node; if (FIRST_TRIE) TrEntry_previous(FIRST_TRIE) = trie; FIRST_TRIE = trie; - CURRENT_TRIE = trie; - node = core_trie_load(TRIE_ENGINE, file, &trie_data_load); - TrEntry_trie(trie) = node; return trie; } diff --git a/library/Tries/core_tries.c b/library/Tries/core_tries.c index 4e276cec2..5124d2f6a 100644 --- a/library/Tries/core_tries.c +++ b/library/Tries/core_tries.c @@ -429,19 +429,41 @@ TrNode core_trie_load(TrEngine engine, FILE *file, void (*load_function)(TrNode, TrNode node; char version[15]; + fscanf(file, "%14s", version); + if (!strcmp(version, "BEGIN_TRIE_v2")) { + fseek(file, -11, SEEK_END); + fscanf(file, "%s", version); + if (strcmp(version, "END_TRIE_v2")) { + fprintf(stderr, "******************************************\n"); + fprintf(stderr, " Tries core module: trie file corrupted\n"); + fprintf(stderr, "******************************************\n"); + return NULL; + } + fseek(file, 13, SEEK_SET); + CURRENT_LOAD_VERSION = 2; + } else if (!strcmp(version, "BEGIN_TRIE")) { + fseek(file, -8, SEEK_END); + fscanf(file, "%s", version); + if (strcmp(version, "END_TRIE")) { + fprintf(stderr, "******************************************\n"); + fprintf(stderr, " Tries core module: trie file corrupted\n"); + fprintf(stderr, "******************************************\n"); + return NULL; + } + fseek(file, 10, SEEK_SET); + CURRENT_LOAD_VERSION = 1; + } else { + fprintf(stderr, "****************************************\n"); + fprintf(stderr, " Tries core module: invalid trie file\n"); + fprintf(stderr, "****************************************\n"); + return NULL; + } CURRENT_TRIE_ENGINE = engine; CURRENT_INDEX = -1; CURRENT_DEPTH = 0; DATA_LOAD_FUNCTION = load_function; node = core_trie_open(engine); - fscanf(file, "%s", version); - if (!strcmp(version, "BEGIN_TRIE_v2")) { - CURRENT_LOAD_VERSION = 2; - traverse_and_load(node, file); - } else if (!strcmp(version, "BEGIN_TRIE")) { - CURRENT_LOAD_VERSION = 1; - traverse_and_load(node, file); - } + traverse_and_load(node, file); return node; } diff --git a/library/Tries/itries.c b/library/Tries/itries.c index 07559d9d1..72f2b46c9 100644 --- a/library/Tries/itries.c +++ b/library/Tries/itries.c @@ -579,6 +579,8 @@ static int p_itrie_load(void) { itrie = itrie_load(file); if (fclose(file)) return FALSE; + if (!itrie) + return FALSE; return YAP_Unify(arg_itrie, YAP_MkIntTerm((YAP_Int) itrie)); } #undef arg_itrie diff --git a/library/Tries/tries.c b/library/Tries/tries.c index b11a46949..cf34f4518 100644 --- a/library/Tries/tries.c +++ b/library/Tries/tries.c @@ -527,6 +527,8 @@ static int p_trie_load(void) { data = trie_load(file); if (fclose(file)) return FALSE; + if (!data) + return FALSE; return YAP_Unify(arg_trie, YAP_MkIntTerm((YAP_Int) data)); } #undef arg_trie