[Devel] [PATCH vz10] selftests/filelock: zero-initialize struct flock

Pavel Tikhomirov ptikhomirov at virtuozzo.com
Thu Jun 25 14:13:36 MSK 2026


note: This should also be sent upstream.

On 6/25/26 13:12, Pavel Tikhomirov wrote:
> Reviewed-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
> 
> On 6/25/26 09:46, Vasileios Almpanis wrote:
>> Reviewed-by: Vasileios Almpanis <vasileios.almpanis at virtuozzo.com>
>>
>> On 6/25/26 12:42 AM, Eva Kurchatova wrote:
>>> The ofdlocks test installs an OFD read lock at offset 5..7 and then
>>> issues two F_OFD_GETLK queries (one starting at offset 5 with length 1,
>>> one starting at offset 0 with length 0) expecting them to report the
>>> same lock. It then compares the two struct flock results with memcmp().
>>>
>>> Because 'fl' and 'fl2' live on the stack and are only partially
>>> initialized by the caller (l_type, l_whence, l_start, l_len, l_pid),
>>> the implicit padding bytes that 'struct flock' has on x86_64 are left
>>> with whatever happens to be on the stack. The kernel does not touch
>>> those padding bytes on the F_OFD_GETLK return path, so memcmp() of the
>>> full sizeof(struct flock) compares uninitialized padding from two
>>> different stack frames and intermittently reports them as different:
>>>
>>>    [SUCCESS] F_UNLCK test returns: locked, type 0 pid -1 len 3
>>>    [FAIL]    F_UNLCK test returns: locked, type 0 pid -1 len 3
>>>
>>> i.e. the printed fields are identical but memcmp() still fails.
>>>
>>> Zero-initialize both structs at declaration so the padding is
>>> deterministic and the comparison is meaningful.
>>>
>>> Signed-off-by: Eva Kurchatova <eva.kurchatova at virtuozzo.com>
>>>
>>> https://virtuozzo.atlassian.net/browse/VSTOR-134203
>>> Feature: fix selftests
>>>
>>> ---
>>>   tools/testing/selftests/filelock/ofdlocks.c | 2 +-
>>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/tools/testing/selftests/filelock/ofdlocks.c b/tools/testing/selftests/filelock/ofdlocks.c
>>> index a55b79810ab2..45dc417dae89 100644
>>> --- a/tools/testing/selftests/filelock/ofdlocks.c
>>> +++ b/tools/testing/selftests/filelock/ofdlocks.c
>>> @@ -35,7 +35,7 @@ static int lock_get(int fd, struct flock *fl)
>>>   int main(void)
>>>   {
>>>       int rc;
>>> -    struct flock fl, fl2;
>>> +    struct flock fl = {0}, fl2 = {0};
>>>       int fd = open("/tmp/aa", O_RDWR | O_CREAT | O_EXCL, 0600);
>>>       int fd2 = open("/tmp/aa", O_RDONLY);
>>>   
>>
> 

-- 
Best regards, Pavel Tikhomirov
Senior Software Developer, Virtuozzo.



More information about the Devel mailing list