Skip to content

Commit 2173ed7

Browse files
idodeclareVladimir Kotal
authored andcommitted
Squash YYYY and some other Sonar-identified bugs
Noncompliant Code Example --- Date date = new SimpleDateFormat("yyyy/MM/dd").parse("2015/12/31"); String result = new SimpleDateFormat("YYYY/MM/dd").format(date); //Noncompliant; result is '2016/12/31'
1 parent f8b14b4 commit 2173ed7

File tree

22 files changed

+84
-60
lines changed

22 files changed

+84
-60
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/CompatibleAnalyser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.analysis;
2525

@@ -49,7 +49,9 @@ protected TokenStreamComponents createComponents(String fieldName) {
4949
case QueryBuilder.PROJECT:
5050
return new TokenStreamComponents(new PathTokenizer());
5151
case QueryBuilder.HIST:
52-
return new HistoryAnalyzer().createComponents(fieldName);
52+
try (HistoryAnalyzer historyAnalyzer = new HistoryAnalyzer()) {
53+
return historyAnalyzer.createComponents(fieldName);
54+
}
5355
default:
5456
return new TokenStreamComponents(createPlainFullTokenizer());
5557
}

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/FileAnalyzer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ protected TokenStreamComponents createComponents(String fieldName) {
183183
case QueryBuilder.PROJECT:
184184
return new TokenStreamComponents(new PathTokenizer());
185185
case QueryBuilder.HIST:
186-
return new HistoryAnalyzer().createComponents(fieldName);
186+
try (HistoryAnalyzer historyAnalyzer = new HistoryAnalyzer()) {
187+
return historyAnalyzer.createComponents(fieldName);
188+
}
187189
//below is set by PlainAnalyzer to workaround #1376 symbols search works like full text search
188190
case QueryBuilder.REFS: {
189191
return new TokenStreamComponents(symbolTokenizerFactory.get());

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/HistoryAnalyzer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.analysis;
2525

@@ -55,9 +55,11 @@ public HistoryAnalyzer() {
5555
}
5656

5757
/**
58-
* Filters LowerCaseTokenizer with StopFilter.
58+
* Creates components using a new {@link PlainFullTokenizer}, filtered using
59+
* a default set of English stop-words.
5960
* @param fieldName name of field for which to create components
60-
* @return components for this analyzer
61+
* @return components for this analyzer (NB safe to use even if this
62+
* analyzer were to be garbage-collected)
6163
*/
6264
@Override
6365
protected TokenStreamComponents createComponents(String fieldName) {

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/document/DocumentMatcher.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, Chris Fraire <[email protected]>.
21+
* Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2222
*/
2323
package org.opengrok.indexer.analysis.document;
2424

@@ -124,7 +124,10 @@ public AnalyzerFactory isMagic(byte[] contents, InputStream in)
124124
return null;
125125
}
126126
if (bomLength > 0) {
127-
in.skip(bomLength);
127+
if (in.skip(bomLength) != bomLength) {
128+
in.reset();
129+
return null;
130+
}
128131
}
129132

130133
// read line-by-line for a first few lines

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/executables/JavaClassAnalyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void analyze(Document doc, InputStream in, Writer xrefOut,
143143
try {
144144
xrefOut.flush();
145145
} catch (IOException ex) {
146-
LOGGER.log(Level.WARNING, "Couldn't flush xref, will retry once added to doc", ex);
146+
LOGGER.log(Level.WARNING, "Couldn''t flush. Will retry once added to doc", ex);
147147
}
148148
}
149149

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/executables/JavaClassAnalyzerFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
2323
*/
2424

2525
package org.opengrok.indexer.analysis.executables;
@@ -72,8 +72,8 @@ public AnalyzerFactory isMagic(byte[] content, InputStream in) {
7272
}
7373
}
7474
// Require known major_version number.
75-
int majorVersion = (content[MAJOR_VER_HIGHBYTE] << 1) |
76-
content[MAJOR_VER_LOWBYTE];
75+
int majorVersion = ((content[MAJOR_VER_HIGHBYTE] & 0xff) << 1) |
76+
(content[MAJOR_VER_LOWBYTE] & 0xff);
7777
if (majorVersion >= JDK1_1_MAJOR_VER && majorVersion <=
7878
JAVA_SE_9_MAJOR_VER) {
7979
return JavaClassAnalyzerFactory.DEFAULT_INSTANCE;

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/plain/PlainAnalyzer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ protected Reader getReader(InputStream stream) throws IOException {
110110
public void analyze(Document doc, StreamSource src, Writer xrefOut)
111111
throws IOException, InterruptedException {
112112
Definitions defs = null;
113+
NullWriter nullWriter = null;
113114

114115
doc.add(new OGKTextField(QueryBuilder.FULL,
115116
getReader(src.getStream())));
@@ -138,7 +139,8 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut)
138139
* turned off we still need to run writeXref to produce scopes,
139140
* we use a dummy writer that will throw away any xref output.
140141
*/
141-
xrefOut = new NullWriter();
142+
nullWriter = new NullWriter();
143+
xrefOut = nullWriter;
142144
}
143145

144146
if (xrefOut != null) {
@@ -157,6 +159,10 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut)
157159

158160
addNumLines(doc, xref.getLineNumber());
159161
addLOC(doc, xref.getLOC());
162+
} finally {
163+
if (nullWriter != null) {
164+
nullWriter.close();
165+
}
160166
}
161167
}
162168
}

opengrok-indexer/src/main/java/org/opengrok/indexer/framework/PluginClassLoader.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.framework;
2425

@@ -131,16 +132,16 @@ private Class loadClassFromFile(String classname) throws ClassNotFoundException
131132
});
132133
return c;
133134
}
134-
} catch (IOException e) {
135-
throw new ClassNotFoundException(e.toString(), e);
136135
} catch (Throwable e) {
137136
throw new ClassNotFoundException(e.toString(), e);
138137
}
139138
}
140139

141140
private byte[] loadBytes(InputStream in) throws IOException {
142141
byte[] bytes = new byte[in.available()];
143-
in.read(bytes);
142+
if (in.read(bytes) != bytes.length) {
143+
throw new IOException("unexpected truncated read");
144+
}
144145
return bytes;
145146
}
146147

@@ -254,7 +255,7 @@ public Class loadClass(String name, boolean resolveIt) throws ClassNotFoundExcep
254255
}
255256
return c;
256257
}
257-
} catch (ClassNotFoundException ex) {
258+
} catch (ClassNotFoundException ignored) {
258259
}
259260
}
260261

@@ -268,7 +269,7 @@ public Class loadClass(String name, boolean resolveIt) throws ClassNotFoundExcep
268269
}
269270
return c;
270271
}
271-
} catch (ClassNotFoundException ex) {
272+
} catch (ClassNotFoundException ignored) {
272273
}
273274

274275
try {
@@ -281,7 +282,7 @@ public Class loadClass(String name, boolean resolveIt) throws ClassNotFoundExcep
281282
}
282283
return c;
283284
}
284-
} catch (ClassNotFoundException ex) {
285+
} catch (ClassNotFoundException ignored) {
285286
}
286287

287288
throw new ClassNotFoundException("Class \"" + name + "\" was not found");

opengrok-indexer/src/main/java/org/opengrok/indexer/history/Annotation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/*
2121
* Copyright (c) 2007, 2019 Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2019, Krystof Tulinger <[email protected]>.
23+
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2324
*/
2425

2526
package org.opengrok.indexer.history;
@@ -233,7 +234,8 @@ private Map<String, String> generateColors() {
233234
revisions.forEach(revision -> {
234235
final int lineVersion = getRevisions().size() - getFileVersion(revision);
235236
final double bucketTotal = colorsPerBucket * lineVersion;
236-
final int bucketIndex = (int) Math.max(Math.min(Math.floor(bucketTotal), nColors - 1), 0);
237+
final int bucketIndex = (int) Math.max(
238+
Math.min(Math.floor(bucketTotal), nColors - 1.0), 0);
237239
Color color = colors.get(bucketIndex);
238240
colorMap.put(revision, String.format("rgb(%d, %d, %d)", color.red, color.green, color.blue));
239241
});

opengrok-indexer/src/main/java/org/opengrok/indexer/history/CVSRepository.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

@@ -102,17 +102,17 @@ public void setDirectoryName(File directory) {
102102
try {
103103
root = input.readLine();
104104
} catch (java.io.IOException e) {
105-
LOGGER.log(Level.WARNING, "failed to load: {0}", e);
105+
LOGGER.log(Level.WARNING, "failed to load", e);
106106
return;
107107
} finally {
108108
try {
109109
input.close();
110110
} catch (java.io.IOException e) {
111-
LOGGER.log(Level.INFO, "failed to close: {0}", e);
111+
LOGGER.log(Level.INFO, "failed to close", e);
112112
}
113113
}
114114
} catch (java.io.FileNotFoundException e) {
115-
LOGGER.log(Level.FINE, "not loading CVS Root file: {0}", e);
115+
LOGGER.log(Level.FINE, "not loading CVS Root file", e);
116116
return;
117117
}
118118

0 commit comments

Comments
 (0)