Fixed a few crashes and added a FIXME for another which needs some refactoring.

git-svn-id: https://zxing.googlecode.com/svn/trunk@1814 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin@google.com 2011-06-08 16:15:29 +00:00
parent e04e3ef2c1
commit 127839b2b7
4 changed files with 49 additions and 20 deletions

View file

@ -38,6 +38,7 @@ final class DecodeHandler extends Handler {
private final CaptureActivity activity;
private final MultiFormatReader multiFormatReader;
private boolean running = true;
DecodeHandler(CaptureActivity activity, Hashtable<DecodeHintType, Object> hints) {
multiFormatReader = new MultiFormatReader();
@ -47,11 +48,15 @@ final class DecodeHandler extends Handler {
@Override
public void handleMessage(Message message) {
if (!running) {
return;
}
switch (message.what) {
case R.id.decode:
decode((byte[]) message.obj, message.arg1, message.arg2);
break;
case R.id.quit:
running = false;
Looper.myLooper().quit();
break;
}

View file

@ -446,6 +446,12 @@ final class DecodedBitStreamParser {
} else {
count = 250 * (d1 - 249) + unrandomize255State(bits.readBits(8), codewordPosition++);
}
// We're seeing NegativeArraySizeException errors from users.
if (count < 0) {
throw FormatException.getFormatInstance();
}
byte[] bytes = new byte[count];
for (int i = 0; i < count; i++) {
// Have seen this particular error in the wild, such as at

View file

@ -126,6 +126,11 @@ final class DecodedBitStreamParser {
private static void decodeHanziSegment(BitSource bits,
StringBuffer result,
int count) throws FormatException {
// Don't crash trying to read more bits than we have available.
if (count * 13 > bits.available()) {
throw FormatException.getFormatInstance();
}
// Each character will require 2 bytes. Read the characters as 2-byte pairs
// and decode as GB2312 afterwards
byte[] buffer = new byte[2 * count];
@ -157,6 +162,11 @@ final class DecodedBitStreamParser {
private static void decodeKanjiSegment(BitSource bits,
StringBuffer result,
int count) throws FormatException {
// Don't crash trying to read more bits than we have available.
if (count * 13 > bits.available()) {
throw FormatException.getFormatInstance();
}
// Each character will require 2 bytes. Read the characters as 2-byte pairs
// and decode as Shift_JIS afterwards
byte[] buffer = new byte[2 * count];
@ -191,10 +201,12 @@ final class DecodedBitStreamParser {
CharacterSetECI currentCharacterSetECI,
Vector byteSegments,
Hashtable hints) throws FormatException {
byte[] readBytes = new byte[count];
// Don't crash trying to read more bits than we have available.
if (count << 3 > bits.available()) {
throw FormatException.getFormatInstance();
}
byte[] readBytes = new byte[count];
for (int i = 0; i < count; i++) {
readBytes[i] = (byte) bits.readBits(8);
}

View file

@ -308,14 +308,19 @@ public class Detector {
int dx = Math.abs(toX - fromX);
int dy = Math.abs(toY - fromY);
int error = -dx >> 1;
int ystep = fromY < toY ? 1 : -1;
int xstep = fromX < toX ? 1 : -1;
int state = 0; // In black pixels, looking for white, first or second time
for (int x = fromX, y = fromY; x != toX; x += xstep) {
int ystep = fromY < toY ? 1 : -1;
// In black pixels, looking for white, first or second time.
int state = 0;
for (int x = fromX, y = fromY; x != toX; x += xstep) {
int realX = steep ? y : x;
int realY = steep ? x : y;
if (state == 1) { // In white pixels, looking for black
// In white pixels, looking for black.
// FIXME(dswitkin): This method seems to assume square images, which can cause these calls to
// BitMatrix.get() to throw ArrayIndexOutOfBoundsException.
if (state == 1) {
if (image.get(realX, realY)) {
state++;
}
@ -325,7 +330,8 @@ public class Detector {
}
}
if (state == 3) { // Found black, white, black, and stumbled back onto white; done
// Found black, white, black, and stumbled back onto white, so we're done.
if (state == 3) {
int diffX = x - fromX;
int diffY = y - fromY;
if (xstep < 0) {