dictionary: Allow dict_set_documents() argument to reference old documents.
authorBen Pfaff <blp@cs.stanford.edu>
Tue, 5 Oct 2021 16:19:26 +0000 (09:19 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 5 Oct 2021 16:19:26 +0000 (09:19 -0700)
merge_dictionary() in combine-file.c includes the old documents in the new
ones by just copying pointers.  dict_set_documents() didn't handle this
properly.  This fixes the problem.

Fixes bug #61258.
Thanks to Irfan Ariq for reporting the problem.

src/data/dictionary.c

index ab4642a4da493c536b499a60e71479cbb14f7670..4fbcdf2a1e16f237b2988b10d80800a3752c7343 100644 (file)
@@ -1471,12 +1471,15 @@ dict_get_documents (const struct dictionary *d)
 void
 dict_set_documents (struct dictionary *d, const struct string_array *new_docs)
 {
-  size_t i;
-
-  dict_clear_documents (d);
+  /* Swap out the old documents, instead of destroying them immediately, to
+     allow the new documents to include pointers into the old ones. */
+  struct string_array old_docs = STRING_ARRAY_INITIALIZER;
+  string_array_swap (&d->documents, &old_docs);
 
-  for (i = 0; i < new_docs->n; i++)
+  for (size_t i = 0; i < new_docs->n; i++)
     dict_add_document_line (d, new_docs->strings[i], false);
+
+  string_array_destroy (&old_docs);
 }
 
 /* Replaces the documents for D by UTF-8 encoded string NEW_DOCS, dividing it