Skip to contentSkip to conversation

test_pkcs11.c: set file offset to 0 after ftruncate

PATCH In review [email protected]... 2 participants 2 messages Aug 12, 2024 – Aug 13, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test_pkcs11.c: set file offset to 0 after ftruncate

From: Selva Nair <[email protected]...>

Currently key and cert file fd's are reused after ftruncate() without setting the offset to zero. This causes subsequent data to be written at some finite offset with the hole in the file automatically filled by zeros. Fix it by calling lseek() to set the offset to zero.

The test works nevertheless because p11tool seem to generously ignore any junk before the "BEGIN" marker.

Change-Id Ib0fe15a4ba18d89216b0288e6cd6be66ed377bd4
---
 tests/unit_tests/openvpn/test_pkcs11.c | 2 ++
 1 file changed, 2 insertions(+)
tests/unit_tests/openvpn/test_pkcs11.c +2 -0
index 5518fa15..3fe3e460 100644
@@ -273,6 +273,8 @@ init(void **state)
 
         assert_int_equal(ftruncate(cert_fd, 0), 0);
         assert_int_equal(ftruncate(key_fd, 0), 0);
+        assert_int_equal(lseek(cert_fd, 0, SEEK_SET), 0);
+        assert_int_equal(lseek(key_fd, 0, SEEK_SET), 0);
         num_certs++;
     }
 
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw

"explanation makes sense, man ftruncate clearly says 'fd is not modified'"

smoke tested ("make check") on linux with --enable-pkcs11

Your patch has been applied to the master branch.

Not applied to 2.6 because the code in question does not exist there.

commit dcf735009c8caabf5e4a9feeb0d32907aafe8f17 (master)

Author: Selva Nair
Date:   Mon Aug 12 19:21:58 2024 -0400
     test_pkcs11.c: set file offset to 0 after ftruncate

     Signed-off-by: Selva Nair <[email protected]...>
     Acked-by: Gert Doering <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg29010.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering