public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Bug in TIME function
@ 2019-09-12 18:16 tlake
  2019-09-12 18:44 ` Kaz Kylheku
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: tlake @ 2019-09-12 18:16 UTC (permalink / raw)
  To: cygwin

The code below returns -1. It shouldn't.

 

#include <sys/times.h>

#include <stdio.h>

 

int main(int argc, char *argv[])

{

  printf("return value %ld\n", (long)times((struct tms*)0));

  return 0;

}

 


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

* Re: Bug in TIME function
  2019-09-12 18:16 Bug in TIME function tlake
@ 2019-09-12 18:44 ` Kaz Kylheku
  2019-09-13  0:50   ` Kaz Kylheku
  2019-09-12 23:03 ` Brian Inglis
  2019-09-16  7:30 ` Achim Gratz
  2 siblings, 1 reply; 9+ messages in thread
From: Kaz Kylheku @ 2019-09-12 18:44 UTC (permalink / raw)
  To: cygwin

On 2019-09-12 11:05, tlake@twcny.rr.com wrote:
> The code below returns -1. It shouldn't.

Says who?

I don't see anything in the specification which says that a null pointer 
argument is allowed:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/times.html

Passing a null pointer to an ISO C or POSIX library function results in 
undefined behavior,
except where it is documented otherwise.

GNU/Linux (specifically the Glibc implementation of libc) also doesn't
document any such extension (being able to pass a null pointer to 
times).

So even in light of the goal of Cygwin providing GNU/Linux compatibility
beyond POSIX, there is no justification for supporting times(0).

> #include <sys/times.h>
> #include <stdio.h>
> 
> int main(int argc, char *argv[])
> 
> {
>   printf("return value %ld\n", (long)times((struct tms*)0));

The pointer cast is not required here; you have a prototype of
the times function in scope; the equivalent times(0) will give you
the undefined behavior you're asking for.

>   return 0;
> }

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

* Re: Bug in TIME function
  2019-09-12 18:16 Bug in TIME function tlake
  2019-09-12 18:44 ` Kaz Kylheku
@ 2019-09-12 23:03 ` Brian Inglis
  2019-09-13 11:37   ` tlake
  2019-09-16  7:30 ` Achim Gratz
  2 siblings, 1 reply; 9+ messages in thread
From: Brian Inglis @ 2019-09-12 23:03 UTC (permalink / raw)
  To: cygwin

On 2019-09-12 12:05, tlake@twcny.rr.com wrote:
> The code below returns -1. It shouldn't.
> #include <sys/times.h>
> #include <stdio.h>
> int main(int argc, char *argv[])
> {
>   printf("return value %ld\n", (long)times((struct tms*)0));
>   return 0;
> }

It should.
The times(3) function requires a pointer to object storage to return the results
which are its function.
An invalid pointer to object storage is an error, requiring -1 be returned and
errno set.
You should also print errno and strerror(errno).

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

* Re: Bug in TIME function
  2019-09-12 18:44 ` Kaz Kylheku
@ 2019-09-13  0:50   ` Kaz Kylheku
  0 siblings, 0 replies; 9+ messages in thread
From: Kaz Kylheku @ 2019-09-13  0:50 UTC (permalink / raw)
  To: cygwin

On 2019-09-12 11:16, Kaz Kylheku wrote:
> So even in light of the goal of Cygwin providing GNU/Linux 
> compatibility
> beyond POSIX, there is no justification for supporting times(0).

What I wrote here is not true; on Linux, this is a system call, which
provides detection of some kinds of bad pointers, turning them into 
-1/EFAULT.




--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

* RE: Bug in TIME function
  2019-09-12 23:03 ` Brian Inglis
@ 2019-09-13 11:37   ` tlake
  2019-09-13 19:59     ` Wayne Davison
  0 siblings, 1 reply; 9+ messages in thread
From: tlake @ 2019-09-13 11:37 UTC (permalink / raw)
  To: cygwin

In Linux it returns a time value and return code of 0:

tom@LinuxPC:~/Downloads$ ./timetest
return value 10452
errno 0
 Success
tom@LinuxPC:~/Downloads$ ./timetest
return value 14048
errno 0
 Success

-----Original Message-----
From: Brian Inglis <Brian.Inglis@SystematicSw.ab.ca> 
Sent: Thursday, September 12, 2019 02:45 PM
To: cygwin@cygwin.com
Subject: Re: Bug in TIME function

On 2019-09-12 12:05, tlake@twcny.rr.com wrote:
> The code below returns -1. It shouldn't.
> #include <sys/times.h>
> #include <stdio.h>
> int main(int argc, char *argv[])
> {
>   printf("return value %ld\n", (long)times((struct tms*)0));
>   return 0;
> }

It should.
The times(3) function requires a pointer to object storage to return the results which are its function.
An invalid pointer to object storage is an error, requiring -1 be returned and errno set.
You should also print errno and strerror(errno).

--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains too much technical detail. Reader discretion is advised.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

* Re: Bug in TIME function
  2019-09-13 11:37   ` tlake
@ 2019-09-13 19:59     ` Wayne Davison
  2019-09-13 23:05       ` Kaz Kylheku
  0 siblings, 1 reply; 9+ messages in thread
From: Wayne Davison @ 2019-09-13 19:59 UTC (permalink / raw)
  To: cygwin

On Fri, Sep 13, 2019 at 4:27 AM wrote:
> In Linux [times()] returns a time value and return code of 0:

The Linux man page for times() mentions this special behavior, how it
isn't portable, and even advises against using the function:

"On Linux, the buf argument can be specified as NULL, with the result
that times() just returns a function result. However, POSIX does not
specify this behavior, and most other UNIX implementations require a
non-NULL value for buf."

"[...] a portable application would be wise to avoid using this value.
To measure changes in elapsed time, use clock_gettime(2)
instead."

One might argue that it would be nice to emulate the Linux behavior,
but it's not required by POSIX.

..wayne..

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

* Re: Bug in TIME function
  2019-09-13 19:59     ` Wayne Davison
@ 2019-09-13 23:05       ` Kaz Kylheku
  2019-09-18  0:16         ` Brian Inglis
  0 siblings, 1 reply; 9+ messages in thread
From: Kaz Kylheku @ 2019-09-13 23:05 UTC (permalink / raw)
  To: cygwin

On 2019-09-13 12:11, Wayne Davison wrote:
> On Fri, Sep 13, 2019 at 4:27 AM wrote:
>> In Linux [times()] returns a time value and return code of 0:
> 
> The Linux man page for times() mentions this special behavior, how it
> isn't portable, and even advises against using the function:
> 
> "On Linux, the buf argument can be specified as NULL, with the result
> that times() just returns a function result. However, POSIX does not
> specify this behavior, and most other UNIX implementations require a
> non-NULL value for buf."

Ah, so it is a documented extension in Linux, after all. In that case,
Cygwin should support it.

> One might argue that it would be nice to emulate the Linux behavior,
> but it's not required by POSIX.

Cygwin's explicit motto is "Get that Linux feeling --- on Windows";
and a tiny part of the Linux feeling is that times(NULL) works.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

* Re: Bug in TIME function
  2019-09-12 18:16 Bug in TIME function tlake
  2019-09-12 18:44 ` Kaz Kylheku
  2019-09-12 23:03 ` Brian Inglis
@ 2019-09-16  7:30 ` Achim Gratz
  2 siblings, 0 replies; 9+ messages in thread
From: Achim Gratz @ 2019-09-16  7:30 UTC (permalink / raw)
  To: cygwin

tlake@twcny.rr.com writes:
> The code below returns -1. It shouldn't.

This is hopefully fixed in the 3.1.0-05 test release of Cygwin.


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

SD adaptations for Waldorf Q V3.00R3 and Q+ V3.54R2:
http://Synth.Stromeko.net/Downloads.html#WaldorfSDada

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

* Re: Bug in TIME function
  2019-09-13 23:05       ` Kaz Kylheku
@ 2019-09-18  0:16         ` Brian Inglis
  0 siblings, 0 replies; 9+ messages in thread
From: Brian Inglis @ 2019-09-18  0:16 UTC (permalink / raw)
  To: cygwin

On 2019-09-13 16:14, Kaz Kylheku wrote:
> On 2019-09-13 12:11, Wayne Davison wrote:
>> On Fri, Sep 13, 2019 at 4:27 AM wrote:
>>> In Linux [times()] returns a time value and return code of 0:
>>
>> The Linux man page for times() mentions this special behavior, how it
>> isn't portable, and even advises against using the function:
>>
>> "On Linux, the buf argument can be specified as NULL, with the result
>> that times() just returns a function result. However, POSIX does not
>> specify this behavior, and most other UNIX implementations require a
>> non-NULL value for buf."
> 
> Ah, so it is a documented extension in Linux, after all. In that case,
> Cygwin should support it.
> 
>> One might argue that it would be nice to emulate the Linux behavior,
>> but it's not required by POSIX.
> 
> Cygwin's explicit motto is "Get that Linux feeling --- on Windows";
> and a tiny part of the Linux feeling is that times(NULL) works.

For functions returning data, -1/EFAULT should be considered a feature, except
if you only want some approximation of elapsed wall clock time from an arbitrary
epoch, which may overflow or change any time, and for which there are functions
better suited with differing resolutions.

For me, that return value is about the same as Cygwin uptime and net stats
workstation:

$ uptime
 14:10:56 up 68 days,  1:54,  3 users,  load average: 1.17, 1.12, 1.11
$ net stats workstation
"Workstation Statistics for \\...

Statistics since 2019-07-10 12:18:35
..."

which is a couple of minutes less than:

$ wmic os get LastBootUpTime
LastBootUpTime
20190710121614.432467-360

and the return value from clock_gettime(CLOCK_BOOTTIME,...).

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

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

end of thread, other threads:[~2019-09-16 20:18 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-12 18:16 Bug in TIME function tlake
2019-09-12 18:44 ` Kaz Kylheku
2019-09-13  0:50   ` Kaz Kylheku
2019-09-12 23:03 ` Brian Inglis
2019-09-13 11:37   ` tlake
2019-09-13 19:59     ` Wayne Davison
2019-09-13 23:05       ` Kaz Kylheku
2019-09-18  0:16         ` Brian Inglis
2019-09-16  7:30 ` Achim Gratz

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 read-only IMAP folder(s) and NNTP newsgroup(s).