[Nut-upsdev] gcc4 compiler warnings

Arjen de Korte nut+devel at de-korte.org
Fri Feb 10 14:20:30 UTC 2006


> Gentlemen,
>
> I believe the functions that cause the most compiler warnings are
> functions such as:
>
> int ser_send_buf(int fd, const char *buf, size_t buflen);
> int ser_get_char(int fd, char *ch, long d_sec, long d_usec);
> int ser_get_line(int fd, char *buf, size_t buflen, char endchar,
>    const char *ignset, long d_sec, long d_usec);
> int ser_get_buf_len(int fd, char *buf, size_t buflen, long d_sec, long
> d_usec);
>
> etc. in drivers/serial.h.
>
> Changing the prototypes of these functions to use "unsigned char *buf"
> instead of "char *buf" will have _no effect whatsoever_ on the drivers
> that call there functions.

I just realized the same. But there is a better way to make the compiler
shut up about these warnings, by defining these buffers as 'void *'
instead of 'char *' like:

int ser_send_buf(int fd, const void *buf, size_t buflen);
int ser_get_char(int fd, void *ch, long d_sec, long d_usec);
int ser_get_line(int fd, void *buf, size_t buflen, char endchar,
   const void *ignset, long d_sec, long d_usec);
int ser_get_buf_len(int fd, void *buf, size_t buflen, long d_sec,
   long d_usec);

As far as I can see, there is only one function where this doesn't work,
ser_send_char() since this passes the character by value and not by
reference. And depending on what you put in 'endchar', this may yield a
similar problem in other fuctions.

> These functions do not perform any actual
> operations on characters, the only fill buffers with bytes.

Indeed.

>  For
> example, my driver contains code such as:
>
> {
>   unsigned char buf[MAXMSGSIZE];
>   ...
>   r = ser_send_buf(upsfd, buf, 6);
>   ...
> }
>
> Somebody else's driver might contain
>
> {
>   char buf[MAXMSGSIZE];
>   ...
>   r = ser_send_buf(upsfd, buf, 6);
>   ...
> }
>
> The behavior is completely unaffected by what ser_send_buf thinks the
> type of this pointer is; the only difference is in the compiler warnings.
> In this case, the second driver could turn off the warnings by doing:
>
>   r = ser_send_buf(upsfd, (unsigned char *)buf, 6);

Regards, Arjen



More information about the Nut-upsdev mailing list