Skip to content

Commit c243b86

Browse files
authored
Merge pull request #1177 from microsoft/littleaj/lgtm_fixes
Fix bugs from static analysis
2 parents 58b944e + 8af89c3 commit c243b86

File tree

14 files changed

+56
-56
lines changed

14 files changed

+56
-56
lines changed

agent/src/main/java/com/microsoft/applicationinsights/agent/internal/config/builder/XmlAgentConfigurationBuilder.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,20 +239,22 @@ private void addMethods(ClassInstrumentationData classData, Element classNode) {
239239

240240
private Element getTopTag(File configurationFile) throws ParserConfigurationException, IOException, SAXException {
241241
DocumentBuilder builder = createDocumentBuilder();
242-
Document doc = builder.parse(new FileInputStream(configurationFile));
243-
doc.getDocumentElement().normalize();
242+
try (final FileInputStream fis = new FileInputStream(configurationFile)) {
243+
Document doc = builder.parse(fis);
244+
doc.getDocumentElement().normalize();
244245

245-
NodeList topTags = doc.getElementsByTagName(MAIN_TAG);
246-
if (topTags == null || topTags.getLength() == 0) {
247-
return null;
248-
}
246+
NodeList topTags = doc.getElementsByTagName(MAIN_TAG);
247+
if (topTags == null || topTags.getLength() == 0) {
248+
return null;
249+
}
249250

250-
Node topNodeTag = topTags.item(0);
251-
if (topNodeTag.getNodeType() != Node.ELEMENT_NODE) {
252-
return null;
253-
}
251+
Node topNodeTag = topTags.item(0);
252+
if (topNodeTag.getNodeType() != Node.ELEMENT_NODE) {
253+
return null;
254+
}
254255

255-
return (Element) topNodeTag;
256+
return (Element) topNodeTag;
257+
}
256258
}
257259

258260
private DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {

azure-application-insights-spring-boot-starter/src/main/java/com/microsoft/applicationinsights/autoconfigure/HeartBeatProvider/SpringBootHeartBeatProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public Boolean call() {
8888
}
8989
}
9090
catch (Exception e) {
91-
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property, stack trace"
92-
+ "is: %s", ExceptionUtils.getStackTrace(e));
91+
if (InternalLogger.INSTANCE.isWarnEnabled()) {
92+
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property: %s", ExceptionUtils.getStackTrace(e));
93+
}
9394
}
9495
}
9596
return hasSetValues;

core/src/main/java/com/microsoft/applicationinsights/TelemetryClient.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,16 +520,19 @@ private TelemetryContext createInitializedContext() {
520520
ctx.getCloud().setRole(roleName);
521521
}
522522
for (ContextInitializer init : configuration.getContextInitializers()) {
523+
if (init == null) { // since collection reference is exposed, we need a null check here
524+
InternalLogger.INSTANCE.warn("Found null ContextInitializer in configuration. Skipping...");
525+
continue;
526+
}
527+
523528
try {
524529
init.initialize(ctx);
525530
} catch (ThreadDeath td) {
526531
throw td;
527532
} catch (Throwable t) {
528533
try {
529534
if (InternalLogger.INSTANCE.isErrorEnabled()) {
530-
InternalLogger.INSTANCE.error("Exception in context initializer%s: %s",
531-
init == null ? " (context initializer is null)" : ", "+init.getClass().getSimpleName(),
532-
ExceptionUtils.getStackTrace(t));
535+
InternalLogger.INSTANCE.error("Exception in context initializer, %s: %s", init.getClass().getSimpleName(), ExceptionUtils.getStackTrace(t));
533536
}
534537
} catch (ThreadDeath td) {
535538
throw td;

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/SenderThreadLocalBackOffData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ final class SenderThreadLocalBackOffData {
5353
* @param addMilliseconds The amount of seconds that will be added to the 'large' intervals to distinct between sender threads.
5454
*/
5555
public SenderThreadLocalBackOffData(long[] backOffTimeoutsInMillis, long addMilliseconds) {
56-
Preconditions.checkNotNull(backOffTimeoutsInMillis, "backOffTimeoutsInSeconds must be not null");
57-
Preconditions.checkArgument(backOffTimeoutsInMillis.length > 0, "backOffTimeoutsInSeconds must not be empty");
58-
Preconditions.checkArgument(addMilliseconds >= 0, "addSeconds must not be >= 0");
56+
Preconditions.checkNotNull(backOffTimeoutsInMillis, "backOffTimeoutsInMillis must be not null");
57+
Preconditions.checkArgument(backOffTimeoutsInMillis.length > 0, "backOffTimeoutsInMillis must not be empty");
58+
Preconditions.checkArgument(addMilliseconds >= 0, "addMilliseconds must not be >= 0");
5959

6060
currentBackOffIndex = -1;
6161
instanceIsActive = true;

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/SenderThreadsBackOffManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public synchronized void stopAllSendersBackOffActivities() {
8383

8484
@Override
8585
protected SenderThreadLocalBackOffData initialValue() {
86-
int addSeconds = threadsSecondsDifference.incrementAndGet();
87-
senderThreadLocalData = new SenderThreadLocalBackOffData(backOffTimeoutsInMilliseconds, addSeconds * 1000);
86+
senderThreadLocalData = new SenderThreadLocalBackOffData(backOffTimeoutsInMilliseconds, threadsSecondsDifference.incrementAndGet() * 1000L);
8887
registerSenderData(senderThreadLocalData);
8988
return senderThreadLocalData;
9089
}

core/src/main/java/com/microsoft/applicationinsights/internal/channel/common/TransmissionFileSystemOutput.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public TransmissionFileSystemOutput(String folderPath, String maxTransmissionSto
109109
DEFAULT_CAPACITY_MEGABYTES,
110110
MAX_TRANSMISSION_STORAGE_CAPACITY_NAME,
111111
maxTransmissionStorageCapacity);
112-
capacityInBytes = capacityEnforcer.getCurrentValue() * 1024 * 1024;
112+
capacityInBytes = capacityEnforcer.getCurrentValue() * 1024L * 1024L;
113113

114114
folder = new File(folderPath);
115115

@@ -212,7 +212,7 @@ public Transmission fetchOldestFile() {
212212
}
213213

214214
public void setCapacity(int suggestedCapacity) {
215-
this.capacityInBytes = capacityEnforcer.normalizeValue(suggestedCapacity) * 1024 * 1024;
215+
this.capacityInBytes = capacityEnforcer.normalizeValue(suggestedCapacity) * 1024L * 1024L;
216216
}
217217

218218
private List<File> sortOldestLastAndTrim(Collection<File> transmissions, int limit) {

core/src/main/java/com/microsoft/applicationinsights/internal/channel/sampling/FixedRateTelemetrySampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void setSamplingPercentage(Double samplingPercentage) {
8989

9090
@Override
9191
public boolean isSampledIn(Telemetry telemetry) {
92-
Double currentSamplingPercentage = samplingPercentage.get();
92+
double currentSamplingPercentage = samplingPercentage.get();
9393

9494
if (currentSamplingPercentage < 100.0 - 1.0E-12) {
9595
if (telemetry instanceof SupportSampling) {

core/src/main/java/com/microsoft/applicationinsights/internal/heartbeat/DefaultHeartBeatPropertyProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ public Boolean call() {
9797
}
9898
}
9999
catch (Exception e) {
100-
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property, stack trace"
101-
+ "is: %s", ExceptionUtils.getStackTrace(e));
100+
if (InternalLogger.INSTANCE.isWarnEnabled()) {
101+
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property: %s", ExceptionUtils.getStackTrace(e));
102+
}
102103
}
103104
}
104105
return hasSetValues;

core/src/main/java/com/microsoft/applicationinsights/internal/heartbeat/WebAppsHeartbeatProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ public Boolean call() {
107107
}
108108
}
109109
catch (Exception e) {
110-
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property, stack trace"
111-
+ "is: %s", ExceptionUtils.getStackTrace(e));
110+
if (InternalLogger.INSTANCE.isWarnEnabled()) {
111+
InternalLogger.INSTANCE.warn("Failed to obtain heartbeat property: %s", ExceptionUtils.getStackTrace(e));
112+
}
112113
}
113114
}
114115
return hasSetValues;

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/JniPCConnector.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,10 @@ private static void extractToLocalFolder(File dllOnDisk, String libraryToLoad) t
209209

210210
InternalLogger.INSTANCE.trace("Successfully extracted '%s' to local folder", libraryToLoad);
211211
} finally {
212-
if (in != null) {
213-
try {
214-
in.close();
215-
} catch (IOException e) {
216-
InternalLogger.INSTANCE.error("Failed to close input stream for dll extraction: %s", e.toString());
217-
}
212+
try {
213+
in.close();
214+
} catch (IOException e) {
215+
InternalLogger.INSTANCE.error("Failed to close input stream for dll extraction: %s", e.toString());
218216
}
219217
if (out != null) {
220218
try {

0 commit comments

Comments
 (0)