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

CFStringRef#stringValue buffer needs space for null byte #1343

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Features

Bug Fixes
---------

* [#1343](https://github.com/java-native-access/jna/issues/1343): `c.s.j.p.mac.CoreFoundation.CFStringRef#stringValue` buffer needs space for null byte - [@dbwiddis](https://github.com/dbwiddis).

Release 5.8.0
=============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,14 +487,18 @@ public static CFStringRef createCFString(String s) {
* failed.
*/
public String stringValue() {
// Get number of characters
CFIndex length = INSTANCE.CFStringGetLength(this);
if (length.longValue() == 0) {
return "";
}
// Calculate maximum possible size in UTF8 bytes
CFIndex maxSize = INSTANCE.CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
if (maxSize.intValue() == kCFNotFound) {
throw new StringIndexOutOfBoundsException("CFString maximum number of bytes exceeds LONG_MAX.");
}
// Increment size by 1 for a null byte
maxSize.setValue(maxSize.longValue() + 1);
Memory buf = new Memory(maxSize.longValue());
if (0 != INSTANCE.CFStringGetCString(this, buf, maxSize, kCFStringEncodingUTF8)) {
return buf.getString(0, "UTF8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.junit.Assert.fail;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -62,25 +63,27 @@ public class CoreFoundationTest {

@Test
public void testCFStringRef() throws UnsupportedEncodingException {
String awesome = "ǝɯosǝʍɐ sı ∀Nſ"; // Unicode
CFStringRef cfAwesome = CFStringRef.createCFString(awesome);
assertEquals(awesome.length(), CF.CFStringGetLength(cfAwesome).intValue());
assertEquals(awesome, cfAwesome.stringValue());
assertEquals(CoreFoundation.STRING_TYPE_ID, cfAwesome.getTypeID());

byte[] awesomeArr = awesome.getBytes("UTF-8");
Memory mem = new Memory(awesomeArr.length + 1);
// Create a unicode string of a single 3-byte character
byte[] alaf = { (byte) 0xe0, (byte) 0xa0, (byte) 0x80 };
String utf8Str = new String(alaf, StandardCharsets.UTF_8);
CFStringRef cfStr = CFStringRef.createCFString(utf8Str);
assertEquals(utf8Str.length(), CF.CFStringGetLength(cfStr).intValue());
assertEquals(utf8Str, cfStr.stringValue());
assertEquals(CoreFoundation.STRING_TYPE_ID, cfStr.getTypeID());

byte[] utf8Arr = utf8Str.getBytes("UTF-8");
Memory mem = new Memory(utf8Arr.length + 1);
mem.clear();
assertNotEquals(0,
CF.CFStringGetCString(cfAwesome, mem, new CFIndex(mem.size()), CoreFoundation.kCFStringEncodingUTF8));
byte[] awesomeBytes = mem.getByteArray(0, (int) mem.size() - 1);
assertArrayEquals(awesomeArr, awesomeBytes);
CF.CFStringGetCString(cfStr, mem, new CFIndex(mem.size()), CoreFoundation.kCFStringEncodingUTF8));
byte[] utf8Bytes = mem.getByteArray(0, (int) mem.size() - 1);
assertArrayEquals(utf8Arr, utf8Bytes);
// Essentially a toString, can't rely on format but should contain the string
CFStringRef desc = CF.CFCopyDescription(cfAwesome);
assertTrue(desc.stringValue().contains(awesome));
CFStringRef desc = CF.CFCopyDescription(cfStr);
assertTrue(desc.stringValue().contains(utf8Str));

desc.release();
cfAwesome.release();
cfStr.release();

CFStringRef cfEmpty = CFStringRef.createCFString("");
assertTrue(cfEmpty.stringValue().equals(""));
Expand Down