Fix potential TypeError parsing lane_number in vegbilder.js#11974
Fix potential TypeError parsing lane_number in vegbilder.js#11974Kayd-06 wants to merge 1 commit intoopenstreetmap:developfrom
Conversation
|
How exactly this bug could have been triggered? |
|
It triggers if the API returns a string for // Example of failing API response property
const properties = { FELTKODE: "unknown" };
const { FELTKODE: lane_code } = properties;
// lane_code.match(/^[0-9]+/) evaluates to null
// Trying to access [0] throws the exception:
const lane_number = parseInt(lane_code.match(/^[0-9]+/)[0], 10);
// TypeError: Cannot read properties of null (reading '0')Because loadTile processes multiple features from the API in a batch, a single unexpected FELTKODE value throws this uncaught exception, which crashes the entire feature loop and prevents the rest of the images in that tile from rendering. This fix safely handles that edge case by falling back to lane 0 (forward direction) when the parser encounters a non-numeric FELTKODE: const lane_match = lane_code.match(/^[0-9]+/);
const lane_number = parseInt(lane_match ? lane_match[0] : '0', 10);
// Parses to 0 |
|
And how one may reproduce it while using iD? Was it at least reported to happen? |
|
It hasn't been explicitly reported as a user-facing issue yet, I found this via code review while looking for potential runtime exceptions. Because While this might be rare in practice, I figured adding a defensive ternary fallback here is a low-risk way to ensure the data ingestion loop remains robust and degrades gracefully to a forward |
Description
This PR fixes a bug in modules/services/vegbilder.js where
lane_code.match(/^[0-9]+/)[0]is accessed directly without verifying iflane_code.matchactually returned a match array.When the match returns
null(if the string does not start with digits), parsing it causes aTypeError: Cannot read properties of null (reading '0').Changes:
lane_match ? lane_match[0] : '0'to safely prevent accessing[0]onnull, making the parsing logic more robust.