You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
During the use of Guava, I have identified several potential NPEs that are not explicitly listed in the method signatures.
Personally, I believe that exceptions that are not listed in the throws during usage can be considered unexpected behavior, where I don't know how guava's maintainers perceive this. If all the unhandled NullPointerExceptions in this issue are considered expected, then I can close this issue.
public static List readLines(URL url, Charset charset) throws IOException {
// don't use asCharSource(url, charset).readLines() because that returns
// an immutable list, which would change the behavior of this method
return readLines(
url,
charset,
new LineProcessor<List>() {
final List result = Lists.newArrayList();
@Override
public boolean processLine(String line) {
result.add(line);
return true;
}
@Override
public List<String> getResult() {
return result;
}
});
When the first argument in array is null, it throws NPE.
show code
/** * Returns the greatest value present in {@code array}. * * @param array a <i>nonempty</i> array of {@code short} values * @return the value present in {@code array} that is greater than or equal to every other value * in the array * @throws IllegalArgumentException if {@code array} is empty */@GwtIncompatible(
"Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.")
publicstaticshortmax(short... array) {
checkArgument(array.length > 0);
shortmax = array[0];
for (inti = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
returnmax;
}
@BetapublicstaticvoidflushQuietly(Flushableflushable) {
try {
flush(flushable, true);
} catch (IOExceptione) {
logger.log(Level.SEVERE, "IOException should not have been thrown.", e);
}
}
publicstaticbooleanisIsatapAddress(Inet6Addressip) {
// If it's a Teredo address with the right port (41217, or 0xa101)// which would be encoded as 0x5efe then it can't be an ISATAP address.if (isTeredoAddress(ip)) {
returnfalse;
}
byte[] bytes = ip.getAddress();
if ((bytes[8] | (byte) 0x03) != (byte) 0x03) {
// Verify that high byte of the 64 bit identifier is zero, modulo// the U/L and G bits, with which we are not concerned.returnfalse;
}
return (bytes[9] == (byte) 0x00) && (bytes[10] == (byte) 0x5e) && (bytes[11] == (byte) 0xfe);
}
publicstaticvoidcreateParentDirectories(Pathpath, FileAttribute<?>... attrs)
throwsIOException {
// Interestingly, unlike File.getCanonicalFile(), Path/Files provides no way of getting the// canonical (absolute, normalized, symlinks resolved, etc.) form of a path to a nonexistent// file. getCanonicalFile() can at least get the canonical form of the part of the path which// actually exists and then append the normalized remainder of the path to that.PathnormalizedAbsolutePath = path.toAbsolutePath().normalize();
Pathparent = normalizedAbsolutePath.getParent();
if (parent == null) {
// The given directory is a filesystem root. All zero of its ancestors exist. This doesn't// mean that the root itself exists -- consider x:\ on a Windows machine without such a// drive -- or even that the caller can create it, but this method makes no such guarantees// even for non-root files.return;
}
// Check if the parent is a directory first because createDirectories will fail if the parent// exists and is a symlink to a directory... we'd like for this to succeed in that case.// (I'm kind of surprised that createDirectories would fail in that case; doesn't seem like// what you'd want to happen.)if (!Files.isDirectory(parent)) {
Files.createDirectories(parent, attrs);
if (!Files.isDirectory(parent)) {
thrownewIOException("Unable to create parent directories of " + path);
}
}
}
Expected Behavior
I think some of them are not supposed to throw NPE and others need append NullPointerException to throws at least.
Description
During the use of Guava, I have identified several potential NPEs that are not explicitly listed in the method signatures.
Personally, I believe that exceptions that are not listed in the
throws
during usage can be considered unexpected behavior, where I don't know how guava's maintainers perceive this. If all the unhandled NullPointerExceptions in this issue are considered expected, then I can close this issue.Example
1. com.google.common.io.Resources::readLines(URL, Charset)
When charset is null, it throws NPE.
show code
public static List readLines(URL url, Charset charset) throws IOException {
// don't use asCharSource(url, charset).readLines() because that returns
// an immutable list, which would change the behavior of this method
return readLines(
url,
charset,
new LineProcessor<List>() {
final List result = Lists.newArrayList();
}
2. com.google.common.primitives.Shorts::contains(short[], short)
When array is null, it throws NPE.
show code
3. com.google.common.primitives.Longs::contains(long[], long)
The same as 2.
4. com.google.common.primitives.Booleans::contains(boolean[], boolean)
The same as 2.
5. com.google.common.primitives.Ints::contains(int[], int)
When array is null, it throws NPE.
show code
6. com.google.common.primitives.Floats::contains(float[], float)
The same as 5.
7. com.google.common.primitives.Bytes::contains(byte[], byte)
The same as 5.
8. com.google.common.primitives.Doubles::contains(double[], double)
The same as 5.
9. com.google.common.primitives.Shorts::max(short...)
When the first argument in array is null, it throws NPE.
show code
10. com.google.common.net.InetAddresses::toUriString(InetAddress)
When ip is null, it throws NPE.
show code
11. com.google.common.net.InternetDomainName::isValid(String)
When name is null, it throws NPE.
show code
12. com.google.common.primitives.Longs::concat(long[]...)
When arrays is null, it throws NPE.
show code
13. com.google.common.primitives.ImmutableLongArray::copyOf(Iterable<Long>)
When values is null, it throws NPE.
show code
14. com.google.common.primitives.ImmutableDoubleArray::copyOf(Iterable<Double>)
The same as 13.
15. com.google.common.primitives.ImmutableIntArray::copyOf(Iterable<Integer>)
The same as 13.
16. com.google.common.net.InetAddresses::getCoercedIPv4Address(InetAddress)
When ip is null, it throws NPE.
show code
17. com.google.common.io.Flushables::flushQuietly(Flushable)
When flushable is null, it throws NPE.
show code
18. com.google.common.primitives.Chars::concat(char[]...)
The same as 12.
19. com.google.common.primitives.Booleans::concat(boolean[]...)
The same as 12.
20. com.google.common.primitives.Floats::concat(float[]...)
The same as 12.
21. com.google.common.primitives.Bytes::concat(byte[]...)
The same as 12.
22. com.google.common.net.InetAddresses::isIsatapAddress(Inet6Address)
When ip is null, it throws NPE.
show code
23. com.google.common.io.MoreFiles::listFiles(Path)
When dir is null, it throws NPE.
show code
24. com.google.common.io.MoreFiles::createParentDirectories(Path, FileAttribute<?>...)
When path is null, it throws NPE.
show code
Expected Behavior
I think some of them are not supposed to throw NPE and others need append
NullPointerException
tothrows
at least.Actual Behavior
They all throws java.lang.NullPointerException.
Packages
com.google.common.io, com.google.common.net, com.google.common.primitives
Platforms
No response
Checklist
The text was updated successfully, but these errors were encountered: