GitHub advisory · PyPI
NLTK: Downloader cyclic collection indices cause an infinite closure walk
Description
affected: PyPI
nltk>= 3.6.7—nltk.downloaderindex parsing (Downloader._update_index) dynamically reproduced:nltk3.6.7(4/4 checks),3.9.2(4/4),3.10.3(4/4), anddevelop6818767b9(4/4)
Source references (nltk/downloader.py:NNN) are to commit 6818767b9 (develop, v3.10.3-15-g6818767b9). The v3.10.3 tag and the released 3.10.3 wheel carry the identical code at the identical line numbers (the vulnerable closure BFS sits at downloader.py:1300-1310). Dynamically verified wheels additionally show the same defect at downloader.py:972 (3.9.2) and downloader.py:987 (3.6.7).
Downloader._update_index (nltk/downloader.py:1253-1313) is the mandatory first step of every downloader operation (download(), info(), packages(), collections(), list(), status()). After replacing each <item ref="…"> string with the actual Collection/Package object (downloader.py:1285-1290), it computes each collection's recursive package closure with a breadth-first walk that has no visited set and no depth cap:
# nltk/downloader.py:1300-1310
for collection in self._collections.values():
packages = {}
queue = [collection]
for child in queue:
if isinstance(child, Collection):
queue.extend(child.children)
elif isinstance(child, Package):
packages[child.id] = child
collection.packages = packages.values()
A self-referencing or mutually-referencing index — e.g. <collection id="a"><item ref="a"/></collection> — turns a.children into an object-level cycle, and the for child in queue: queue.extend(child.children) loop appends to the very list it is iterating, so the queue grows forever. The function never returns: CPU spins at 100% and memory grows without bound until the process is killed or the host runs out of memory. The identical finite input (one collection containing one package) completes in ~0.2 s on the control…
Manual fix · PyPI
pip install --upgrade "nltk>=3.10.4"Sources