Ignore:
Timestamp:
2013-12-16T00:22:44+01:00 (12 years ago)
Author:
Don-vip
Message:

fix #9434 - Robustness in hash URLs parsing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/OsmUrlToBounds.java

    r6453 r6474  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.tools;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.awt.HeadlessException;
     
    1517public final class OsmUrlToBounds {
    1618    private static final String SHORTLINK_PREFIX = "http://osm.org/go/";
    17    
     19
    1820    private OsmUrlToBounds() {
    1921        // Hide default constructor for utils classes
    2022    }
    2123
    22     public static Bounds parse(String url) {
     24    public static Bounds parse(String url) throws IllegalArgumentException {
    2325        try {
    2426            // a percent sign indicates an encoded URL (RFC 1738).
     
    8688     * @param url string for parsing
    8789     * @return Bounds if hashurl, {@code null} otherwise
    88      */
    89     private static Bounds parseHashURLs(String url) {
     90     * @throws IllegalArgumentException if URL is invalid
     91     */
     92    private static Bounds parseHashURLs(String url) throws IllegalArgumentException {
    9093        int startIndex = url.indexOf("#map=");
    9194        if (startIndex == -1) return null;
    9295        int endIndex = url.indexOf('&', startIndex);
    9396        if (endIndex == -1) endIndex = url.length();
    94         try {
    95             String coordPart = url.substring(startIndex+5, endIndex);
    96             String[] parts = coordPart.split("/");
    97             Bounds b = positionToBounds(Double.parseDouble(parts[1]),
    98                     Double.parseDouble(parts[2]),
    99                     Integer.parseInt(parts[0]));
    100             return b;
    101         } catch (Exception ex) {
    102             Main.debug(ex.getMessage());
    103             return null;
    104         }
     97        String coordPart = url.substring(startIndex+5, endIndex);
     98        String[] parts = coordPart.split("/");
     99        if (parts.length < 3) {
     100            throw new IllegalArgumentException(tr("URL does not contain {0}/{1}/{2}", tr("zoom"), tr("latitude"), tr("longitude")));
     101        }
     102        int zoom;
     103        double lat, lon;
     104        try {
     105            zoom = Integer.parseInt(parts[0]);
     106        } catch (NumberFormatException e) {
     107            throw new IllegalArgumentException(tr("URL does not contain valid {0}", tr("zoom")), e);
     108        }
     109        try {
     110            lat = Double.parseDouble(parts[1]);
     111        } catch (NumberFormatException e) {
     112            throw new IllegalArgumentException(tr("URL does not contain valid {0}", tr("latitude")), e);
     113        }
     114        try {
     115            lon = Double.parseDouble(parts[2]);
     116        } catch (NumberFormatException e) {
     117            throw new IllegalArgumentException(tr("URL does not contain valid {0}", tr("longitude")), e);
     118        }
     119        return positionToBounds(lat, lon, zoom);
    105120    }
    106121
Note: See TracChangeset for help on using the changeset viewer.