-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from cryptomator/feature/dont-resolve-null-poi…
…nters Fix SIGSEV crashes by adding NULL pointer checks
- Loading branch information
Showing
10 changed files
with
128 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
jfuse-api/src/main/java/org/cryptomator/jfuse/api/util/MemoryUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.cryptomator.jfuse.api.util; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
|
||
public class MemoryUtils { | ||
|
||
@Nullable | ||
public static String toUtf8StringOrNull(MemorySegment string, long offset) { | ||
return MemorySegment.NULL.equals(string) ? null : string.getUtf8String(offset); | ||
} | ||
|
||
@Nullable | ||
public static String toUtf8StringOrNull(MemorySegment string) { | ||
return toUtf8StringOrNull(string, 0); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
jfuse-api/src/test/java/org/cryptomator/jfuse/api/util/MemoryUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.cryptomator.jfuse.api.util; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.lang.foreign.Arena; | ||
import java.lang.foreign.MemorySegment; | ||
|
||
public class MemoryUtilsTest { | ||
|
||
|
||
@Test | ||
@DisplayName("On MemorySegment != NULL pointer, method returns utf8 string in the memory region") | ||
void testValidSegmentReturnsString() { | ||
try (var arena = Arena.ofConfined()) { | ||
var address = arena.allocate(4); | ||
address.setUtf8String(0, "abc"); | ||
String result = MemoryUtils.toUtf8StringOrNull(address); | ||
Assertions.assertEquals("abc", result); | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("With offset, on MemorySegment != NULL pointer, method returns utf8 string in the memory region") | ||
void testValidSegmentReturnsStringAtOffset() { | ||
try (var arena = Arena.ofConfined()) { | ||
var address = arena.allocate(10); | ||
address.setUtf8String(5, "abc"); | ||
String result = MemoryUtils.toUtf8StringOrNull(address, 5); | ||
Assertions.assertEquals("abc", result); | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("When MemorySegment == NULL pointer, method returns null") | ||
void testNullPointerSegmentReturnsNull() { | ||
String result = MemoryUtils.toUtf8StringOrNull(MemorySegment.NULL, 0); | ||
Assertions.assertNull(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters