Skip to content

Commit 9137c5d

Browse files
PhilippVoigtkevinkreisernilsnolde
authored
Potential integer underflow in file suffix generation (valhalla#3783)
* Potential integer underflow in file suffix generation Integer underflow if maximum tile ID for leven is a 6-digit number. Signed-off-by: Philipp Voigt (<[email protected]>) * Potential integer underflow in file suffix generation Added CHANGELOG.md entry Signed-off-by: Philipp Voigt (<[email protected]>) * Potential integer underflow in file suffix generation Added CHANGELOG.md entry Signed-off-by: Philipp Voigt (<[email protected]>) * Potential integer underflow in file suffix generation Moved CHANGELOG.md entry down Signed-off-by: Philipp Voigt (<[email protected]>) * add a test for tile configs with a max tile id that is a multiple of 3 digits. master doesnt fail though * Make test fail on master Added test case with GraphId that uses all 6 available digits Signed-off-by: Philipp Voigt (<[email protected]>) * Make test fail on master Confirmed that test fails on master and removed assert Signed-off-by: Philipp Voigt (<[email protected]>) * Make test fail on master Confirmed that test fails on master and removed assert Signed-off-by: Philipp Voigt (<[email protected]>) * Update CHANGELOG.md Signed-off-by: Philipp Voigt (<[email protected]>) Co-authored-by: Kevin Kreiser <[email protected]> Co-authored-by: Nils <[email protected]>
1 parent 1be0105 commit 9137c5d

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* FIXED: Fix status endpoint not reporting that the service is shutting down [#3785](https://github.com/valhalla/valhalla/pull/3785)
5858
* FIXED: Fix TimdDistanceMatrix SetSources and SetTargets [#3792](https://github.com/valhalla/valhalla/pull/3792)
5959
* FIXED: Added highway and surface factor in truckcost [#3590](https://github.com/valhalla/valhalla/pull/3590)
60+
* FIXED: Potential integer underflow in file suffix generation [#3783](https://github.com/valhalla/valhalla/pull/3783)
6061

6162
* **Enhancement**
6263
* CHANGED: Pronunciation for names and destinations [#3132](https://github.com/valhalla/valhalla/pull/3132)

src/baldr/graphtile.cc

+14-10
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,10 @@ void GraphTile::AssociateOneStopIds(const GraphId& graphid) {
422422
}
423423
}
424424

425-
std::string
426-
GraphTile::FileSuffix(const GraphId& graphid, const std::string& fname_suffix, bool is_file_path) {
425+
std::string GraphTile::FileSuffix(const GraphId& graphid,
426+
const std::string& fname_suffix,
427+
bool is_file_path,
428+
const TileLevel* tiles) {
427429
/*
428430
if you have a graphid where level == 8 and tileid == 24134109851 you should get:
429431
8/024/134/109/851.gph since the number of levels is likely to be very small this limits the total
@@ -433,16 +435,18 @@ GraphTile::FileSuffix(const GraphId& graphid, const std::string& fname_suffix, b
433435
*/
434436

435437
// figure the largest id for this level
436-
if (graphid.level() >= TileHierarchy::levels().size() &&
437-
graphid.level() != TileHierarchy::GetTransitLevel().level) {
438+
if ((tiles && tiles->level != graphid.level()) ||
439+
(!tiles && graphid.level() >= TileHierarchy::levels().size() &&
440+
graphid.level() != TileHierarchy::GetTransitLevel().level)) {
438441
throw std::runtime_error("Could not compute FileSuffix for GraphId with invalid level: " +
439442
std::to_string(graphid));
440443
}
441444

442445
// get the level info
443-
const auto& level = graphid.level() == TileHierarchy::GetTransitLevel().level
444-
? TileHierarchy::GetTransitLevel()
445-
: TileHierarchy::levels()[graphid.level()];
446+
const auto& level = tiles ? *tiles
447+
: (graphid.level() == TileHierarchy::GetTransitLevel().level
448+
? TileHierarchy::GetTransitLevel()
449+
: TileHierarchy::levels()[graphid.level()]);
446450

447451
// figure out how many digits in tile-id
448452
const auto max_id = level.tiles.ncolumns() * level.tiles.nrows() - 1;
@@ -468,11 +472,11 @@ GraphTile::FileSuffix(const GraphId& graphid, const std::string& fname_suffix, b
468472
for (uint32_t tile_id = graphid.tileid(); tile_id != 0; tile_id /= 10) {
469473
tile_id_str[ind--] = '0' + static_cast<char>(tile_id % 10);
470474
if ((tile_id_strlen - ind) % 4 == 0) {
471-
tile_id_str[ind--] = separator;
475+
ind--; // skip an additional character to leave space for separators
472476
}
473477
}
474-
// add missing separators
475-
for (size_t sep_ind = 0; sep_ind < ind; sep_ind += 4) {
478+
// add separators
479+
for (size_t sep_ind = 0; sep_ind < tile_id_strlen; sep_ind += 4) {
476480
tile_id_str[sep_ind] = separator;
477481
}
478482

test/graphtile.cc

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ TEST(Graphtile, FileSuffix) {
3232
EXPECT_THROW(GraphTile::FileSuffix(GraphId(1036800, 2, 0)), std::runtime_error);
3333
EXPECT_THROW(GraphTile::FileSuffix(GraphId(4050, 0, 0)), std::runtime_error);
3434
EXPECT_THROW(GraphTile::FileSuffix(GraphId(1036800, 3, 0)), std::runtime_error);
35+
36+
TileLevel level{7, valhalla::baldr::RoadClass::kSecondary, "half_degree_is_a_multiple_of_3",
37+
Tiles<PointLL>{{{-180, -90}, {180, 90}}, .5, 1}};
38+
39+
EXPECT_EQ(GraphTile::FileSuffix(GraphId(1234, 7, 0), ".qux", false, &level), "7/001/234.qux");
40+
EXPECT_EQ(GraphTile::FileSuffix(GraphId(123456, 7, 0), ".qux", false, &level), "7/123/456.qux");
3541
}
3642

3743
TEST(Graphtile, IdFromString) {

valhalla/baldr/graphtile.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,17 @@ class GraphTile {
111111

112112
/**
113113
* Gets the directory like filename suffix given the graphId
114-
* @param graphid Graph Id to construct filename.
115-
* @param gzipped Modifies the suffix if you expect gzipped file names
114+
* @param graphid Graph Id to construct filename.
115+
* @param gzipped Modifies the suffix if you expect gzipped file names
116116
* @param is_file_path Determines the 1000 separator to be used for file or URL access
117+
* @param tiles Allows passing a custom tile definition rather than pulling from static
118+
* hierarchy, which is useful for testing
117119
* @return Returns a filename including directory path as a suffix to be appended to another uri
118120
*/
119121
static std::string FileSuffix(const GraphId& graphid,
120122
const std::string& suffix = valhalla::baldr::SUFFIX_NON_COMPRESSED,
121-
bool is_file_path = true);
123+
bool is_file_path = true,
124+
const TileLevel* tiles = nullptr);
122125

123126
/**
124127
* Get the tile Id given the full path to the file.

0 commit comments

Comments
 (0)