Skip to content

Commit

Permalink
Merge pull request #1 from exceljava/issue-79
Browse files Browse the repository at this point in the history
Correct buffer size to fix memory corruption
  • Loading branch information
tonyroberts authored May 28, 2019
2 parents ec2f24f + 89c4d91 commit b5a8f8c
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions runtime/src/main/java/com4j/Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,32 @@ public int comEnumValue() {
}
}

/**
* Determine the correct size of {@link Variant}.
*
* The size of the variant depends on whether this is
* a 32 or 64 bit system due to pointers in the structure
* definition. See https://docs.microsoft.com/en-gb/windows/desktop/api/oaidl/ns-oaidl-tagvariant
*/
private static int variantSize() {
/* Typical os.arch values: `x86_64`, `amd64` */
String model = System.getProperty("os.arch");
if (model.indexOf("64") != -1) {
return 24;
}
return 16;
}

private static final int variantSize = variantSize();

/**
* Creates an empty {@link Variant}.
*/
public Variant() {
image = ByteBuffer.allocateDirect(16);
image = ByteBuffer.allocateDirect(variantSize);
image.order(ByteOrder.LITTLE_ENDIAN);
// The initial content of a buffer is, in general, undefined. See the documentation of java.nio.Buffer.
byte[] b = new byte[16]; // this initializes the array with zeros
byte[] b = new byte[variantSize]; // this initializes the array with zeros
image.put(b); // this prints the zeros to the buffer to guarantee, that the buffer is initialized with zeros.
image.position(0);
}
Expand Down

0 comments on commit b5a8f8c

Please sign in to comment.