Skip to content

Commit 500e28c

Browse files
authored
Add null guards (#4542)
While the `@NotNull` decorator informs developers that the correct usage is that the decorated variables should not be `null`, it does not enforce such in any way. This leads to cases where an incorrectly fed `null` could propagate far away before being detected (such as via `NullPointerException`s). This commit implements some `null` guards where needed for a fail-fast treatment of `null` inputs.
1 parent bbaef9d commit 500e28c

File tree

7 files changed

+92
-5
lines changed

7 files changed

+92
-5
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/OpenGrokThreadFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import java.util.concurrent.Executors;
2828
import java.util.concurrent.ThreadFactory;
29+
import java.util.Objects;
2930

3031
/**
3132
* ThreadFactory to be used throughout OpenGrok to make sure all threads have common prefix.
@@ -45,7 +46,7 @@ public OpenGrokThreadFactory(String name) {
4546

4647
@Override
4748
public Thread newThread(@NotNull Runnable runnable) {
48-
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
49+
Thread thread = Executors.defaultThreadFactory().newThread(Objects.requireNonNull(runnable, "runnable"));
4950
thread.setName(PREFIX + threadPrefix + thread.getId());
5051
return thread;
5152
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.Reader;
2727
import java.io.StringReader;
2828
import java.util.List;
29+
import java.util.Objects;
2930

3031
import org.jetbrains.annotations.NotNull;
3132
import org.opengrok.indexer.util.IOUtils;
@@ -47,7 +48,7 @@ public int read(char @NotNull [] cbuf, int off, int len) throws IOException {
4748
if (input == null) {
4849
input = createInternalReader();
4950
}
50-
return input.read(cbuf, off, len);
51+
return input.read(Objects.requireNonNull(cbuf, "cbuf"), off, len);
5152
}
5253

5354
@Override

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexCheck.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.HashSet;
3737
import java.util.List;
3838
import java.util.Map;
39+
import java.util.Objects;
3940
import java.util.Set;
4041
import java.util.concurrent.ConcurrentHashMap;
4142
import java.util.concurrent.ExecutionException;
@@ -101,7 +102,7 @@ public enum IndexCheckMode {
101102
* @param configuration configuration based on which to perform the check
102103
*/
103104
public IndexCheck(@NotNull Configuration configuration) {
104-
this(configuration, null);
105+
this(configuration, null); // configuration is guarded against null inside this constructor
105106
}
106107

107108
/**
@@ -111,7 +112,7 @@ public IndexCheck(@NotNull Configuration configuration) {
111112
* on whether projects are enabled in the configuration.
112113
*/
113114
public IndexCheck(@NotNull Configuration configuration, Collection<String> projectNames) {
114-
this.configuration = configuration;
115+
this.configuration = Objects.requireNonNull(configuration, "configuration");
115116
if (projectNames != null) {
116117
this.projectNames.addAll(projectNames);
117118
}

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IndexDatabase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ private static class CountingWriter extends Writer {
21242124

21252125
@Override
21262126
public void write(@NotNull char[] chars, int off, int len) throws IOException {
2127-
out.write(chars, off, len);
2127+
out.write(Objects.requireNonNull(chars), off, len);
21282128
count += len;
21292129
}
21302130

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2024, Heewon Lee <[email protected]>.
23+
*/
24+
package org.opengrok.indexer.configuration;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
29+
30+
public class OpenGrokThreadFactoryTest {
31+
@Test
32+
void testNullRunnable() throws Exception {
33+
assertThrows(NullPointerException.class, () -> {
34+
OpenGrokThreadFactory factory = new OpenGrokThreadFactory("");
35+
factory.newThread(null);
36+
});
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2024, Heewon Lee <[email protected]>.
23+
*/
24+
package org.opengrok.indexer.history;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
29+
30+
public class HistoryReaderTest {
31+
@Test
32+
void testNullCbuf() throws Exception {
33+
assertThrows(NullPointerException.class, () -> {
34+
HistoryReader historyReader = new HistoryReader(new History());
35+
historyReader.read(null, 0, 0);
36+
});
37+
}
38+
}

opengrok-indexer/src/test/java/org/opengrok/indexer/index/IndexCheckTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,12 @@ void testMultipleTestsWithSameInstance() throws Exception {
255255
}
256256
}
257257
}
258+
259+
@Test
260+
void testNullConfiguration() throws Exception {
261+
assertThrows(NullPointerException.class, () -> {
262+
new IndexCheck(null);
263+
}
264+
);
265+
}
258266
}

0 commit comments

Comments
 (0)