Commit 07636136 authored by John Stultz's avatar John Stultz Committed by Thomas Gleixner

selftests: timers: Fix valid-adjtimex signed left-shift undefined behavior

The struct adjtimex freq field takes a signed value who's units are in
shifted (<<16) parts-per-million.

Unfortunately for negative adjustments, the straightforward use of:

  freq = ppm << 16 trips undefined behavior warnings with clang:

valid-adjtimex.c:66:6: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
        -499<<16,
        ~~~~^
valid-adjtimex.c:67:6: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
        -450<<16,
        ~~~~^
..

Fix it by using a multiply by (1 << 16) instead of shifting negative values
in the valid-adjtimex test case. Align the values for better readability.
Reported-by: default avatarLee Jones <joneslee@google.com>
Reported-by: default avatarMuhammad Usama Anjum <usama.anjum@collabora.com>
Signed-off-by: default avatarJohn Stultz <jstultz@google.com>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Reviewed-by: default avatarMuhammad Usama Anjum <usama.anjum@collabora.com>
Link: https://lore.kernel.org/r/20240409202222.2830476-1-jstultz@google.com
Link: https://lore.kernel.org/lkml/0c6d4f0d-2064-4444-986b-1d1ed782135f@collabora.com/
parent 5284984a
...@@ -21,9 +21,6 @@ ...@@ -21,9 +21,6 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
...@@ -62,45 +59,47 @@ int clear_time_state(void) ...@@ -62,45 +59,47 @@ int clear_time_state(void)
#define NUM_FREQ_OUTOFRANGE 4 #define NUM_FREQ_OUTOFRANGE 4
#define NUM_FREQ_INVALID 2 #define NUM_FREQ_INVALID 2
#define SHIFTED_PPM (1 << 16)
long valid_freq[NUM_FREQ_VALID] = { long valid_freq[NUM_FREQ_VALID] = {
-499<<16, -499 * SHIFTED_PPM,
-450<<16, -450 * SHIFTED_PPM,
-400<<16, -400 * SHIFTED_PPM,
-350<<16, -350 * SHIFTED_PPM,
-300<<16, -300 * SHIFTED_PPM,
-250<<16, -250 * SHIFTED_PPM,
-200<<16, -200 * SHIFTED_PPM,
-150<<16, -150 * SHIFTED_PPM,
-100<<16, -100 * SHIFTED_PPM,
-75<<16, -75 * SHIFTED_PPM,
-50<<16, -50 * SHIFTED_PPM,
-25<<16, -25 * SHIFTED_PPM,
-10<<16, -10 * SHIFTED_PPM,
-5<<16, -5 * SHIFTED_PPM,
-1<<16, -1 * SHIFTED_PPM,
-1000, -1000,
1<<16, 1 * SHIFTED_PPM,
5<<16, 5 * SHIFTED_PPM,
10<<16, 10 * SHIFTED_PPM,
25<<16, 25 * SHIFTED_PPM,
50<<16, 50 * SHIFTED_PPM,
75<<16, 75 * SHIFTED_PPM,
100<<16, 100 * SHIFTED_PPM,
150<<16, 150 * SHIFTED_PPM,
200<<16, 200 * SHIFTED_PPM,
250<<16, 250 * SHIFTED_PPM,
300<<16, 300 * SHIFTED_PPM,
350<<16, 350 * SHIFTED_PPM,
400<<16, 400 * SHIFTED_PPM,
450<<16, 450 * SHIFTED_PPM,
499<<16, 499 * SHIFTED_PPM,
}; };
long outofrange_freq[NUM_FREQ_OUTOFRANGE] = { long outofrange_freq[NUM_FREQ_OUTOFRANGE] = {
-1000<<16, -1000 * SHIFTED_PPM,
-550<<16, -550 * SHIFTED_PPM,
550<<16, 550 * SHIFTED_PPM,
1000<<16, 1000 * SHIFTED_PPM,
}; };
#define LONG_MAX (~0UL>>1) #define LONG_MAX (~0UL>>1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment