java-topology/defects/calligra-0001/README.md
russell@unturf.com ad90dddbcc libreoffice+calligra: 2 CWE-407 defects, MOADs 0002-0005 CLEAN
libreoffice-0001: SavePivotTableXml member-to-cache O(M*C) std::find
  sc/source/filter/excel/xepivotxml.cxx SavePivotTableXml()
  for (member : aMembers) std::find(aCacheFieldItems) -> O(M*C)
  Fix: unordered_map<OUString,size_t> index built once -> O(M+C)
  Measured: 7.1x speedup at M=C=2000

calligra-0001: KoShapeManager addShape QList::contains O(N^2) dedup
  libs/flake/KoShapeManager.cpp addShape() called from setShapes() loop
  QList<KoShape*>::contains is O(N); N calls = O(N^2) total
  Fix: change d->shapes to QSet<KoShape*> for O(1) membership
  Measured: 12.6x speedup at N=5000

MOADs 0002-0005: CLEAN for both targets (see README.md per defect)
Both unit tests: PASS
2026-03-31 20:29:29 -04:00

2 KiB

calligra-0001: KoShapeManager::addShape QList::contains O(N^2) dedup

Target: Calligra (libs/flake/KoShapeManager.cpp) MOAD: 0001 (CWE-407) Severity: MEDIUM Complexity: O(N^2) -> O(N) Measured speedup: 12.6x at N=5000

Location

libs/flake/KoShapeManager.cpp KoShapeManager::addShape(), line 138

Defect

KoShapeManager::setShapes() iterates over all shapes and calls addShape() for each. Inside addShape(), a membership guard uses QList::contains():

if (d->shapes.contains(shape))   // QList::contains is O(N)
    return;
d->shapes.append(shape);

Since d->shapes is a QList<KoShape*> and the list grows with each call, the i-th shape requires scanning i entries. Total cost for N shapes is O(N^2).

This affects document load, SVG import (SvgImport.cpp iterates shapes per layer), and image export (painter.setShapes(page->shapes()) called per page).

Fix

Change d->shapes and d->additionalShapes from QList<KoShape*> to QSet<KoShape*>. QSet::contains and QSet::insert are O(1). Replace removeAll with remove. The shapes() accessor returns d->shapes.values() to preserve the QList<KoShape*> public API.

MOAD-0002 through 0005 (scan notes)

  • MOAD-0002 (Intertangle): KoDocument/KoPADocument is a typical Qt document god object. No new coupling beyond expected KDE Frameworks patterns. CLEAN.
  • MOAD-0003 (Leaked Context): QThreadStorage<FastPathCacheItem*> in KoColorConversionCache is a per-thread performance fast path for color space conversion, not request-scoped identity. CLEAN.
  • MOAD-0004 (CWE-312): Calligra Sidewinder filter logs qCDebug(lcSidewinder) << "passwordHash=" << record->wPassword() where wPassword() is the XLS 16-bit scrambled hash, not our plaintext password. qCDebug is a categorized Qt debug message compiled away in release or silenced unless QT_LOGGING_RULES=*.debug=true. LOW severity. CLEAN.
  • MOAD-0005 (Thundering Herd): No unguarded concurrent cache patterns found. CLEAN.