{"action":"created","issue":{"url":"https://api.github.com/repos/jnr/jnr-ffi/issues/7","repository_url":"https://api.github.com/repos/jnr/jnr-ffi","labels_url":"https://api.github.com/repos/jnr/jnr-ffi/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/jnr/jnr-ffi/issues/7/comments","events_url":"https://api.github.com/repos/jnr/jnr-ffi/issues/7/events","html_url":"https://github.com/jnr/jnr-ffi/issues/7","id":22091540,"number":7,"title":"Default Struct.StringRef constructor creates Integer.MAX_VALUE string?","user":{"login":"flandr","id":1644146,"avatar_url":"https://avatars.githubusercontent.com/u/1644146?v=3","gravatar_id":"","url":"https://api.github.com/users/flandr","html_url":"https://github.com/flandr","followers_url":"https://api.github.com/users/flandr/followers","following_url":"https://api.github.com/users/flandr/following{/other_user}","gists_url":"https://api.github.com/users/flandr/gists{/gist_id}","starred_url":"https://api.github.com/users/flandr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/flandr/subscriptions","organizations_url":"https://api.github.com/users/flandr/orgs","repos_url":"https://api.github.com/users/flandr/repos","events_url":"https://api.github.com/users/flandr/events{/privacy}","received_events_url":"https://api.github.com/users/flandr/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2013-11-05T00:45:09Z","updated_at":"2016-09-26T19:05:32Z","closed_at":null,"body":"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\r\n\r\n    struct inlined_string {\r\n        char string_field[kFieldSize];\r\n    }\r\n\r\nand the latter for structures like\r\n\r\n    struct reference_string {\r\n        char *string_ref_field;\r\n    }\r\n\r\nI 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. \r\n\r\nIt 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."},"comment":{"url":"https://api.github.com/repos/jnr/jnr-ffi/issues/comments/249666006","html_url":"https://github.com/jnr/jnr-ffi/issues/7#issuecomment-249666006","issue_url":"https://api.github.com/repos/jnr/jnr-ffi/issues/7","id":249666006,"user":{"login":"headius","id":10135,"avatar_url":"https://avatars.githubusercontent.com/u/10135?v=3","gravatar_id":"","url":"https://api.github.com/users/headius","html_url":"https://github.com/headius","followers_url":"https://api.github.com/users/headius/followers","following_url":"https://api.github.com/users/headius/following{/other_user}","gists_url":"https://api.github.com/users/headius/gists{/gist_id}","starred_url":"https://api.github.com/users/headius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/headius/subscriptions","organizations_url":"https://api.github.com/users/headius/orgs","repos_url":"https://api.github.com/users/headius/repos","events_url":"https://api.github.com/users/headius/events{/privacy}","received_events_url":"https://api.github.com/users/headius/received_events","type":"User","site_admin":false},"created_at":"2016-09-26T19:05:32Z","updated_at":"2016-09-26T19:05:32Z","body":"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.\r\n\r\nIf 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."}}
{"actor":{"display_login":"headius"}}