Skip to content

Commit

Permalink
Cast when printing dispatch_unote_ident_t.
Browse files Browse the repository at this point in the history
Different platforms may define dispatch_unote_ident_t differently
and subsequently this type may have different bit widths. Therefore,
when printing du_ident, cast explicitly to deal with format mismatch
errors.

The alternative is to specify explicit format string macros, but
this is a little complex and casting is already being used in swiftlang#584.

One special case for consideration is `_dispatch_timer_unote_disarm`;
this gets the `du_ident` and converts that into a `uint32_t` timer
index. When `dispatch_unote_ident` is a `uint32_t` this is of course
fine, but needs consideration for when it isn't. Here, we just cast
down. This is somewhat reasonable since this is initialized from
`_dispatch_timer_unote_idx` which returns an `unsigned int`. (Of course,
we are not actually bounds checking that index, but that's outside the
scope of this commit).
  • Loading branch information
3405691582 committed Jul 18, 2022
1 parent 08ec7fd commit eedb747
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/event/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ _dispatch_timer_heap_update(dispatch_timer_heap_t dth,
#define _dispatch_timer_du_debug(what, du) \
_dispatch_debug("kevent-source[%p]: %s kevent[%p] { ident = 0x%llx }", \
_dispatch_wref2ptr((du)->du_owner_wref), what, \
(du), (unsigned long long)((du)->du_ident))
(du), (unsigned long long)(du)->du_ident)

DISPATCH_ALWAYS_INLINE
static inline unsigned int
Expand All @@ -792,7 +792,7 @@ static void
_dispatch_timer_unote_disarm(dispatch_timer_source_refs_t dt,
dispatch_timer_heap_t dth)
{
uint32_t tidx = dt->du_ident;
uint32_t tidx = (uint32_t)dt->du_ident;

dispatch_assert(_dispatch_unote_armed(dt));
_dispatch_timer_heap_remove(&dth[tidx], dt);
Expand Down
2 changes: 2 additions & 0 deletions src/event/event_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ _dispatch_timer_flags_from_clock(dispatch_clock_t clock)

#if defined(_WIN32)
typedef uintptr_t dispatch_unote_ident_t;
#elif defined(__OpenBSD__)
typedef uintptr_t dispatch_unote_ident_t;
#else
typedef uint32_t dispatch_unote_ident_t;
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/event/event_kevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ dispatch_kevent_debug(const char *verb, const dispatch_kevent_s *kev,

#define _dispatch_du_debug(what, du) \
_dispatch_debug("kevent-source[%p]: %s kevent[%p] " \
"{ filter = %s, ident = 0x%x }", \
"{ filter = %s, ident = 0x%llx }", \
_dispatch_wref2ptr((du)->du_owner_wref), what, \
(du), _evfiltstr((du)->du_filter), (du)->du_ident)
(du), _evfiltstr((du)->du_filter), (unsigned long long)(du)->du_ident)

#if DISPATCH_MACHPORT_DEBUG
#ifndef MACH_PORT_TYPE_SPREQUEST
Expand Down
6 changes: 3 additions & 3 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ _dispatch_bug_kevent_vanished(dispatch_unote_t du)
"{ %p[%s], ident: %" PRIdPTR " / 0x%" PRIxPTR ", handler: %p }",
dux_type(du._du)->dst_kind, dou._dq,
dou._dq->dq_label ? dou._dq->dq_label : "<unknown>",
du._du->du_ident, du._du->du_ident, func);
(intptr_t)du._du->du_ident, (uintptr_t)du._du->du_ident, func);
}

#endif // RDAR_49023449
Expand Down Expand Up @@ -1154,8 +1154,8 @@ _dispatch_logv_init(void *context DISPATCH_UNUSED)
}
#else
dprintf(dispatch_logfile, "=== log file opened for %s[%u] at "
"%ld.%06u ===\n", getprogname() ?: "", getpid(),
tv.tv_sec, (int)tv.tv_usec);
"%lld.%06u ===\n", getprogname() ?: "", getpid(),
(time_t)tv.tv_sec, (int)tv.tv_usec);
#endif
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -1402,8 +1402,7 @@ _dispatch_source_debug_attr(dispatch_source_t ds, char* buf, size_t bufsiz)
"mask = 0x%x, pending_data = 0x%llx, registered = %d, "
"armed = %d, %s%s%s",
target && target->dq_label ? target->dq_label : "", target,
(unsigned long long)dr->du_ident, dr->du_fflags,
(unsigned long long)dr->ds_pending_data,
(unsigned long long)dr->du_ident, dr->du_fflags, (unsigned long long)dr->ds_pending_data,
_du_state_registered(du_state), _du_state_armed(du_state),
(dqf & DSF_CANCELED) ? "cancelled, " : "",
(dqf & DSF_NEEDS_EVENT) ? "needs-event, " : "",
Expand Down

0 comments on commit eedb747

Please sign in to comment.