Skip to content

Commit aad7d91

Browse files
committed
equeue: avoid non-standard bit shifts
Shifting negative numbers right is implementation-defined, and shifting positive signed numbers left and exceeding positive range is undefined. Take care to make sure we always shift unsigned values.
1 parent 8637069 commit aad7d91

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

events/source/equeue.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ static inline int equeue_tickdiff(unsigned a, unsigned b)
3636
static inline int equeue_clampdiff(unsigned a, unsigned b)
3737
{
3838
int diff = equeue_tickdiff(a, b);
39-
return ~(diff >> (8 * sizeof(int) -1)) & diff;
39+
return diff > 0 ? diff : 0;
4040
}
4141

4242
// Increment the unique id in an event, hiding the event from cancel
4343
static inline void equeue_incid(equeue_t *q, struct equeue_event *e)
4444
{
4545
e->id += 1;
46-
if ((e->id << q->npw2) == 0) {
46+
if (((unsigned)e->id << q->npw2) == 0) {
4747
e->id = 1;
4848
}
4949
}
@@ -280,7 +280,7 @@ void equeue_enqueue(equeue_t *q, struct equeue_event *e, unsigned tick)
280280
static int equeue_event_id(equeue_t *q, struct equeue_event *e)
281281
{
282282
// setup event and hash local id with buffer offset for unique id
283-
return ((e->id << q->npw2) | ((unsigned char *)e - q->buffer));
283+
return ((unsigned)e->id << q->npw2) | ((unsigned char *)e - q->buffer);
284284
}
285285

286286
static struct equeue_event *equeue_unqueue_by_address(equeue_t *q, struct equeue_event *e)
@@ -319,10 +319,10 @@ static struct equeue_event *equeue_unqueue_by_id(equeue_t *q, int id)
319319
{
320320
// decode event from unique id and check that the local id matches
321321
struct equeue_event *e = (struct equeue_event *)
322-
&q->buffer[id & ((1 << q->npw2) - 1)];
322+
&q->buffer[id & ((1u << q->npw2) - 1u)];
323323

324324
equeue_mutex_lock(&q->queuelock);
325-
if (e->id != id >> q->npw2) {
325+
if (e->id != (unsigned)id >> q->npw2) {
326326
equeue_mutex_unlock(&q->queuelock);
327327
return 0;
328328
}
@@ -447,10 +447,10 @@ int equeue_timeleft(equeue_t *q, int id)
447447

448448
// decode event from unique id and check that the local id matches
449449
struct equeue_event *e = (struct equeue_event *)
450-
&q->buffer[id & ((1 << q->npw2) - 1)];
450+
&q->buffer[id & ((1u << q->npw2) - 1u)];
451451

452452
equeue_mutex_lock(&q->queuelock);
453-
if (e->id == id >> q->npw2) {
453+
if (e->id == (unsigned)id >> q->npw2) {
454454
ret = equeue_clampdiff(e->target, equeue_tick());
455455
}
456456
equeue_mutex_unlock(&q->queuelock);

0 commit comments

Comments
 (0)