Changeset 18380 in josm


Ignore:
Timestamp:
2022-02-15T18:12:49+01:00 (2 years ago)
Author:
taylor.smock
Message:

ImageryPatterns: Fix CID 1469640: Null point dereferences

This uses an Optional.ofNullable, and if the returned API key is null, we throw
an IOException, which has better troubleshooting text.

see #21844

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryPatterns.java

    r18378 r18380  
    99import java.util.Map;
    1010import java.util.Objects;
     11import java.util.Optional;
    1112import java.util.regex.Matcher;
    1213import java.util.regex.Pattern;
    1314
    1415import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
     16import org.openstreetmap.josm.spi.preferences.Config;
    1517
    1618/**
     
    9496        if (id != null && url != null) {
    9597            final Matcher matcher = PATTERN_API_KEY.matcher(url);
    96             if (matcher.matches()) {
     98            if (matcher.find()) {
    9799                try {
    98                     final String apiKey = FeatureAdapter.retrieveApiKey(id);
    99                     return matcher.replaceAll(apiKey);
    100                 } catch (IOException | NullPointerException e) {
     100                    return Optional.ofNullable(FeatureAdapter.retrieveApiKey(id))
     101                            .map(matcher::replaceAll)
     102                            /* None of the configured API key sites had an API key for the id. */
     103                            .orElseThrow(() -> {
     104                                // Give a more complete error message so that users can fix the problem without
     105                                // opening a bug report. Hopefully.
     106                                final String message;
     107                                if (Config.getPref().getKeySet().contains("apikey.sites")) {
     108                                    message = tr("Advanced preference ''{0}'' is not default. Please consider resetting it.", "apikey.sites");
     109                                } else {
     110                                    message = tr("API key for imagery with id={0} may not be available.", id);
     111                                }
     112                                return new IOException(message);
     113                            });
     114                } catch (IOException e) {
    101115                    // Match rough behavior in JMapViewer TemplatedTMSTileSource, but with better error message.
    102                     throw new IllegalArgumentException(tr("Could not retrieve API key for imagery with id={0}. Cannot add layer.", id), e);
     116                    throw new IllegalArgumentException(tr("Could not retrieve API key for imagery with id={0}. Cannot add layer.\n{1}",
     117                            id, e.getMessage()), e);
    103118                }
    104119            }
Note: See TracChangeset for help on using the changeset viewer.