Merge lp:~widelands-dev/widelands/bugfix-1805042-S2map-preloader-checks into lp:widelands

Proposed by Arty
Status: Merged
Merged at revision: 8953
Proposed branch: lp:~widelands-dev/widelands/bugfix-1805042-S2map-preloader-checks
Merge into: lp:widelands
Diff against target: 58 lines (+31/-1)
1 file modified
src/map_io/s2map.cc (+31/-1)
To merge this branch: bzr merge lp:~widelands-dev/widelands/bugfix-1805042-S2map-preloader-checks
Reviewer Review Type Date Requested Status
GunChleoc Approve
Review via email: mp+360762@code.launchpad.net

Commit message

validity checks for S2 map headers to avoid crashes when preloading invalid S2 map files

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

Continuous integration builds have changed state:

Travis build 4323. State: failed. Details: https://travis-ci.org/widelands/widelands/builds/467356938.
Appveyor build 4118. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_bugfix_1805042_S2map_preloader_checks-4118.

Revision history for this message
GunChleoc (gunchleoc) wrote :

LGTM :)

@bunnybot merge

review: Approve
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 4323. State: failed. Details: https://travis-ci.org/widelands/widelands/builds/467356938.

Revision history for this message
bunnybot (widelandsofficial) wrote :

Continuous integration builds have changed state:

Travis build 4366. State: passed. Details: https://travis-ci.org/widelands/widelands/builds/472186394.
Appveyor build 4159. State: success. Details: https://ci.appveyor.com/project/widelands-dev/widelands/build/_widelands_dev_widelands_bugfix_1805042_S2map_preloader_checks-4159.

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

@bunnybot merge

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/map_io/s2map.cc'
2--- src/map_io/s2map.cc 2018-12-13 07:24:01 +0000
3+++ src/map_io/s2map.cc 2018-12-25 21:33:11 +0000
4@@ -61,6 +61,29 @@
5 char bulk[2290]; // unknown
6 } /* size 2352 */;
7
8+// Some basic checks to identify obviously invalid headers
9+bool is_valid_header(const S2MapDescrHeader& header) {
10+ if (strncmp(header.magic, "WORLD_V1.0", 10)) {
11+ return false;
12+ }
13+ if (header.name[19]) {
14+ return false;
15+ }
16+ if (header.w <= 0 || header.h <= 0) {
17+ return false;
18+ }
19+ if (header.uses_world < 0 || header.uses_world > 2) {
20+ return false;
21+ }
22+ if (header.nplayers < 0 || header.nplayers > 7) {
23+ return false;
24+ }
25+ if (header.author[19]) {
26+ return false;
27+ }
28+ return true;
29+}
30+
31 // TODO(unknown): the following bob types appear in S2 maps but are unknown
32 // Somebody who can run Settlers II please check them out
33 // 11 (0x0B)
34@@ -400,9 +423,11 @@
35 }
36
37 /**
38- * Load informational data of an S2 map
39+ * Loads informational data of an S2 map.
40+ * Throws exception if data is invalid.
41 */
42 void S2MapLoader::load_s2mf_header(FileRead& fr) {
43+ // no need to check file size: fr.data(..) already throws if the file is too small
44 S2MapDescrHeader header;
45 memcpy(&header, fr.data(sizeof(header)), sizeof(header));
46
47@@ -414,6 +439,11 @@
48 header.h = swap_16(header.h);
49 #endif
50
51+ // Check header validity to prevent unexpected crashes later
52+ if (!is_valid_header(header)) {
53+ throw wexception("invalid S2 file");
54+ }
55+
56 // don't really set size, but make the structures valid
57 map_.width_ = header.w;
58 map_.height_ = header.h;

Subscribers

People subscribed via source and target branches

to status/vote changes: