#13692·organicmaps

Use-after-move / empty vector dereference in MergePoints (libs/drape_frontend/read_metaline_task.cpp)

Author: HugeCreated Oct 1, 2026Updated Oct 1, 2026

Description of the Issue

In libs/drape_frontend/read_metaline_task.cpp, MergePoints() can dereference an iterator on an empty vector, leading to undefined behavior / crash (SIGSEGV) whenever a metaline contains duplicate FeatureIDs (e.g. [A, B, A]).

Location

libs/drape_frontend/read_metaline_task.cpp (lines ~103–118 in MergePoints):

cpp
  for (auto const & f : featuresOrder)
  {
    auto const it = points.find(f);
    ASSERT(it != points.cend(), ());

    if (!result.empty())
    {
      auto iBeg = it->second.begin();
      if (result.back().EqualDxDy(*iBeg, kMwmPointAccuracy))
        ++iBeg;
      result.insert(result.end(), iBeg, it->second.end());
    }
    else
      result = std::move(it->second);
  }

Mechanism / Root Cause

  1. No deduplication in reader: ReadMetalinesFromFile() parses MetalineData::m_features directly from varints without checking for duplicates:
    cpp
    auto const fid = ReadVarInt<int32_t>(src);
    FeatureID const featureId(mwmId, static_cast<uint32_t>(std::abs(fid)));
    data.m_features.emplace_back(featureId);
  2. Move on first occurrence: In MergePoints(), if points.size() > 1 (bypassing the single-feature early return), the loop starts with result.empty() == true. The first feature in featuresOrder (e.g. A) falls into the else branch:
    cpp
    result = std::move(it->second);
    This moves the vector contents into result, leaving points[A] empty (it->second.empty() == true).
  3. Empty dereference on subsequent occurrence: When feature A is encountered again later in featuresOrder (e.g. with featuresOrder = [A, B, A]):
    • !result.empty() is now true.
    • auto iBeg = it->second.begin(); is obtained. Since it->second is empty, iBeg == it->second.end().
    • result.back().EqualDxDy(*iBeg, kMwmPointAccuracy) unconditionally dereferences *iBeg.
    • Dereferencing end() of an empty vector is undefined behavior, causing an invalid memory read / crash.

Suggested Fix

At minimum, guard against an empty vector or check iBeg != it->second.end() before dereferencing:

cpp
    if (!result.empty())
    {
      if (it->second.empty())
        continue;

      auto iBeg = it->second.begin();
      if (result.back().EqualDxDy(*iBeg, kMwmPointAccuracy))
        ++iBeg;
      result.insert(result.end(), iBeg, it->second.end());
    }
    else
      result = std::move(it->second);

Or ensure deduplication / consistency when populating m_features in ReadMetalinesFromFile().


Note: Due to current time constraints, I will not be able to prepare a repro test case or submit a PR before next week. Opening this report early so the team is aware of the issue.