Changeset 13194 in josm


Ignore:
Timestamp:
2017-12-05T00:58:56+01:00 (7 years ago)
Author:
Don-vip
Message:

fix #15634 - add robustness against invalid GPX files

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/GpxReader.java

    r12620 r13194  
    9090        }
    9191
     92        private double parseCoord(Attributes atts, String key) {
     93            String val = atts.getValue(key);
     94            if (val != null) {
     95                return parseCoord(val);
     96            } else {
     97                // Some software do not respect GPX schema and use "minLat" / "minLon" instead of "minlat" / "minlon"
     98                return parseCoord(atts.getValue(key.replaceFirst("l", "L")));
     99            }
     100        }
     101
    92102        private double parseCoord(String s) {
    93             try {
    94                 return Double.parseDouble(s);
    95             } catch (NumberFormatException ex) {
    96                 return Double.NaN;
    97             }
     103            if (s != null) {
     104                try {
     105                    return Double.parseDouble(s);
     106                } catch (NumberFormatException ex) {
     107                    Logging.trace(ex);
     108                }
     109            }
     110            return Double.NaN;
    98111        }
    99112
    100113        private LatLon parseLatLon(Attributes atts) {
    101114            return new LatLon(
    102                     parseCoord(atts.getValue("lat")),
    103                     parseCoord(atts.getValue("lon")));
     115                    parseCoord(atts, "lat"),
     116                    parseCoord(atts, "lon"));
    104117        }
    105118
     
    178191                case "bounds":
    179192                    data.put(META_BOUNDS, new Bounds(
    180                                 parseCoord(atts.getValue("minlat")),
    181                                 parseCoord(atts.getValue("minlon")),
    182                                 parseCoord(atts.getValue("maxlat")),
    183                                 parseCoord(atts.getValue("maxlon"))));
     193                                parseCoord(atts, "minlat"),
     194                                parseCoord(atts, "minlon"),
     195                                parseCoord(atts, "maxlat"),
     196                                parseCoord(atts, "maxlon")));
    184197                    break;
    185198                default: // Do nothing
  • trunk/test/unit/org/openstreetmap/josm/io/GpxReaderTest.java

    r12184 r13194  
    1313
    1414import org.junit.Test;
     15import org.openstreetmap.josm.TestUtils;
     16import org.openstreetmap.josm.data.Bounds;
    1517import org.openstreetmap.josm.data.coor.LatLon;
    1618import org.openstreetmap.josm.data.gpx.GpxData;
     
    6466        new GpxReader(new ByteArrayInputStream("--foo--bar--".getBytes(StandardCharsets.UTF_8))).parse(true);
    6567    }
     68
     69    /**
     70     * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/15634">#15634</a>
     71     * @throws IOException if an error occurs during reading
     72     * @throws SAXException if any XML error occurs
     73     */
     74    @Test
     75    public void testTicket15634() throws IOException, SAXException {
     76        assertEquals(new Bounds(53.7229357, -7.9135019, 53.9301103, -7.59656),
     77                GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(15634, "drumlish.gpx")).getMetaBounds());
     78    }
    6679}
Note: See TracChangeset for help on using the changeset viewer.