Merge lp:~widelands-dev/widelands/bug-1815613-map-origin-segfault into lp:widelands

Proposed by GunChleoc
Status: Merged
Merged at revision: 8985
Proposed branch: lp:~widelands-dev/widelands/bug-1815613-map-origin-segfault
Merge into: lp:widelands
Diff against target: 82 lines (+18/-12)
1 file modified
src/logic/map.cc (+18/-12)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bug-1815613-map-origin-segfault
Reviewer Review Type Date Requested Status
hessenfarmer test Approve
kaputtnik (community) testing Approve
Review via email: mp+363080@code.launchpad.net

Commit message

Fix number overflow in Map::set_origin and clean up the function.

Description of the change

The fix is replacing decltype with size_t, because the positive range of int16_t is too small. The rest is cleanup.

To post a comment you must log in.
Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 4463. State: errored. Details: https://travis-ci.org/widelands/widelands/builds/492316137.
Appveyor build 4251. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_bug_1815613_map_origin_segfault-4251.

Revision history for this message
kaputtnik (franku) wrote :

Good to have a general log output.

Tested and working :-)

review: Approve (testing)
Revision history for this message
hessenfarmer (stephan-lutz) wrote :

tested and works like a charm

@bunnybot merge

review: Approve (test)
Revision history for this message
bunnybot (widelandsofficial) wrote :

Refusing to merge, since Travis is not green. Use @bunnybot merge force for merging anyways.

Travis build 4463. State: errored. Details: https://travis-ci.org/widelands/widelands/builds/492316137.

Revision history for this message
Toni Förster (stonerl) wrote :

The error was just a timeout in Travis.

@bunnybot merge force

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/logic/map.cc'
--- src/logic/map.cc 2018-12-13 07:24:01 +0000
+++ src/logic/map.cc 2019-02-12 17:49:27 +0000
@@ -330,60 +330,66 @@
330 assert(0 <= new_origin.y);330 assert(0 <= new_origin.y);
331 assert(new_origin.y < height_);331 assert(new_origin.y < height_);
332332
333 const size_t field_size = width_ * height_;
334
333 for (uint8_t i = get_nrplayers(); i;) {335 for (uint8_t i = get_nrplayers(); i;) {
334 starting_pos_[--i].reorigin(new_origin, extent());336 starting_pos_[--i].reorigin(new_origin, extent());
335 }337 }
336338
337 std::unique_ptr<Field[]> new_field_order(new Field[width_ * height_]);339 std::unique_ptr<Field[]> new_field_order(new Field[field_size]);
338 memset(new_field_order.get(), 0, sizeof(Field) * width_ * height_);340 memset(new_field_order.get(), 0, sizeof(Field) * field_size);
339341
340 // Rearrange The fields342 // Rearrange The fields
341 // NOTE because of the triangle design, we have to take special care about cases343 // NOTE because of the triangle design, we have to take special care of cases
342 // NOTE where y is changed by an odd number344 // NOTE where y is changed by an odd number
343 bool yisodd = (new_origin.y % 2) != 0;345 bool yisodd = (new_origin.y % 2) != 0;
344 for (Coords c(Coords(0, 0)); c.y < height_; ++c.y) {346 for (Coords c(Coords(0, 0)); c.y < height_; ++c.y) {
345 bool cyisodd = (c.y % 2) != 0;347 bool cyisodd = (c.y % 2) != 0;
346 for (c.x = 0; c.x < width_; ++c.x) {348 for (c.x = 0; c.x < width_; ++c.x) {
347 Coords temp;349 Coords temp;
348 if (yisodd && cyisodd)350 if (yisodd && cyisodd) {
349 temp = Coords(c.x + new_origin.x + 1, c.y + new_origin.y);351 temp = Coords(c.x + new_origin.x + 1, c.y + new_origin.y);
350 else352 } else {
351 temp = Coords(c.x + new_origin.x, c.y + new_origin.y);353 temp = Coords(c.x + new_origin.x, c.y + new_origin.y);
354 }
352 normalize_coords(temp);355 normalize_coords(temp);
353 new_field_order[get_index(c, width_)] = operator[](temp);356 new_field_order[get_index(c, width_)] = operator[](temp);
354 }357 }
355 }358 }
356 // Now that we restructured the fields, we just overwrite the old order359 // Now that we restructured the fields, we just overwrite the old order
357 for (decltype(width_) ind = 0; ind < width_ * height_; ind++) {360 for (size_t ind = 0; ind < field_size; ind++) {
358 fields_[ind] = new_field_order[ind];361 fields_[ind] = new_field_order[ind];
359 }362 }
360363
361 // Inform immovables and bobs about their new coordinates.364 // Inform immovables and bobs about their new coordinates.
362 for (FCoords c(Coords(0, 0), fields_.get()); c.y < height_; ++c.y)365 for (FCoords c(Coords(0, 0), fields_.get()); c.y < height_; ++c.y) {
363 for (c.x = 0; c.x < width_; ++c.x, ++c.field) {366 for (c.x = 0; c.x < width_; ++c.x, ++c.field) {
364 assert(c.field == &operator[](c));367 assert(c.field == &operator[](c));
365 if (upcast(Immovable, immovable, c.field->get_immovable()))368 if (upcast(Immovable, immovable, c.field->get_immovable())) {
366 immovable->position_ = c;369 immovable->position_ = c;
370 }
367 for (Bob* bob = c.field->get_first_bob(); bob; bob = bob->get_next_bob()) {371 for (Bob* bob = c.field->get_first_bob(); bob; bob = bob->get_next_bob()) {
368 bob->position_.x = c.x;372 bob->position_.x = c.x;
369 bob->position_.y = c.y;373 bob->position_.y = c.y;
370 bob->position_.field = c.field;374 bob->position_.field = c.field;
371 }375 }
372 }376 }
377 }
373378
374 // Take care about port spaces379 // Take care of port spaces
375 PortSpacesSet new_port_spaces;380 PortSpacesSet new_port_spaces;
376 for (PortSpacesSet::iterator it = port_spaces_.begin(); it != port_spaces_.end(); ++it) {381 for (PortSpacesSet::iterator it = port_spaces_.begin(); it != port_spaces_.end(); ++it) {
377 Coords temp;382 Coords temp;
378 if (yisodd && ((it->y % 2) == 0))383 if (yisodd && ((it->y % 2) == 0)) {
379 temp = Coords(it->x - new_origin.x - 1, it->y - new_origin.y);384 temp = Coords(it->x - new_origin.x - 1, it->y - new_origin.y);
380 else385 } else {
381 temp = Coords(it->x - new_origin.x, it->y - new_origin.y);386 temp = Coords(it->x - new_origin.x, it->y - new_origin.y);
387 }
382 normalize_coords(temp);388 normalize_coords(temp);
383 log("(%i,%i) -> (%i,%i)\n", it->x, it->y, temp.x, temp.y);
384 new_port_spaces.insert(temp);389 new_port_spaces.insert(temp);
385 }390 }
386 port_spaces_ = new_port_spaces;391 port_spaces_ = new_port_spaces;
392 log("Map origin was shifted by (%d, %d)\n", new_origin.x, new_origin.y);
387}393}
388394
389/*395/*

Subscribers

People subscribed via source and target branches

to status/vote changes: