Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct buffer size to fix memory corruption #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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