Ignore:
Timestamp:
2022-05-24T21:16:34+02:00 (2 years ago)
Author:
taylor.smock
Message:

utilsplugin2: Fix #22087: NPE in ChildrenMatch#init

This is caused by JOSM passing a null PushBackTokenizer to factories.
This also fixes an issue where an NPE could occur when utilsplugin2 returns
null from that same method.

Location:
applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/search
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/search/RangeMatch.java

    r35436 r35970  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.plugins.utilsplugin2.search;
     3
     4import java.util.Objects;
    35
    46import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    79
    810/**
     11 * Matches objects with properties in a certain range.
    912 * TODO: remove this copied class and make it public in JOSM core
    1013 */
    11 public abstract class RangeMatch extends SearchCompiler.Match {
     14abstract class RangeMatch extends SearchCompiler.Match {
    1215
    1316    private final long min;
    1417    private final long max;
    1518
    16     public RangeMatch(long min, long max) {
     19    RangeMatch(long min, long max) {
    1720        this.min = Math.min(min, max);
    1821        this.max = Math.max(min, max);
    1922    }
    2023
    21     public RangeMatch(PushbackTokenizer.Range range) {
     24    RangeMatch(PushbackTokenizer.Range range) {
    2225        this(range.getStart(), range.getEnd());
    2326    }
     
    3841    @Override
    3942    public String toString() {
    40         return getString() + "=" + min + "-" + max;
     43        return getString() + '=' + min + '-' + max;
    4144    }
    4245
    4346    @Override
    4447    public int hashCode() {
    45         final int prime = 31;
    46         int result = 1;
    47         result = prime * result + (int) (max ^ (max >>> 32));
    48         result = prime * result + (int) (min ^ (min >>> 32));
    49         return result;
     48        return Objects.hash(max, min);
    5049    }
    5150
     
    5756            return false;
    5857        RangeMatch other = (RangeMatch) obj;
    59         if (max != other.max)
    60             return false;
    61         if (min != other.min)
    62             return false;
    63         return true;
     58        return max == other.max
     59                && min == other.min;
    6460    }
    6561}
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/search/UtilsSimpleMatchFactory.java

    r35436 r35970  
    22package org.openstreetmap.josm.plugins.utilsplugin2.search;
    33
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
    46import java.util.Arrays;
    57import java.util.Collection;
     8import java.util.Collections;
    69
    710import org.openstreetmap.josm.data.osm.search.PushbackTokenizer;
     
    1619public class UtilsSimpleMatchFactory implements SimpleMatchFactory {
    1720
    18     private static Collection<String> keywords = Arrays.asList("usedinways", "usedinrelations", "parents", "children");
     21    private static final Collection<String> keywords =
     22            Collections.unmodifiableCollection(Arrays.asList("usedinways", "usedinrelations", "parents", "children"));
    1923
    2024    @Override
     
    2630    public SearchCompiler.Match get(String keyword, boolean caseSensitive, boolean regexSearch, PushbackTokenizer tokenizer)
    2731            throws SearchParseError {
    28         if ("usedinways".equals(keyword)) {
     32        if (tokenizer == null) {
     33            throw new SearchParseError("<html>" + tr("Expecting {0} after {1}", "<code>:</code>", "<i>" + keyword + "</i>") + "</html>");
     34        }
     35        switch (keyword) {
     36        case "usedinways":
    2937            return new UsedInWaysMatch(tokenizer);
    30         } else
    31             if ("usedinrelations".equals(keyword)) {
    32                 return new UsedInRelationsMatch(tokenizer);
    33             } else
    34                 if ("parents".equals(keyword)) {
    35                     return new ParentsMatch(tokenizer);
    36                 } else
    37                     if ("children".equals(keyword)) {
    38                         return new ChildrenMatch(tokenizer);
    39                     } else
    40                         return null;
     38        case "usedinrelations":
     39            return new UsedInRelationsMatch(tokenizer);
     40        case "parents":
     41            return new ParentsMatch(tokenizer);
     42        case "children":
     43            return new ChildrenMatch(tokenizer);
     44        default:
     45            throw new IllegalStateException("Not expecting keyword " + keyword);
     46        }
    4147    }
    4248}
Note: See TracChangeset for help on using the changeset viewer.