Ignore:
Timestamp:
2023-02-16T16:01:49+01:00 (23 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_]*$

File:
1 edited

Legend:

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

    r18651 r18665  
    1111import java.util.Base64;
    1212import java.util.Objects;
     13import java.util.Optional;
    1314import java.util.concurrent.CountDownLatch;
    1415import java.util.concurrent.TimeUnit;
     
    215216        }
    216217        CountDownLatch done = new CountDownLatch(1);
    217         Consumer<IOAuthToken> consumer = authToken -> {
     218        Consumer<Optional<IOAuthToken>> consumer = authToken -> {
    218219                    if (!remoteControlIsRunning) {
    219220                        RemoteControl.stop();
     
    221222                    // Clean up old token/password
    222223                    OAuthAccessTokenHolder.getInstance().setAccessToken(null);
    223                     OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), authToken);
     224                    OAuthAccessTokenHolder.getInstance().setAccessToken(OsmApi.getOsmApi().getServerUrl(), authToken.orElse(null));
    224225                    OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());
    225226                    done.countDown();
     
    229230                OsmScopes.read_prefs, OsmScopes.write_prefs,
    230231                OsmScopes.write_api, OsmScopes.write_notes);
    231         synchronized (done) {
    232             // Only wait at most 5 minutes
    233             int counter = 0;
    234             while (done.getCount() >= 0 && counter < 5) {
    235                 try {
    236                     if (done.await(1, TimeUnit.MINUTES)) {
    237                         break;
    238                     }
    239                 } catch (InterruptedException e) {
    240                     Thread.currentThread().interrupt();
    241                     Logging.trace(e);
    242                     consumer.accept(null);
    243                     throw new MissingOAuthAccessTokenException(e);
     232        // Only wait at most 5 minutes
     233        int counter = 0;
     234        while (done.getCount() >= 0 && counter < 5) {
     235            try {
     236                if (done.await(1, TimeUnit.MINUTES)) {
     237                    break;
    244238                }
    245                 counter++;
    246             }
     239            } catch (InterruptedException e) {
     240                Thread.currentThread().interrupt();
     241                Logging.trace(e);
     242                consumer.accept(null);
     243                throw new MissingOAuthAccessTokenException(e);
     244            }
     245            counter++;
    247246        }
    248247    }
Note: See TracChangeset for help on using the changeset viewer.