[19/25] libelf: F_GETFD may not predefined with msvc/mingw, guard the usage of it

Message ID 20221020182603.815-20-luoyonggang@gmail.com
State Superseded
Headers
Series Patches for building with mingw/gcc msvc/clang-cl |

Commit Message

Yonggang Luo Oct. 20, 2022, 6:25 p.m. UTC
  Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 libelf/elf_begin.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Mark Wielaard Dec. 12, 2022, 3:54 p.m. UTC | #1
Hi,

On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
wrote:
> Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
> ---
>  libelf/elf_begin.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
> index 6d31882e..d867cd6f 100644
> --- a/libelf/elf_begin.c
> +++ b/libelf/elf_begin.c
> @@ -1163,12 +1163,14 @@ elf_begin (int fildes, Elf_Cmd cmd, Elf *ref)
>    if (ref != NULL)
>      /* Make sure the descriptor is not suddenly going away.  */
>      rwlock_rdlock (ref->lock);
> +#if defined(F_GETFD)
>    else if (unlikely (fcntl (fildes, F_GETFD) == -1 && errno ==
> EBADF))
>      {
>        /* We cannot do anything productive without a file
> descriptor.  */
>        __libelf_seterrno (ELF_E_INVALID_FILE);
>        return NULL;
>      }
> +#endif

If you cannot check validity of fildes using fcntl, then shouldn't you
at least check for fildes >= 0 ?

Cheers,

Mark
  

Patch

diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 6d31882e..d867cd6f 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -1163,12 +1163,14 @@  elf_begin (int fildes, Elf_Cmd cmd, Elf *ref)
   if (ref != NULL)
     /* Make sure the descriptor is not suddenly going away.  */
     rwlock_rdlock (ref->lock);
+#if defined(F_GETFD)
   else if (unlikely (fcntl (fildes, F_GETFD) == -1 && errno == EBADF))
     {
       /* We cannot do anything productive without a file descriptor.  */
       __libelf_seterrno (ELF_E_INVALID_FILE);
       return NULL;
     }
+#endif
 
   switch (cmd)
     {