You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
The text was updated successfully, but these errors were encountered:
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.
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
andStruct.StringRef
. I assumed that the former was intended for use in structures likeand the latter for structures like
I get this impression given that the
set
method ofStruct.String
writes directly to memory, while that ofStruct.StringRef
allocates new memory and then writes. The default constructor for both, however, sets the string length toInteger.MAX_VALUE
, or2**31 - 1
, and this value is used for allocation, rather than the input value size. This leads to failed allocations (theStruct.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.The text was updated successfully, but these errors were encountered: