Skip to content

Commit b905d46

Browse files
authored
Merge pull request #4288 from melissalinkert/implicit-type-narrowing
Fix a bunch of `Implicit type narrowing` warnings
2 parents dce7424 + 7f1979d commit b905d46

File tree

10 files changed

+52
-14
lines changed

10 files changed

+52
-14
lines changed

components/formats-bsd/src/loci/formats/ResourceNamer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ public String getLetter() {
139139
public static int alphabeticIndexCount(String index) {
140140
int count = 0;
141141
char[] letters = index.toCharArray();
142+
if (letters.length > 6) {
143+
// this will cause an integer overflow
144+
throw new IllegalArgumentException("Cannot count more than 6 characters");
145+
}
142146
for (int i = 0; i < letters.length; i++) {
143-
count += (letters[i] - 64) * Math.pow(ALPHABET_LENGTH, i);
147+
int letterIndex = (int) Math.pow(ALPHABET_LENGTH, i);
148+
int letterValue = letterIndex * (letters[i] - 64);
149+
count += letterValue;
144150
}
145151
return count;
146152
}

components/formats-bsd/src/loci/formats/gui/AWTImageTools.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,7 @@ else if (pixels instanceof short[][]) {
16321632

16331633
for (int i=0; i<out.length; i++) {
16341634
for (int j=0; j<out[i].length; j++) {
1635-
if (shorts[i][j] < 0) shorts[i][j] += 32767;
1635+
if (shorts[i][j] < 0) shorts[i][j] += Short.MAX_VALUE;
16361636

16371637
int diff = max - min;
16381638
float dist = (float) (shorts[i][j] - min) / diff;

components/formats-bsd/src/loci/formats/in/FlowSightReader.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,21 @@ public boolean hasNext() {
382382
shift = 0;
383383
value = 0;
384384
while (! loaded) {
385+
if (shift > 15) {
386+
// this implies that all subsequently read bits will be cleared
387+
LOGGER.error("Unexpected bit shift value {}, image may be truncated", shift);
388+
return false;
389+
}
385390
byte nibble;
386391
try {
387392
nibble = getNextNibble();
388-
value += ((short) (nibble & 0x7) ) << shift;
389-
shift += 3;
393+
394+
// these 2 additions are safe since shift is 15 or less at this point
395+
// due to check at top of loop
396+
short lowBits = (short) (nibble & 0x7);
397+
value += (short) (lowBits << shift);
398+
shift += (short) 3;
399+
390400
if ((nibble & 0x8) == 0) {
391401
loaded = true;
392402
bHasNext = true;

components/formats-bsd/src/loci/formats/in/MinimalTiffReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,14 +341,16 @@ public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
341341
}
342342
exponent++;
343343
mantissa &= (int) (Math.pow(2, mantissaBits) - 1);
344-
exponent += 127 - (Math.pow(2, exponentBits - 1) - 1);
344+
int max = (int) Math.pow(2, exponentBits - 1);
345+
exponent += 127 - (max - 1);
345346
}
346347
}
347348
else if (exponent == maxExponent) {
348349
exponent = 255;
349350
}
350351
else {
351-
exponent += 127 - (Math.pow(2, exponentBits - 1) - 1);
352+
int max = (int) Math.pow(2, exponentBits - 1);
353+
exponent += 127 - (max - 1);
352354
}
353355

354356
mantissa <<= (23 - mantissaBits);

components/formats-bsd/src/loci/formats/in/OBFReader.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,13 @@ private void readFromStack(Stack stack, byte[] buffer, int bufferOffset, int byt
756756
long bytesWrittenAfter = inflater.getBytesWritten();
757757

758758
long decompressedBytes = bytesWrittenAfter - bytesWrittenBefore;
759+
if (decompressedBytes + bufferOffset > Integer.MAX_VALUE) {
760+
throw new FormatException(
761+
"Unexpectedly large number of bytes decompressed: " + decompressedBytes);
762+
}
759763

760-
bufferOffset += decompressedBytes;
761-
remainingBytes -= decompressedBytes;
764+
bufferOffset += (int) decompressedBytes;
765+
remainingBytes -= (int) decompressedBytes;
762766

763767
if(remainingBytes != 0) {
764768
throw new FormatException(

components/formats-bsd/src/loci/formats/tiff/TiffParser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,12 @@ public byte[] getSamples(IFD ifd, byte[] buf, int x, int y,
10621062
return buf;
10631063
}
10641064

1065-
long nrows = numTileRows;
1065+
// very unlikely, but checking here so that the tile row count
1066+
// can be cast to int without worrying about overflowing
1067+
if (numTileRows > Integer.MAX_VALUE) {
1068+
throw new FormatException(numTileRows + " rows of tiles not supported");
1069+
}
1070+
int nrows = (int) numTileRows;
10661071
if (planarConfig == 2) numTileRows *= samplesPerPixel;
10671072

10681073
Region imageBounds = new Region(x, y, (int) width, (int) height);
@@ -1145,7 +1150,9 @@ public byte[] getSamples(IFD ifd, byte[] buf, int x, int y,
11451150
int src = (int) (q * tileSize) + realX + realY;
11461151
int dest = (int) (q * planeSize) + pixel * (tileX - x) +
11471152
outputRowLen * (tileY - y);
1148-
if (planarConfig == 2) dest += (planeSize * (row / nrows));
1153+
if (planarConfig == 2) {
1154+
dest += (planeSize * (row / nrows));
1155+
}
11491156

11501157
// copying the tile directly will only work if there is no overlap
11511158
// and only one tile needs to be read

components/formats-bsd/src/loci/formats/tools/MakeTestOmeTiff.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private BufferedImage createPlane(final String name, final CoreMetadata info,
374374
g.setFont(text.font);
375375
final Rectangle2D r =
376376
g.getFont().getStringBounds(text.line, g.getFontRenderContext());
377-
yoff += r.getHeight() + text.ypad;
377+
yoff += (int) r.getHeight() + text.ypad;
378378
g.drawString(text.line, text.xoff, yoff);
379379
}
380380
g.dispose();

components/formats-gpl/src/loci/formats/in/BioRadGelReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected void initFile(String id) throws FormatException, IOException {
152152
in.seek(START_OFFSET);
153153

154154
boolean codeFound = false;
155-
int skip = 0;
155+
long skip = 0;
156156
long baseFP = 0;
157157

158158
while (!codeFound) {

components/formats-gpl/src/loci/formats/in/PCIReader.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,16 @@ else if (relativePath.equals("Comments")) {
415415
TiffParser tp = new TiffParser(s);
416416
// don't correct the image width if it's stored as a TIFF
417417
if (!tp.isValidHeader() && s.length() > expectedPlaneSize) {
418-
m.sizeX += (s.length() - expectedPlaneSize) / (m.sizeY * bpp * m.sizeC);
418+
long extraBytesInFile = s.length() - expectedPlaneSize;
419+
long extraPixelsPerRow = extraBytesInFile / (m.sizeY * bpp * m.sizeC);
420+
421+
// this would be very unexpected, but checking here so that
422+
// casting extraPixelsPerRow to int or adding to m.sizeX cannot overflow
423+
if (extraPixelsPerRow >= Integer.MAX_VALUE - m.sizeX) {
424+
throw new FormatException("Stream length (" + s.length() +
425+
") inconsistent with plane size in bytes (" + expectedPlaneSize + ")");
426+
}
427+
m.sizeX += (int) extraPixelsPerRow;
419428
}
420429
}
421430

components/formats-gpl/src/loci/formats/in/XLEFReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ private void transformBytes8To16(byte[] in, byte[] out, int outRes) {
341341

342342
int inPtr = 0;
343343
int outPtr = 0;
344-
double factor = Math.pow(2, outRes - 8);
344+
int factor = (int) Math.pow(2, outRes - 8);
345345
while (inPtr < in.length && outPtr < out.length) {
346346
int val = in[inPtr++];
347347
val *= factor;

0 commit comments

Comments
 (0)