Skip to content

Commit fd37b37

Browse files
committed
Merged taxID larger than any taxid in nodes.dmp could corrupt memory #931
1 parent 0898eb9 commit fd37b37

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/taxonomy/NcbiTaxonomy.cpp

+29-5
Original file line numberDiff line numberDiff line change
@@ -457,22 +457,46 @@ size_t NcbiTaxonomy::loadMerged(const std::string &mergedFile) {
457457
EXIT(EXIT_FAILURE);
458458
}
459459

460+
std::unordered_map<TaxID, TaxID> mergedMap;
461+
TaxID localMaxTaxID = maxTaxID;
460462
std::string line;
461-
size_t count = 0;
462463
while (std::getline(ss, line)) {
463464
std::vector<std::string> result = splitByDelimiter(line, "\t|\t", 2);
464465
if (result.size() != 2) {
465466
Debug(Debug::ERROR) << "Invalid name entry!\n";
466467
EXIT(EXIT_FAILURE);
467468
}
468469

469-
unsigned int oldId = (unsigned int)strtoul(result[0].c_str(), NULL, 10);
470-
unsigned int mergedId = (unsigned int)strtoul(result[1].c_str(), NULL, 10);
470+
TaxID oldId = (TaxID) strtoul(result[0].c_str(), NULL, 10);
471+
TaxID mergedId = (TaxID) strtoul(result[1].c_str(), NULL, 10);
472+
473+
// Only update if the oldId doesn't exist yet AND the mergedId does exist
471474
if (!nodeExists(oldId) && nodeExists(mergedId)) {
472-
D[oldId] = D[mergedId];
473-
++count;
475+
if (oldId > localMaxTaxID) {
476+
localMaxTaxID = oldId;
477+
}
478+
if (mergedId > localMaxTaxID) {
479+
localMaxTaxID = mergedId;
480+
}
481+
mergedMap[oldId] = mergedId;
474482
}
475483
}
484+
485+
// realloc D if we find a higher maxTaxID
486+
if (localMaxTaxID > maxTaxID) {
487+
int* newD = new int[localMaxTaxID + 1];
488+
std::copy(D, D + maxTaxID + 1, newD);
489+
std::fill(newD + maxTaxID + 1, newD + (localMaxTaxID + 1), -1);
490+
delete[] D;
491+
D = newD;
492+
maxTaxID = localMaxTaxID;
493+
}
494+
495+
size_t count = 0;
496+
for (std::unordered_map<TaxID, TaxID>::iterator it = mergedMap.begin(); it != mergedMap.end(); ++it) {
497+
D[it->first] = D[it->second];
498+
++count;
499+
}
476500
Debug(Debug::INFO) << " Done, added " << count << " merged nodes.\n";
477501
return count;
478502
}

0 commit comments

Comments
 (0)