Ignore:
Timestamp:
2023-02-16T16:01:49+01:00 (17 months ago)
Author:
taylor.smock
Message:

Fix several coverity issues

CID-1504572: Synchronization on java.util.concurrent objects

The lock wasn't necessary after moving from AtomicBoolean to
CountDownLatch

CID-1504570: Dereference null value -- the default getApiUrl return was null
CID-1504570: Explicit null dereferenced

Consumer<IOAuthToken> to Consumer<Optional<IOAuthToken>>

CID-1476014, CID-1476013, CID-1476011: Resource leak

These resource leaks are unlikely to happen outside of plugin code,
and are all related to StringSelection#getTransferData, which may
return a Closeable object, but is unlikely to be the Transferable
for any of the affected classes.


This also fixes some recently introduced SonarLint issues. It also suppresses
squid:S100 ("Method names should comply with a naming convention") for the MapCSS
Functions class, since the convention for that method names in that class is
^[a-zA-Z][a-zA-Z0-9_]*$

Location:
trunk/src/org/openstreetmap/josm/data/oauth
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/oauth/IOAuthAuthorization.java

    r18650 r18665  
    22package org.openstreetmap.josm.data.oauth;
    33
     4import java.util.Optional;
    45import java.util.function.Consumer;
    56
     
    1617     * @param scopes The scopes to ask for
    1718     */
    18     void authorize(IOAuthParameters parameters, Consumer<IOAuthToken> consumer, Enum<?>... scopes);
     19    void authorize(IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer, Enum<?>... scopes);
    1920}
  • trunk/src/org/openstreetmap/josm/data/oauth/IOAuthParameters.java

    r18650 r18665  
    9191    }
    9292
     93    /**
     94     * Store the preferences for these parameters
     95     */
    9396    void rememberPreferences();
    9497}
  • trunk/src/org/openstreetmap/josm/data/oauth/IOAuthToken.java

    r18650 r18665  
    1313     * Sign a client
    1414     * @param client The client to sign
     15     * @throws OAuthException if the OAuth token type is unknown (AKA we do't know how to handle it)
    1516     */
    1617    void sign(HttpClient client) throws OAuthException;
  • trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Authorization.java

    r18662 r18665  
    1313import java.util.Map;
    1414import java.util.Objects;
     15import java.util.Optional;
    1516import java.util.UUID;
    1617import java.util.function.Consumer;
     
    4546
    4647    @Override
    47     public void authorize(IOAuthParameters parameters, Consumer<IOAuthToken> consumer, Enum<?>... scopes) {
     48    public void authorize(IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer, Enum<?>... scopes) {
    4849        final String state = UUID.randomUUID().toString();
    4950        final String codeVerifier = UUID.randomUUID().toString(); // Cryptographically random string (ASCII)
     
    6263        private final String state;
    6364        private final IOAuthParameters parameters;
    64         private final Consumer<IOAuthToken> consumer;
     65        private final Consumer<Optional<IOAuthToken>> consumer;
    6566        private final String codeVerifier;
    6667
    67         OAuth20AuthorizationHandler(String state, String codeVerifier, IOAuthParameters parameters, Consumer<IOAuthToken> consumer) {
     68        OAuth20AuthorizationHandler(String state, String codeVerifier, IOAuthParameters parameters, Consumer<Optional<IOAuthToken>> consumer) {
    6869            this.state = state;
    6970            this.parameters = parameters;
     
    9899                    HttpClient.Response response = tradeCodeForToken.getResponse();
    99100                    OAuth20Token oAuth20Token = new OAuth20Token(parameters, response.getContentReader());
    100                     consumer.accept(oAuth20Token);
     101                    consumer.accept(Optional.of(oAuth20Token));
    101102                } catch (IOException | OAuth20Exception e) {
    102                     consumer.accept(null);
     103                    consumer.accept(Optional.empty());
    103104                    throw new JosmRuntimeException(e);
    104105                } finally {
  • trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Token.java

    r18650 r18665  
    2020import org.openstreetmap.josm.tools.HttpClient;
    2121import org.openstreetmap.josm.tools.JosmRuntimeException;
     22import org.openstreetmap.josm.tools.Utils;
    2223
    2324/**
     
    8182    @Override
    8283    public void sign(HttpClient client) throws OAuthException {
    83         if (!this.oauthParameters.getApiUrl().contains(client.getURL().getHost())) {
     84        if (!Utils.isBlank(this.oauthParameters.getApiUrl())
     85                && !this.oauthParameters.getApiUrl().contains(client.getURL().getHost())) {
    8486            String host = URI.create(this.oauthParameters.getAccessTokenUrl()).getHost();
    8587            throw new IllegalArgumentException("Cannot sign URL with token for different host: Expected " + host
Note: See TracChangeset for help on using the changeset viewer.