Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8270058: Use Objects.check{Index,FromIndexSize} for java.desktop #4718

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -30,6 +30,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Objects;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
@@ -81,8 +82,7 @@ public long getFrameLength() {
public int read(float[] b, int off, int len) throws IOException {
if (b == null)
throw new NullPointerException();
if (off < 0 || len < 0 || len > b.length - off)
throw new IndexOutOfBoundsException();
Objects.checkFromIndexSize(off, len, b.length);
if (pos >= buffer_len)
return -1;
if (len == 0)
@@ -45,6 +45,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleComponent;
@@ -1789,9 +1790,7 @@ public String getToolTipText(MouseEvent event) {
}

private void checkIndex(int index) {
if (index < 0 || index >= pages.size()) {
throw new IndexOutOfBoundsException("Index: "+index+", Tab count: "+pages.size());
}
Objects.checkIndex(index, pages.size());
Copy link
Member

@jayathirthrao jayathirthrao Jul 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not replace IOOBE checks with messages. Its better to keep it old way.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects.checkIndex itself throws exceptions for wrong values. So this should be ok. No?

Copy link
Member

@jayathirthrao jayathirthrao Jul 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have specific message here like "Tab count" which we are not passing and will be helpful for debugging in future.

Copy link
Member Author

@kelthuzadx kelthuzadx Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restored.

}


@@ -29,6 +29,7 @@
*/

import java.io.*;
import java.util.Objects;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
@@ -196,10 +197,7 @@ void setPrinterJob(PrinterJob job, boolean pageable) {

public PageFormat getPageFormat(int pageIndex)
throws IndexOutOfBoundsException {

if (pageIndex < 0 || pageIndex >= getNumberOfPages()) {
throw new IndexOutOfBoundsException();
}
Objects.checkIndex(pageIndex, getNumberOfPages());

PageFormat pf = myPrinterJob.defaultPage();
switch (pageIndex % 2) {
@@ -225,10 +223,8 @@ String getOrientStr(PageFormat pf) {

public Printable getPrintable(int pageIndex)
throws IndexOutOfBoundsException {
Objects.checkIndex(pageIndex, getNumberOfPages());

if (pageIndex < 0 || pageIndex >= getNumberOfPages()) {
throw new IndexOutOfBoundsException();
}
if (pageIndex < 2) {
paintSquares = true;
} else {
@@ -37,6 +37,7 @@
import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Vector;

import javax.imageio.IIOException;
@@ -114,26 +115,23 @@ public int getNumImages(boolean allowSearch) throws IOException {
public int getWidth(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

return 10;
}

public int getHeight(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

return 15;
}

public Iterator getImageTypes(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

Vector imageTypes = new Vector();
imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType
@@ -150,8 +148,7 @@ public IIOMetadata getImageMetadata(int imageIndex)

if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
@@ -165,8 +162,7 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
@@ -32,6 +32,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;
import java.util.Vector;

import javax.imageio.IIOImage;
@@ -87,8 +88,7 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 1 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 1);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
Copy link
Contributor

@prsadhuk prsadhuk Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use checkFromToIndex​ for this IOOBE too? Used in many other places in this PR...

Please also confirm all tests modified are green after this modification..

Copy link
Member Author

@kelthuzadx kelthuzadx Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkFromToIndex​ has different semantice with checkIndex:

  • checkFromToIndex
     * <ul>
     *  <li>{@code fromIndex < 0}</li>
     *  <li>{@code fromIndex > toIndex}</li>
     *  <li>{@code toIndex > length}</li>
     *  <li>{@code length < 0}, which is implied from the former inequalities</li>
     * </ul>
  • checkIndex
     * <ul>
     *  <li>{@code index < 0}</li>
     *  <li>{@code index >= length}</li>
     *  <li>{@code length < 0}, which is implied from the former inequalities</li>
     * </ul>

The following tests are passed after applying this patch:

test/jdk/javax/imageio/AppletResourceTest.java
test/jdk/javax/imageio/ImageReaderReadAll.java
test/jdk/javax/imageio/metadata/IIOMetadataFormat/UserPluginMetadataFormatTest.java

ClippedImages.java is a manual test, I have no environment to test it, but since it's a trivial replacement, I think it's okay..

Copy link
Contributor

@prsadhuk prsadhuk Jul 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant

if (imageIndex < minIndex)
                    throw new IndexOutOfBoundsException();

can't it be replaced with Objects.checkFromToIndex(minIndex, imageIndex, ..

Copy link
Member Author

@kelthuzadx kelthuzadx Jul 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry for the late response. checkFromToIndex has many constraints, this code may not satisfy those constraints, so I don't think we should do that in this PR.(Maybe we can file a new PR to do replacement carefully)

Copy link
Contributor

@prsadhuk prsadhuk Jul 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK...The subject line of this PR states that checkFromToIndex will also be used but I see only checkIndex so I intervened. Anyways, the code in question is in test so not much of an issue.

Copy link
Member Author

@kelthuzadx kelthuzadx Jul 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @prsadhuk for review! I've updated the title to exclude checkFromToIndex.

@@ -101,8 +101,7 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
public Iterator getImageTypes(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 1 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 1);

Vector imageTypes = new Vector();
imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType
@@ -39,6 +39,7 @@
import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
@@ -115,26 +116,23 @@ public int getNumImages(boolean allowSearch) throws IOException {
public int getWidth(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

return 10;
}

public int getHeight(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

return 15;
}

public Iterator getImageTypes(int imageIndex) throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);

Vector imageTypes = new Vector();
imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType
@@ -150,8 +148,7 @@ public IIOMetadata getImageMetadata(int imageIndex) throws IOException {

if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();
@@ -169,8 +166,7 @@ public BufferedImage read(int imageIndex, ImageReadParam param)
throws IOException {
if (input == null)
throw new IllegalStateException();
if (imageIndex >= 5 || imageIndex < 0)
throw new IndexOutOfBoundsException();
Objects.checkIndex(imageIndex, 5);
if (seekForwardOnly) {
if (imageIndex < minIndex)
throw new IndexOutOfBoundsException();