Mailing List Archive

[PATCH] w32: Fix clang compiler error with function pointer
This fixes the following compiler error

../../src/gpgme-w32spawn.c:253:20: error: incompatible function pointer
types assigning to 'BOOL (*)(DWORD) __attribute__((stdcall))'
(aka 'int (*)(unsigned long)') from 'FARPROC' (aka 'long long (*)()')
[-Wincompatible-function-pointer-types]
func = GetProcAddress (handle, "AllowSetForegroundWindow");
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Biswapriyo Nath <nathbappai@gmail.com>
---
src/gpgme-w32spawn.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/gpgme-w32spawn.c b/src/gpgme-w32spawn.c
index 09dbea7..8a15e7f 100644
--- a/src/gpgme-w32spawn.c
+++ b/src/gpgme-w32spawn.c
@@ -240,7 +240,8 @@ my_spawn (char **argv, struct spawn_fd_item_s *fd_list, unsigned int flags)
if (flags & IOSPAWN_FLAG_ALLOW_SET_FG)
{
static int initialized;
- static BOOL (WINAPI * func)(DWORD);
+ typedef BOOL (WINAPI * allow_set_forground)(DWORD);
+ static allow_set_forground func;
void *handle;

if (!initialized)
@@ -250,7 +251,7 @@ my_spawn (char **argv, struct spawn_fd_item_s *fd_list, unsigned int flags)
handle = LoadLibrary ("user32.dll");
if (handle)
{
- func = GetProcAddress (handle, "AllowSetForegroundWindow");
+ func = (allow_set_forground) GetProcAddress (handle, "AllowSetForegroundWindow");
if (!func)
FreeLibrary (handle);
}
--
2.40.1


_______________________________________________
Gnupg-devel mailing list
Gnupg-devel@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gnupg-devel
Re: [PATCH] w32: Fix clang compiler error with function pointer [ In reply to ]
Biswapriyo Nath via Gnupg-devel wrote:
> [...]
> - static BOOL (WINAPI * func)(DWORD);
> + typedef BOOL (WINAPI * allow_set_forground)(DWORD);
> + static allow_set_forground func;
> [...]
> - func = GetProcAddress (handle, "AllowSetForegroundWindow");
> + func = (allow_set_forground) GetProcAddress (handle, "AllowSetForegroundWindow");
> [...]

Is "allow_set_forground" intended or a typo for "allow_set_foreground"?

Also, this looks like a clang bug: both version of the code should
declare a function pointer of the same type.


-- Jacob

_______________________________________________
Gnupg-devel mailing list
Gnupg-devel@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gnupg-devel
Re: [PATCH] w32: Fix clang compiler error with function pointer [ In reply to ]
> Is "allow_set_forground" intended or a typo for "allow_set_foreground"?

Yes, I didn't notice it.

> Also, this looks like a clang bug: both version of the code should
> declare a function pointer of the same type.

I do not think so. gcc also shows a compiler warning with that line as
following.

../../src/gpgme-w32spawn.c:253:20: warning: assignment to 'BOOL
(*)(DWORD)' {aka 'int (*)(long unsigned int)'} from incompatible
pointer type 'FARPROC' {aka 'long long
int (*)()'} [-Wincompatible-pointer-types]
253 | func = GetProcAddress (handle,
"AllowSetForegroundWindow");
| ^

clang only treats it as a compiler error by default.

_______________________________________________
Gnupg-devel mailing list
Gnupg-devel@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gnupg-devel
Re: [PATCH] w32: Fix clang compiler error with function pointer [ In reply to ]
Biswapriyo Nath wrote:
>> Is "allow_set_forground" intended or a typo for "allow_set_foreground"?
>>
>
> Yes, I didn't notice it.
>
>
>> Also, this looks like a clang bug: both version of the code should
>> declare a function pointer of the same type.
>>
>
> I do not think so. gcc also shows a compiler warning with that line as
> following.
>
> ../../src/gpgme-w32spawn.c:253:20: warning: assignment to 'BOOL
> (*)(DWORD)' {aka 'int (*)(long unsigned int)'} from incompatible
> pointer type 'FARPROC' {aka 'long long
> int (*)()'} [-Wincompatible-pointer-types]
> 253 | func = GetProcAddress (handle,
> "AllowSetForegroundWindow");
> | ^
>
> clang only treats it as a compiler error by default.
>

Oh, right. Your patch actually has two changes. You should also be
able to also cast the pointer to "(BOOL (*)(DWORD))" without naming the
type. E.g.

func = (BOOL (*)(DWORD)) GetProcAddress (handle, "AllowSetForegroundWindow");


... but I do not have a Windows box to test that on.


-- Jacob

_______________________________________________
Gnupg-devel mailing list
Gnupg-devel@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gnupg-devel
Re: [PATCH] w32: Fix clang compiler error with function pointer [ In reply to ]
> Oh, right. Your patch actually has two changes. You should also be
> able to also cast the pointer to "(BOOL (*)(DWORD))" without naming the
> type. E.g.
>
> func = (BOOL (*)(DWORD)) GetProcAddress (handle, "AllowSetForegroundWindow");

Yeah, that would also work. I just add the type to make it little bit
more readable.

_______________________________________________
Gnupg-devel mailing list
Gnupg-devel@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gnupg-devel
Re: [PATCH] w32: Fix clang compiler error with function pointer [ In reply to ]
Any comment on this patch? Is it possible to add the patch please?

_______________________________________________
Gnupg-devel mailing list
Gnupg-devel@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gnupg-devel
Re: [PATCH] w32: Fix clang compiler error with function pointer [ In reply to ]
On Mon, 12 Jun 2023 10:12, Biswapriyo Nath said:
> Any comment on this patch? Is it possible to add the patch please?

I see no reason to work around a clang oddity for a Windows target. We
have a well working toolchain and that is what you should use.


Salam-Shalom,

Werner


--
The pioneers of a warless world are the youth that
refuse military service. - A. Einstein
Re: [PATCH] w32: Fix clang compiler error with function pointer [ In reply to ]
* Werner Koch via Gnupg-devel:

> On Mon, 12 Jun 2023 10:12, Biswapriyo Nath said:
>> Any comment on this patch? Is it possible to add the patch please?
>
> I see no reason to work around a clang oddity for a Windows target. We
> have a well working toolchain and that is what you should use.

GCC will eventually make this change as well (although probably not in
GCC 14). This hasn't been valid C for decades.

Thanks,
Florian


_______________________________________________
Gnupg-devel mailing list
Gnupg-devel@gnupg.org
https://lists.gnupg.org/mailman/listinfo/gnupg-devel