From 4ad2734576481f09a3f5ded3d80e9e938dbac5f8 Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Fri, 19 Sep 2014 14:29:01 +0200 Subject: [PATCH] CID-1114601 to 1114604 Recommended practice is to test the result of skip and read for EOF --- .../exception/InternalErrorException.java | 4 ++++ .../exception/ManagementServerException.java | 4 ++++ .../cloud/storage/template/VhdProcessor.java | 24 ++++++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/api/src/com/cloud/exception/InternalErrorException.java b/api/src/com/cloud/exception/InternalErrorException.java index 8404011b7f2..efb68d47d10 100644 --- a/api/src/com/cloud/exception/InternalErrorException.java +++ b/api/src/com/cloud/exception/InternalErrorException.java @@ -24,4 +24,8 @@ public class InternalErrorException extends ManagementServerException { super(message); } + public InternalErrorException(String message, Throwable cause) { + super(message, cause); + } + } diff --git a/api/src/com/cloud/exception/ManagementServerException.java b/api/src/com/cloud/exception/ManagementServerException.java index c8a106a6c6c..86fd233320a 100644 --- a/api/src/com/cloud/exception/ManagementServerException.java +++ b/api/src/com/cloud/exception/ManagementServerException.java @@ -30,4 +30,8 @@ public class ManagementServerException extends CloudException { super(message); } + public ManagementServerException(String message, Throwable cause) { + super(message, cause); + } + } diff --git a/core/src/com/cloud/storage/template/VhdProcessor.java b/core/src/com/cloud/storage/template/VhdProcessor.java index aff2942ea39..875376f301b 100644 --- a/core/src/com/cloud/storage/template/VhdProcessor.java +++ b/core/src/com/cloud/storage/template/VhdProcessor.java @@ -79,13 +79,25 @@ public class VhdProcessor extends AdapterBase implements Processor { byte[] creatorApp = new byte[4]; try { strm = new FileInputStream(vhdFile); - strm.skip(info.size - vhdFooterSize + vhdFooterCreatorAppOffset); - strm.read(creatorApp); - strm.skip(vhdFooterCurrentSizeOffset - vhdFooterCreatorVerOffset); - strm.read(currentSize); - } catch (Exception e) { + long skipped = strm.skip(info.size - vhdFooterSize + vhdFooterCreatorAppOffset); + if (skipped == -1) { + throw new InternalErrorException("Unexpected end-of-file"); + } + long read = strm.read(creatorApp); + if (read == -1) { + throw new InternalErrorException("Unexpected end-of-file"); + } + skipped = strm.skip(vhdFooterCurrentSizeOffset - vhdFooterCreatorVerOffset); + if (skipped == -1) { + throw new InternalErrorException("Unexpected end-of-file"); + } + read = strm.read(currentSize); + if (read == -1) { + throw new InternalErrorException("Unexpected end-of-file"); + } + } catch (IOException e) { s_logger.warn("Unable to read vhd file " + vhdPath, e); - throw new InternalErrorException("Unable to read vhd file " + vhdPath + ": " + e); + throw new InternalErrorException("Unable to read vhd file " + vhdPath + ": " + e, e); } finally { if (strm != null) { try {