[06/11,C++/mingw] ser-tcp.c casts

Message ID 1446492970-21432-7-git-send-email-palves@redhat.com
State New, archived
Headers

Commit Message

Pedro Alves Nov. 2, 2015, 7:36 p.m. UTC
  Fixes a few errors like these:

../../src/gdb/ser-tcp.c: In function 'int net_open(serial*, const char*)':
../../src/gdb/ser-tcp.c:286:73: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
     res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len);
                                                                         ^

gdb/ChangeLog:
2015-11-01  Pedro Alves  <palves@redhat.com>

	* ser-tcp.c (net_open) [USE_WIN32API]: Cast getsockopt argument to
	char * instead of void *.
	(net_read_prim): Cast recv argument to
	char * instead of void *.
	(net_write_prim): Cast send argument to char * instead of void *.
---
 gdb/ser-tcp.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
  

Comments

Simon Marchi Nov. 2, 2015, 8:40 p.m. UTC | #1
On 15-11-02 02:36 PM, Pedro Alves wrote:
> Fixes a few errors like these:
> 
> ../../src/gdb/ser-tcp.c: In function 'int net_open(serial*, const char*)':
> ../../src/gdb/ser-tcp.c:286:73: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
>      res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len);
>                                                                          ^
> 
> gdb/ChangeLog:
> 2015-11-01  Pedro Alves  <palves@redhat.com>
> 
> 	* ser-tcp.c (net_open) [USE_WIN32API]: Cast getsockopt argument to
> 	char * instead of void *.
> 	(net_read_prim): Cast recv argument to
> 	char * instead of void *.
> 	(net_write_prim): Cast send argument to char * instead of void *.
> ---
>  gdb/ser-tcp.c | 19 +++++++++++++------
>  1 file changed, 13 insertions(+), 6 deletions(-)
> 
> diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c
> index ce40b61..507e0a1 100644
> --- a/gdb/ser-tcp.c
> +++ b/gdb/ser-tcp.c
> @@ -280,10 +280,12 @@ net_open (struct serial *scb, const char *name)
>  
>      len = sizeof (err);
>      /* On Windows, the fourth parameter to getsockopt is a "char *";
> -       on UNIX systems it is generally "void *".  The cast to "void *"
> -       is OK everywhere, since in C "void *" can be implicitly
> -       converted to any pointer type.  */
> +       on UNIX systems it is generally "void *".  */
> +#ifdef USE_WIN32API
> +    res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
> +#else
>      res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len);
> +#endif

You could have done exactly the opposite of the old comment, that is cast to
(char *).  It shouldn't generate any warning in either case.  But I think that
your way is fine too.
  
Pedro Alves Nov. 2, 2015, 8:55 p.m. UTC | #2
On 11/02/2015 08:40 PM, Simon Marchi wrote:
> On 15-11-02 02:36 PM, Pedro Alves wrote:
>> Fixes a few errors like these:
>>
>> ../../src/gdb/ser-tcp.c: In function 'int net_open(serial*, const char*)':
>> ../../src/gdb/ser-tcp.c:286:73: error: invalid conversion from 'void*' to 'char*' [-fpermissive]
>>      res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len);
>>                                                                          ^
>>
>> gdb/ChangeLog:
>> 2015-11-01  Pedro Alves  <palves@redhat.com>
>>
>> 	* ser-tcp.c (net_open) [USE_WIN32API]: Cast getsockopt argument to
>> 	char * instead of void *.
>> 	(net_read_prim): Cast recv argument to
>> 	char * instead of void *.
>> 	(net_write_prim): Cast send argument to char * instead of void *.
>> ---
>>  gdb/ser-tcp.c | 19 +++++++++++++------
>>  1 file changed, 13 insertions(+), 6 deletions(-)
>>
>> diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c
>> index ce40b61..507e0a1 100644
>> --- a/gdb/ser-tcp.c
>> +++ b/gdb/ser-tcp.c
>> @@ -280,10 +280,12 @@ net_open (struct serial *scb, const char *name)
>>  
>>      len = sizeof (err);
>>      /* On Windows, the fourth parameter to getsockopt is a "char *";
>> -       on UNIX systems it is generally "void *".  The cast to "void *"
>> -       is OK everywhere, since in C "void *" can be implicitly
>> -       converted to any pointer type.  */
>> +       on UNIX systems it is generally "void *".  */
>> +#ifdef USE_WIN32API
>> +    res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
>> +#else
>>      res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len);
>> +#endif
> 
> You could have done exactly the opposite of the old comment, that is cast to
> (char *).  It shouldn't generate any warning in either case.  But I think that
> your way is fine too.

Oh, good point.  I always forget that implicit cast to void* works.

I'll update the patch.

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c
index ce40b61..507e0a1 100644
--- a/gdb/ser-tcp.c
+++ b/gdb/ser-tcp.c
@@ -280,10 +280,12 @@  net_open (struct serial *scb, const char *name)
 
     len = sizeof (err);
     /* On Windows, the fourth parameter to getsockopt is a "char *";
-       on UNIX systems it is generally "void *".  The cast to "void *"
-       is OK everywhere, since in C "void *" can be implicitly
-       converted to any pointer type.  */
+       on UNIX systems it is generally "void *".  */
+#ifdef USE_WIN32API
+    res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
+#else
     res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len);
+#endif
     if (res < 0 || err)
       {
 	/* Maybe the target still isn't ready to accept the connection.  */
@@ -339,16 +341,21 @@  net_close (struct serial *scb)
 int
 net_read_prim (struct serial *scb, size_t count)
 {
-  /* Need to cast to silence -Wpointer-sign on MinGW, as Winsock's
-     'recv' takes 'char *' as second argument, while 'scb->buf' is
-     'unsigned char *'.  */
+#ifdef USE_WIN32API
+  return recv (scb->fd, (char *) scb->buf, count, 0);
+#else
   return recv (scb->fd, (void *) scb->buf, count, 0);
+#endif
 }
 
 int
 net_write_prim (struct serial *scb, const void *buf, size_t count)
 {
+#ifdef USE_WIN32API
+  return send (scb->fd, (const char *) buf, count, 0);
+#else
   return send (scb->fd, buf, count, 0);
+#endif
 }
 
 int