Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 7d95ed6

Browse files
committed
lorapf: fix numeric overflow causing packages to be assumed from the future
1 parent 72ed70b commit 7d95ed6

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

esp32/pygate/lora_pkt_fwd/jitqueue.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ enum jit_error_e jit_enqueue(struct jit_queue_s *queue, struct timeval *time, st
229229
* Warning: unsigned arithmetic (handle roll-over)
230230
* t_packet < t_current + TX_START_DELAY + MARGIN
231231
*/
232-
if ((packet->count_us - time_us) <= (TX_START_DELAY + TX_MARGIN_DELAY + TX_JIT_DELAY)) {
232+
if (packet->count_us <= time_us + TX_START_DELAY + TX_MARGIN_DELAY + TX_JIT_DELAY) {
233233
MSG_DEBUG(DEBUG_JIT_ERROR, "ERROR: Packet REJECTED, already too late to send it (current=%u, packet=%u, type=%d)\n", time_us, packet->count_us, pkt_type);
234234
pthread_mutex_unlock(&mx_jit_queue);
235235
return JIT_ERROR_TOO_LATE;
@@ -243,12 +243,10 @@ enum jit_error_e jit_enqueue(struct jit_queue_s *queue, struct timeval *time, st
243243
* So let's define a safe delay above which we can say that the packet is out of bound: TX_MAX_ADVANCE_DELAY
244244
* Note: - Valid for Downlinks only, not for Beacon packets
245245
*
246-
* Warning: unsigned arithmetic (handle roll-over)
247-
t_packet > t_current + TX_MAX_ADVANCE_DELAY
248246
*/
249247
if ((pkt_type == JIT_PKT_TYPE_DOWNLINK_CLASS_A) || (pkt_type == JIT_PKT_TYPE_DOWNLINK_CLASS_B)) {
250-
if ((packet->count_us - time_us) > TX_MAX_ADVANCE_DELAY) {
251-
MSG_DEBUG(DEBUG_JIT_ERROR, "ERROR: Packet REJECTED, timestamp seems wrong, too much in advance (current=%u, packet=%u, type=%d)\n", time_us, packet->count_us, pkt_type);
248+
if (packet->count_us > time_us + TX_MAX_ADVANCE_DELAY) {
249+
MSG_DEBUG(DEBUG_JIT_ERROR, "ERROR: Packet REJECTED, timestamp seems wrong, too much in advance (current=%u, packet=%u, max=%f, type=%d)\n", time_us, packet->count_us, TX_MAX_ADVANCE_DELAY, pkt_type);
252250
pthread_mutex_unlock(&mx_jit_queue);
253251
return JIT_ERROR_TOO_EARLY;
254252
}
@@ -392,7 +390,7 @@ enum jit_error_e jit_peek(struct jit_queue_s *queue, struct timeval *time, int *
392390
* Warning: unsigned arithmetic
393391
* t_packet > t_current + TX_MAX_ADVANCE_DELAY
394392
*/
395-
if ((queue->nodes[i].pkt.count_us - time_us) >= TX_MAX_ADVANCE_DELAY) {
393+
if (queue->nodes[i].pkt.count_us >= time_us + TX_MAX_ADVANCE_DELAY) {
396394
/* We drop the packet to avoid lock-up */
397395
queue->num_pkt--;
398396
if (queue->nodes[i].pkt_type == JIT_PKT_TYPE_BEACON) {

0 commit comments

Comments
 (0)