Skip to content

Commit 35362af

Browse files
committed
Mirror cupsEnumDests change from 2.x to use cupsGetClock.
1 parent 5bf3203 commit 35362af

3 files changed

Lines changed: 29 additions & 37 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ libcups v3.0.0 (YYYY-MM-DD)
55
---------------------------
66

77
- Fixed return values of `ippDateToTime` when the timezone isn't GMT.
8+
- Fixed a potential timing issue with `cupsEnumDests`.
89

910

1011
libcups v3.0rc4 (2025-03-18)

cups/clock.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Monotonic clock API for CUPS.
33
//
4-
// Copyright © 2024 by OpenPrinting.
4+
// Copyright © 2024-2025 by OpenPrinting.
55
//
66
// Licensed under Apache License v2.0. See the file "LICENSE" for more
77
// information.
@@ -20,9 +20,9 @@ static cups_mutex_t cups_clock_mutex = CUPS_MUTEX_INITIALIZER;
2020
#ifdef _WIN32
2121
static ULONGLONG cups_first_tick; // First tick count
2222
#else
23-
# ifdef CLOCK_MONOTONIC
23+
# if defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW)
2424
static struct timespec cups_first_clock;// First clock value
25-
# endif // CLOCK_MONOTONIC
25+
# endif // CLOCK_MONOTONIC || CLOCK_MONOTONIC_RAW
2626
static struct timeval cups_first_time; // First time value
2727
#endif // _WIN32
2828

@@ -44,9 +44,9 @@ cupsGetClock(void)
4444
#ifdef _WIN32
4545
ULONGLONG curtick; // Current tick count
4646
#else
47-
# ifdef CLOCK_MONOTONIC
47+
# if defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW)
4848
struct timespec curclock; // Current clock value
49-
# endif // CLOCK_MONOTONIC
49+
# endif // CLOCK_MONOTONIC || CLOCK_MONOTONIC_RAW
5050
struct timeval curtime; // Current time value
5151
#endif // _WIN32
5252

@@ -71,9 +71,13 @@ cupsGetClock(void)
7171
secs = 0.001 * (curtick - cups_first_tick);
7272

