Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cad4e327 authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Proper duping / closing of FileDescriptors

While looking into another bug, I noticed that the file descriptors
do not get properly duped when converting to HidlMemory and that they
aren't explicitly closed when creating a temporary instance of
SharedMemory.

Test: Manual verification of ST functionality, which uses these
      utilities.
Fixes: 188931943
Change-Id: I76322c56c58be5db8f1a40eb20eb3b96552412f9
parent 52fbf369
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -76,8 +76,7 @@ public final class HidlMemoryUtil {
            return new HidlMemory("ashmem", 0, null);
        }

        try {
            SharedMemory shmem = SharedMemory.create(name != null ? name : "", input.length);
        try (SharedMemory shmem = SharedMemory.create(name != null ? name : "", input.length)) {
            ByteBuffer buffer = shmem.mapReadWrite();
            buffer.put(input);
            shmem.unmap(buffer);
@@ -119,8 +118,7 @@ public final class HidlMemoryUtil {
            return new HidlMemory("ashmem", 0, null);
        }

        try {
            SharedMemory shmem = SharedMemory.create(name != null ? name : "", input.size());
        try (SharedMemory shmem = SharedMemory.create(name != null ? name : "", input.size())) {
            ByteBuffer buffer = shmem.mapReadWrite();
            for (Byte b : input) {
                buffer.put(b);
@@ -214,8 +212,12 @@ public final class HidlMemoryUtil {
        if (fd == null) {
            return new HidlMemory("ashmem", 0, null);
        }
        NativeHandle handle = new NativeHandle(fd, true);
        try {
            NativeHandle handle = new NativeHandle(Os.dup(fd), true);
            return new HidlMemory("ashmem", size, handle);
        } catch (ErrnoException e) {
            throw new RuntimeException(e);
        }
    }

    private static ByteBuffer getBuffer(@NonNull HidlMemory mem) {