On 05/04/2025 19:45, Pádraig Brady wrote:
On 05/04/2025 19:02, Philip Rowlands wrote:
I was interested to see that the code makes uses of strtod, and how this 
interacts with the minute / hour / day suffix.

$ strace -e trace=clock_nanosleep sleep 3
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=3, tv_nsec=0},

$ strace -e trace=clock_nanosleep sleep 0xas
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=10, tv_nsec=0},

$ strace -e trace=clock_nanosleep sleep 0xam
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=600, tv_nsec=0},

$ strace -e trace=clock_nanosleep sleep 0xad
clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=173, tv_nsec=0},

Here 0xad is interpreted as 173 seconds, not 10 days.

It's a corner case, but should sleep(1) support or tolerate hex input, and if so, how 
should it handle the "d" suffix? FWIW I'd just vote to return an error for hex 
intervals.

Good point :)
Given FreeBSD and ksh sleep implementations also behave like GNU,
we should probably just document the edge case that
(day) suffixes are best avoided with hex inputs.

    ~$ ksh
    $ type sleep
    sleep is a shell builtin
    $ time sleep 0x10d
    real        4m29.00s

I'll apply the attached later to document this.

cheers,
Pádraig
From 2702266627e81c030d36942801b1d075a0399d3a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <p...@draigbrady.com>
Date: Sat, 5 Apr 2025 20:26:23 +0100
Subject: [PATCH] doc: mention the edge case of floating point durations with
 'd' suffix

* doc/coreutils.texi (sleep invocation): Mention that suffixes are
best avoided with hex arguments.
(timeout invocation): Likewise.
* tests/misc/sleep.sh: Ensure 'd' is not interpreted as "day".
---
 doc/coreutils.texi  | 7 +++++++
 tests/misc/sleep.sh | 1 +
 2 files changed, 8 insertions(+)

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index d6633f04c..2d1f162aa 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -18913,6 +18913,11 @@ C locale (@pxref{Floating point}) followed by an optional unit:
 @samp{h} for hours
 @samp{d} for days
 @end display
+@macro hexDuration
+Note it's best to avoid combining suffixes with hexadecimal arguments,
+as any @samp{d} will @emph{not} be interpreted as a suffix.
+@end macro
+@hexDuration
 A duration of 0 disables the associated timeout.
 The actual timeout duration is dependent on system conditions,
 which should be especially considered when specifying sub-second timeouts.
@@ -19089,6 +19094,8 @@ non-negative integer argument without a suffix, GNU @command{sleep}
 also accepts two or more arguments, unit suffixes, and floating-point
 numbers in either the current or the C locale.  @xref{Floating point}.
 
+@hexDuration
+
 For instance, the following could be used to @command{sleep} for
 1 second, 234 milli-, 567 micro- and 890 nanoseconds:
 
diff --git a/tests/misc/sleep.sh b/tests/misc/sleep.sh
index d1e3bc76c..6806a6fe2 100755
--- a/tests/misc/sleep.sh
+++ b/tests/misc/sleep.sh
@@ -32,6 +32,7 @@ returns_ 1 timeout 10 sleep || fail=1
 # subsecond actual sleep
 timeout 10 sleep 0.001 || fail=1
 timeout 10 sleep 0x.002p1 || fail=1
+timeout 10 sleep 0x0.01d || fail=1  # d is part of hex, not a day suffix
 
 # Using small timeouts for larger sleeps is racy,
 # but false positives should be avoided on most systems
-- 
2.48.1

Reply via email to