]> git.earman.xyz Git - sensor-watch.git/commitdiff
fix undefined behavior found by clang's sanitize
authorAlex Maestas <git@se30.xyz>
Thu, 7 Dec 2023 02:24:26 +0000 (02:24 +0000)
committerAlex Maestas <git@se30.xyz>
Thu, 7 Dec 2023 02:24:26 +0000 (02:24 +0000)
watch-library/hardware/watch/watch_rtc.c
watch-library/simulator/watch/watch_rtc.c

index 881e25753c7f05d63999fd81f8e196c9d51a639a..93cb9f1cfb6e30c9d67e66d26a61eda3d46afc04 100644 (file)
@@ -84,7 +84,7 @@ void watch_rtc_register_periodic_callback(ext_irq_cb_t callback, uint8_t frequen
     if (__builtin_popcount(frequency) != 1) return;
 
     // this left-justifies the period in a 32-bit integer.
-    uint32_t tmp = frequency << 24;
+    uint32_t tmp = (frequency & 0xFF) << 24;
     // now we can count the leading zeroes to get the value we need.
     // 0x01 (1 Hz) will have 7 leading zeros for PER7. 0xF0 (128 Hz) will have no leading zeroes for PER0.
     uint8_t per_n = __builtin_clz(tmp);
@@ -99,7 +99,7 @@ void watch_rtc_register_periodic_callback(ext_irq_cb_t callback, uint8_t frequen
 
 void watch_rtc_disable_periodic_callback(uint8_t frequency) {
     if (__builtin_popcount(frequency) != 1) return;
-    uint8_t per_n = __builtin_clz(frequency << 24);
+    uint8_t per_n = __builtin_clz((frequency & 0xFF) << 24);
     RTC->MODE2.INTENCLR.reg = 1 << per_n;
 }
 
index f6279eedb53d38e0821e186f5d4ff0f8a945dc9a..2bb6074c714ceda61f6c6e94fa01f77cbff6d194 100644 (file)
@@ -92,7 +92,7 @@ void watch_rtc_register_periodic_callback(ext_irq_cb_t callback, uint8_t frequen
     if (__builtin_popcount(frequency) != 1) return;
 
     // this left-justifies the period in a 32-bit integer.
-    uint32_t tmp = frequency << 24;
+    uint32_t tmp = (frequency & 0xFF) << 24;
     // now we can count the leading zeroes to get the value we need.
     // 0x01 (1 Hz) will have 7 leading zeros for PER7. 0xF0 (128 Hz) will have no leading zeroes for PER0.
     uint8_t per_n = __builtin_clz(tmp);
@@ -105,7 +105,7 @@ void watch_rtc_register_periodic_callback(ext_irq_cb_t callback, uint8_t frequen
 
 void watch_rtc_disable_periodic_callback(uint8_t frequency) {
     if (__builtin_popcount(frequency) != 1) return;
-    uint8_t per_n = __builtin_clz(frequency << 24);
+    uint8_t per_n = __builtin_clz((frequency & 0xFF) << 24);
     if (tick_callbacks[per_n] != -1) {
         emscripten_clear_interval(tick_callbacks[per_n]);
         tick_callbacks[per_n] = -1;