Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed a casting bug that could cause a read timeout of zero

- Made the code a bit more tidy, one try catch instead of two for
  the Thread.sleep() calls.
- When the HSM respond to the wrong command (which really shouldn't happen
  but did due to the casting bug) the expected and actual command will be shown
  in the exception.
  • Loading branch information...
commit 1408614a1e77deadbeeb4e4e7b0094d906b17dd9 1 parent a3ac89b
@Ratler authored
Showing with 10 additions and 13 deletions.
  1. +10 −13 src/main/java/org/unitedid/yhsm/internal/CommandHandler.java
View
23 src/main/java/org/unitedid/yhsm/internal/CommandHandler.java
@@ -39,21 +39,19 @@ private CommandHandler() {}
log.debug("CMD BUFFER: {}", Utils.byteArrayToHex(Utils.concatAllArrays(cmdBuffer, data)));
device.write(Utils.concatAllArrays(cmdBuffer, data));
-
- if (!readResponse) {
- try {
+ try {
+ if (!readResponse) {
Thread.sleep(10); // We just sleep for safety since we cant check if we got any output
return null;
- } catch (InterruptedException e) {
- e.printStackTrace();
}
- }
- try {
- int timeout = (int) device.getTimeout() * 1000; // Need milliseconds
int sleptTime = 0;
+ int timeout = 100; // We enforce a default just in case (100 ms)
+ if (device.getTimeout() > 0) {
+ timeout = (int) (device.getTimeout() * 1000); // Need milliseconds
+ }
- log.debug("CommandHandler (" + Defines.getCommandString(command) + ") timeout set to: " + timeout);
+ log.debug("CommandHandler ({}) timeout set to: {} ms ", Defines.getCommandString(command), timeout);
while (sleptTime <= timeout) {
Thread.sleep(1);
@@ -62,12 +60,11 @@ private CommandHandler() {}
break;
}
}
- log.debug("CommandHandler slept for: " + sleptTime + " ms");
+ log.debug("CommandHandler slept for: {} ms", sleptTime);
} catch (InterruptedException e) {
- e.printStackTrace();
+ throw new RuntimeException(e);
}
-
return readDevice(device, command);
}
@@ -91,7 +88,7 @@ private CommandHandler() {}
return device.read(len);
} else {
reset(device);
- throw new YubiHSMErrorException("YubiHSM responded to the wrong command!");
+ throw new YubiHSMErrorException("YubiHSM responded to the wrong command. Expected " + Defines.getCommandString(command) + " but got " + Defines.getCommandString((byte) (result[1] - Defines.YSM_RESPONSE)));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.