On Sun, Apr 05 2026, Zhu Yanjun wrote:

> 在 2026/4/5 0:34, Pratyush Yadav 写道:
>> On Sat, Apr 04 2026, Zhu Yanjun wrote:
>>
>>> 在 2026/4/4 3:24, Pratyush Yadav 写道:
>>>> From: "Pratyush Yadav (Google)" <[email protected]>
>>>> Add some helper functions that will be used by memfd tests. This moves
>>>> some of the complexity out of the test itself, which results in better
>>>> test readability and less code duplication.
>>>> Reviewed-by: Mike Rapoport (Microsoft) <[email protected]>
>>>> Signed-off-by: Pratyush Yadav <[email protected]>
>>>> Signed-off-by: Pratyush Yadav (Google) <[email protected]>
>>>> ---
>>>>    .../selftests/liveupdate/luo_test_utils.c     | 191 +++++++++++++++++-
>>>>    .../selftests/liveupdate/luo_test_utils.h     |  10 +
>>>>    2 files changed, 200 insertions(+), 1 deletion(-)
>>>> diff --git a/tools/testing/selftests/liveupdate/luo_test_utils.c
>>>> b/tools/testing/selftests/liveupdate/luo_test_utils.c
>>>> index 3c8721c505df..ceb918ef9813 100644
>>>> --- a/tools/testing/selftests/liveupdate/luo_test_utils.c
>>>> +++ b/tools/testing/selftests/liveupdate/luo_test_utils.c
>>>> @@ -1,8 +1,12 @@
>>>>    // SPDX-License-Identifier: GPL-2.0-only
>>>>      /*
>>>> - * Copyright (c) 2025, Google LLC.
>>>> + * Copyright (c) 2025-2026, Google LLC.
>>>>     * Pasha Tatashin <[email protected]>
>>>> + * Pratyush Yadav (Google) <[email protected]>
>>>> + *
>>>> + * Copyright (C) 2025 Amazon.com Inc. or its affiliates.
>>>> + * Pratyush Yadav <[email protected]>
>>>>     */
>>>>      #define _GNU_SOURCE
>>>> @@ -20,9 +24,194 @@
>>>>    #include <sys/stat.h>
>>>>    #include <errno.h>
>>>>    #include <stdarg.h>
>>>> +#include <sys/vfs.h>
>>>> +#include <linux/magic.h>
>>>>      #include "luo_test_utils.h"
>>>>    +int cwd_is_tmpfs(void)
>>>> +{
>>>> +  struct statfs buf;
>>>> +
>>>> +  if (statfs(".", &buf) < 0)
>>>> +          return -errno;
>>>> +
>>>> +  return buf.f_type == TMPFS_MAGIC;
>>>> +}
>>>> +
>>>> +/* Read exactly specified size from fd. Any less results in error. */
>>>> +int read_size(int fd, char *buffer, size_t size)
>>>> +{
>>>> +  size_t remain = size;
>>>> +  ssize_t bytes_read;
>>>> +
>>>> +  while (remain) {
>>> while (remain > 0) ?
>> I don't think it is needed. bytes_read will always be smaller than or
>> equal to remain, so remain will never go below 0 anyway. And while
>> (remain) is nicer to read.
>
> If a wrong number is transferred, this makes remain less than 0. Then this 
> loop
> will run again and again.

That can only happen if read() returns more then the requested number of
bytes. That is, if bytes_read > remain. And read() will never do so
since that can be a buffer overflow and would break userspace left and
right.

So, I don't see how this is a problem.

>
> This while (remain > 0) will avoid this kind of scenario.
>
> ZhuYanjun
>
>>
>>>> +          bytes_read = read(fd, buffer, remain);
>>>> +          if (bytes_read == 0)
>>>> +                  return -ENODATA;
>>>> +          if (bytes_read < 0) {
>>>> +                  if (errno == EINTR)
>>>> +                          continue;
>>>> +                  else
>>>> +                          return -errno;
>>>> +          }
>>>> +
>>>> +          remain -= bytes_read;
>>>> +          buffer += bytes_read;
>>>> +  }
>>>> +
>>>> +  return 0;
>>>> +}
>>>> +
>> [...]
>>

-- 
Regards,
Pratyush Yadav

Reply via email to