7373
#else
74-
# ifdef CLOCK_MONOTONIC
74+
# if defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW)
7575
// Get the current tick count in milliseconds...
76+
# ifdef CLOCK_MONOTONIC_RAW
77+
if (!clock_gettime(CLOCK_MONOTONIC_RAW, &curclock))
78+
# else
7679
if (!clock_gettime(CLOCK_MONOTONIC, &curclock))
80+
# endif // CLOCK_MONOTONIC_RAW
7781
{
7882
if (!cups_clock_init)
7983
{

cups/dest.c

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ typedef struct _cups_dnssd_device_s // Enumerated device
8787
typedef struct _cups_dnssd_resdata_s // Data for resolving URI
8888
{
8989
int *cancel; // Pointer to "cancel" variable
90-
struct timeval end_time; // Ending time
90+
double end_time; // Ending time
9191
} _cups_dnssd_resdata_t;
9292

9393
typedef struct _cups_getdata_s
@@ -129,7 +129,7 @@ static void cups_dest_query_cb(cups_dnssd_query_t *query, void *cb_data, cups_d
129129
static const char *cups_dest_resolve(cups_dest_t *dest, const char *uri, int msec, int *cancel, cups_dest_cb_t cb, void *user_data);
130130
static bool cups_dest_resolve_cb(void *context);
131131
static void cups_dnssd_unquote(char *dst, const char *src, size_t dstsize);
132-
static int cups_elapsed(struct timeval *t);
132+
static int cups_elapsed(double *t);
133133
static bool cups_enum_dests(http_t *http, unsigned flags, int msec, int *cancel, cups_ptype_t type, cups_ptype_t mask, cups_dest_cb_t cb, void *user_data);
134134
static size_t cups_find_dest(const char *name, const char *instance, size_t num_dests, cups_dest_t *dests, size_t prev, int *rdiff);
135135
static bool cups_get_cb(_cups_getdata_t *data, unsigned flags, cups_dest_t *dest);
@@ -2517,23 +2517,12 @@ cups_dest_resolve(
25172517

25182518

25192519
// Resolve the URI...
2520-
resolve.cancel = cancel;
2521-
gettimeofday(&resolve.end_time, NULL);
2520+
resolve.cancel = cancel;
2521+
resolve.end_time = cupsGetClock();
25222522
if (msec > 0)
2523-
{
2524-
resolve.end_time.tv_sec += msec / 1000;
2525-
resolve.end_time.tv_usec += (msec % 1000) * 1000;
2526-
2527-
while (resolve.end_time.tv_usec >= 1000000)
2528-
{
2529-
resolve.end_time.tv_sec ++;
2530-
resolve.end_time.tv_usec -= 1000000;
2531-
}
2532-
}
2523+
resolve.end_time += 0.001 * msec;
25332524
else
2534-
{
2535-
resolve.end_time.tv_sec += 75;
2536-
}
2525+
resolve.end_time += 75;
25372526

25382527
if (cb)
25392528
(*cb)(user_data, CUPS_DEST_FLAGS_UNCONNECTED | CUPS_DEST_FLAGS_RESOLVING, dest);
@@ -2564,7 +2553,7 @@ cups_dest_resolve_cb(void *context) // I - Resolve data
25642553
{
25652554
_cups_dnssd_resdata_t *resolve = (_cups_dnssd_resdata_t *)context;
25662555
// Resolve data
2567-
struct timeval curtime; // Current time
2556+
double curtime; // Current time
25682557

25692558

25702559
// If the cancel variable is set, return immediately.
@@ -2575,11 +2564,11 @@ cups_dest_resolve_cb(void *context) // I - Resolve data
25752564
}
25762565

25772566
// Otherwise check the end time...
2578-
gettimeofday(&curtime, NULL);
2567+
curtime = cupsGetClock();
25792568

2580-
DEBUG_printf("4cups_dest_resolve_cb: curtime=%d.%06d, end_time=%d.%06d", (int)curtime.tv_sec, (int)curtime.tv_usec, (int)resolve->end_time.tv_sec, (int)resolve->end_time.tv_usec);
2569+
DEBUG_printf("4cups_dest_resolve_cb: curtime=%.6f, end_time=%6f", curtime, resolve->end_time);
25812570

2582-
return (curtime.tv_sec < resolve->end_time.tv_sec || (curtime.tv_sec == resolve->end_time.tv_sec && curtime.tv_usec < resolve->end_time.tv_usec));
2571+
return (curtime < resolve->end_time);
25832572
}
25842573

25852574

@@ -2625,17 +2614,15 @@ cups_dnssd_unquote(char *dst, // I - Destination buffer
26252614
//
26262615

26272616
static int // O - Elapsed time in milliseconds
2628-
cups_elapsed(struct timeval *t) // IO - Previous time
2617+
cups_elapsed(double *t) // IO - Previous time
26292618
{
2630-
int msecs; // Milliseconds
2631-
struct timeval nt; // New time
2632-
2633-
2634-
gettimeofday(&nt, NULL);
2619+
int msecs; // Milliseconds
2620+
double nt; // New time
26352621

2636-
msecs = (int)(1000 * (nt.tv_sec - t->tv_sec) + (nt.tv_usec - t->tv_usec) / 1000);
26372622

2638-
*t = nt;
2623+
nt = cupsGetClock();
2624+
msecs = (int)(1000.0 * (nt - *t));
2625+
*t = nt;
26392626

26402627
return (msecs);
26412628
}
@@ -2667,7 +2654,7 @@ cups_enum_dests(
26672654
size_t count, // Number of queries started
26682655
completed; // Number of completed queries
26692656
int remaining; // Remainder of timeout
2670-
struct timeval curtime; // Current time
2657+
double curtime; // Current time
26712658
_cups_dnssd_data_t data; // Data for callback
26722659
_cups_dnssd_device_t *device; // Current device
26732660
cups_dnssd_t *dnssd = NULL; // DNS-SD context
@@ -2900,7 +2887,7 @@ cups_enum_dests(
29002887
else
29012888
remaining = msec;
29022889

2903-
gettimeofday(&curtime, NULL);
2890+
curtime = cupsGetClock();
29042891

29052892
while (remaining > 0 && (!cancel || !*cancel))
29062893
{

0 commit comments

Comments
 (0)