Skip to content

Fix Lua error with decimal lane values in osrm-extract#7420

Merged
DennisOSRM merged 4 commits intomasterfrom
claude/fix-lua-error-in-guidance
Mar 21, 2026
Merged

Fix Lua error with decimal lane values in osrm-extract#7420
DennisOSRM merged 4 commits intomasterfrom
claude/fix-lua-error-in-guidance

Conversation

@Claude
Copy link
Copy Markdown
Contributor

@Claude Claude AI commented Mar 20, 2026

When processing OSM data with decimal lane values (e.g., lanes=2.5), osrm-extract crashes with: "not a numeric type that fits exactly an integer (number maybe has significant decimals)". This occurs because Sol2's strict type checking rejects Lua floats when C++ expects std::uint8_t.

Changes

  • Modified to_number_uint() in profiles/lib/guidance.lua: Convert validated numbers to integer type using math.tointeger() before returning, ensuring Sol2 bindings accept the value for RoadClassification::num_lanes

  • Added features/car/lanes.feature: Test coverage for valid integer values, invalid decimal values, lanes:forward/lanes:backward tags, and edge cases (zero, negative, non-numeric)

Technical Details

Even when to_number_uint() validated that n % 1 == 0, it returned a Lua number (double) rather than an integer. Sol2 requires exact type matches for C++ integer fields:

local function to_number_uint(s)
  local n = tonumber(s)
  if n ~= nil and n > 0 and n % 1 == 0 then
    return math.tointeger(n) or math.floor(n)  -- Explicit integer conversion
  end
  return nil
end

Invalid lane counts are now silently ignored rather than crashing the extraction process.

Original prompt

This section details on the original issue you should resolve

<issue_title>what(): Lua error (see stderr for traceback)</issue_title>
<issue_description># Issue

[2025-05-28T13:21:55.881822457] [info] Using profile api version 4
[2025-05-28T13:51:34.249052855] [error] stack index 3, expected number, received number: not a numeric type that fits exactly an integer (number maybe has significant decimals) (bad argument into 'void(unsigned char)')
stack traceback:
[C]: in metamethod 'newindex'
/opt/lib/guidance.lua:112: in function 'lib/guidance.set_classification'
/opt/lib/way_handlers.lua:108: in function 'lib/way_handlers.classification'
/opt/lib/way_handlers.lua:711: in function 'lib/way_handlers.run'
/opt/car.lua:463: in function 'process_way'

terminate called after throwing an instance of 'osrm::util::exception'
what(): Lua error (see stderr for traceback)

Steps to reproduce

docker run -t -v "${PWD}:/data" ghcr.io/project-osrm/osrm-backend:v6.0.0-debug osrm-extract -p /opt/car.lua /data/all.pbf || echo "osrm-extract failed"

all.pbf created with osmconvert.

Specifications

  • Operating system
    Oracle Linux Server 8.10
  • Hardware
    ProLiant DL380 Gen9
    Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz X2
    256GiB
    1000G swap
    </issue_description>

<agent_instructions>Verify if the code needs a fix. If yes, create a PR with corresponding cucumber test coverage. If not, close the issue with a message explaining why</agent_instructions>

Comments on the Issue (you are @claude[agent] in this section)

@jcoupey Having the extraction process crash for an unexpected number of lanes sounds pretty harsh. Probably the "faulty" count should be ignored by default as suggested above, maybe with a `[warn]` statement in output as is already the case for many situations with "suspicious" data?

The error occurred because to_number_uint() was returning a Lua float
even for integer values, but Sol2 binding requires exact integer type
for std::uint8_t. Added math.tointeger() conversion to ensure proper
integer type is passed to C++ code.

Co-authored-by: DennisOSRM <1067895+DennisOSRM@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Project-OSRM/osrm-backend/sessions/f697d5b1-5923-449b-a1f3-8ade4d08c1e7
@Claude Claude AI changed the title [WIP] Fix Lua error during classification handling Fix Lua error with decimal lane values in osrm-extract Mar 20, 2026
@Claude Claude AI requested a review from DennisOSRM March 20, 2026 21:41
DennisOSRM and others added 2 commits March 21, 2026 16:06
math.tointeger is only available in Lua 5.3+. Since to_number_uint already
validates n % 1 == 0, math.floor(n) is equivalent and works on all versions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@DennisOSRM DennisOSRM marked this pull request as ready for review March 21, 2026 16:06
@DennisOSRM DennisOSRM merged commit 828973b into master Mar 21, 2026
23 checks passed
@DennisOSRM DennisOSRM deleted the claude/fix-lua-error-in-guidance branch March 21, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

what(): Lua error (see stderr for traceback)

2 participants