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

Default Struct.StringRef constructor creates Integer.MAX_VALUE string? #7

Open
flandr opened this issue Nov 5, 2013 · 1 comment
Open

Comments

@flandr
Copy link
Contributor

flandr commented Nov 5, 2013

I'm trying to understand the best way to use jnr-ffi to pass byte buffers into a native library, and am confused by the intended use of Struct.String and Struct.StringRef. I assumed that the former was intended for use in structures like

struct inlined_string {
    char string_field[kFieldSize];
}

and the latter for structures like

struct reference_string {
    char *string_ref_field;
}

I get this impression given that the set method of Struct.String writes directly to memory, while that of Struct.StringRef allocates new memory and then writes. The default constructor for both, however, sets the string length to Integer.MAX_VALUE, or 2**31 - 1, and this value is used for allocation, rather than the input value size. This leads to failed allocations (the Struct.length is multiplied by 4, leading to integer overflow) unless a maximum value size is specified in advance.

It seems like the proper thing to do for Struct.StringRef.set(String value) would be to allocate enough storage for the passed-in value, rather than a large default. Am I understanding the intentions of these types correctly? I'm happy to submit a patch if so.

@headius
Copy link
Member

headius commented Sep 26, 2016

Agree the logic here is a little weird, but you can also just create a String or StringRef with a specific length, and it appears the MAX_VALUE constructor for String no longer exists.

If it's possible to get the proper length where we're using MAX_VALUE now, I'd happily accept a PR. I did not see any logic using the MAX_VALUE logic, at least within jnr-ffi itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants