mailing list of musl libc
 help / color / mirror / code / Atom feed
* RTC_RD_TIME
@ 2017-08-29 21:28 Jorge Almeida
  2017-08-30  1:43 ` RTC_RD_TIME Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Jorge Almeida @ 2017-08-29 21:28 UTC (permalink / raw)
  To: musl

A function to read the hardware clock (the battery powered thingy in
the motherboard):

(man 4 rtc)

void read_hw(struct rtc_time* rtctime){
   int rtcfd=open("/dev/rtc0" , O_RDONLY);
   ioctl(rtcfd, RTC_RD_TIME, rtctime);
   close(rtcfd);
}

(error checking omitted)

The problem:
warning: overflow in implicit constant conversion [-Woverflow]

(an arrow pointing to RTC_RD_TIME)

The same program compiles without warnings with glibc and dietlibc.

Headers:

#define _DEFAULT_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <time.h>
#include <linux/unistd.h>
#include <math.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/vt.h>
#include <linux/rtc.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>

TIA

Jorge Almeida


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: RTC_RD_TIME
  2017-08-29 21:28 RTC_RD_TIME Jorge Almeida
@ 2017-08-30  1:43 ` Rich Felker
  2017-08-30  6:35   ` RTC_RD_TIME Jorge Almeida
  2017-08-30 12:55   ` RTC_RD_TIME Alexander Monakov
  0 siblings, 2 replies; 6+ messages in thread
From: Rich Felker @ 2017-08-30  1:43 UTC (permalink / raw)
  To: musl

On Tue, Aug 29, 2017 at 10:28:14PM +0100, Jorge Almeida wrote:
> A function to read the hardware clock (the battery powered thingy in
> the motherboard):
> 
> (man 4 rtc)
> 
> void read_hw(struct rtc_time* rtctime){
>    int rtcfd=open("/dev/rtc0" , O_RDONLY);
>    ioctl(rtcfd, RTC_RD_TIME, rtctime);
>    close(rtcfd);
> }
> 
> (error checking omitted)
> 
> The problem:
> warning: overflow in implicit constant conversion [-Woverflow]
> 
> (an arrow pointing to RTC_RD_TIME)
> 
> The same program compiles without warnings with glibc and dietlibc.

I don't know any good fix. The problem is that the glibc (and others')
ioctl function has a signature mismatching the standard one, with an
unsigned argument instead of signed, and the macro values are outside
the range of signed int. Fortunately the warning is harmless but
obviously it can impact builds with -Werror or policy about warnings..

Rich


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: RTC_RD_TIME
  2017-08-30  1:43 ` RTC_RD_TIME Rich Felker
@ 2017-08-30  6:35   ` Jorge Almeida
  2017-08-30 12:55   ` RTC_RD_TIME Alexander Monakov
  1 sibling, 0 replies; 6+ messages in thread
From: Jorge Almeida @ 2017-08-30  6:35 UTC (permalink / raw)
  To: musl

On Wed, Aug 30, 2017 at 2:43 AM, Rich Felker <dalias@libc.org> wrote:
> On Tue, Aug 29, 2017 at 10:28:14PM +0100, Jorge Almeida wrote:

>>    ioctl(rtcfd, RTC_RD_TIME, rtctime);

>> The problem:
>> warning: overflow in implicit constant conversion [-Woverflow]
>>

>>
>> The same program compiles without warnings with glibc and dietlibc.
>

> the range of signed int. Fortunately the warning is harmless but

Good enough. Thanks.

Jorge


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: RTC_RD_TIME
  2017-08-30  1:43 ` RTC_RD_TIME Rich Felker
  2017-08-30  6:35   ` RTC_RD_TIME Jorge Almeida
@ 2017-08-30 12:55   ` Alexander Monakov
  2017-08-30 15:20     ` RTC_RD_TIME Jorge Almeida
  1 sibling, 1 reply; 6+ messages in thread
From: Alexander Monakov @ 2017-08-30 12:55 UTC (permalink / raw)
  To: musl

On Tue, 29 Aug 2017, Rich Felker wrote:
> I don't know any good fix. The problem is that the glibc (and others')
> ioctl function has a signature mismatching the standard one, with an
> unsigned argument instead of signed, and the macro values are outside
> the range of signed int. Fortunately the warning is harmless but
> obviously it can impact builds with -Werror or policy about warnings..

Type confusion causes musl to wrongly pass sign-extended value to the
kernel (instead of zero-extended). That's a real issue, and it's a bit
disappointing you have not mentioned it.

(BSD flavours as well as Linux have 'unsigned long' there, so I don't
understand why it was ok for POSIX to document request type as 'int';
wouldn't it be appropriate for musl to just follow existing practice
here rather than the letter of the standard?)

Without changing the prototype, I think a possible fix is

#ifdef __GNUC__
#define ioctl(fd, req, ...) ioctl(fd, (int)(req), ##__VA_ARGS__)
#endif

(plus a fix in ioctl.c for the sign extension issue)

although I don't understand how the quoted warning appears; I can
produce another one with -Wsign-conversion, but not this.

Alexander


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: RTC_RD_TIME
  2017-08-30 12:55   ` RTC_RD_TIME Alexander Monakov
@ 2017-08-30 15:20     ` Jorge Almeida
  2017-08-30 16:42       ` RTC_RD_TIME Alexander Monakov
  0 siblings, 1 reply; 6+ messages in thread
From: Jorge Almeida @ 2017-08-30 15:20 UTC (permalink / raw)
  To: musl

On Wed, Aug 30, 2017 at 1:55 PM, Alexander Monakov <amonakov@ispras.ru> wrote:


> although I don't understand how the quoted warning appears; I can
> produce another one with -Wsign-conversion, but not this.
>

CC=/opt/bin/musl-gcc -static
FLAGS=-Os -march=native -fomit-frame-pointer -pipe -Wall -pedantic
-falign-functions=1 -falign-jumps=1 -falign-loops=1 -fno-unwind-tables
-fdata-sections -ffunction-sections -Wl,--gc-sections
-fno-asynchronous-unwind-tables -std=c99

(CC) $(FLAGS) -o foo foo.c

Jorge


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: RTC_RD_TIME
  2017-08-30 15:20     ` RTC_RD_TIME Jorge Almeida
@ 2017-08-30 16:42       ` Alexander Monakov
  0 siblings, 0 replies; 6+ messages in thread
From: Alexander Monakov @ 2017-08-30 16:42 UTC (permalink / raw)
  To: musl

Thanks, that clears it up. It needs -pedantic and a 64-bit target.

void f(int);
void g()
{
  f(1u<<31|sizeof 0);
}

So casting sizes to 'int' or such in implementation of _IO[RW]* macros
would avoid this particular warning too (but not -Wsign-conversion).

Alexander


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-08-30 16:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-29 21:28 RTC_RD_TIME Jorge Almeida
2017-08-30  1:43 ` RTC_RD_TIME Rich Felker
2017-08-30  6:35   ` RTC_RD_TIME Jorge Almeida
2017-08-30 12:55   ` RTC_RD_TIME Alexander Monakov
2017-08-30 15:20     ` RTC_RD_TIME Jorge Almeida
2017-08-30 16:42       ` RTC_RD_TIME Alexander Monakov

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).