Skip to content

Commit 0763613

Browse files
johnstultz-workKAGA-KOKO
authored andcommitted
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: Lee Jones <[email protected]> Reported-by: Muhammad Usama Anjum <[email protected]> Signed-off-by: John Stultz <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Muhammad Usama Anjum <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/lkml/[email protected]/
1 parent 5284984 commit 0763613

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

tools/testing/selftests/timers/valid-adjtimex.c

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2222
* GNU General Public License for more details.
2323
*/
24-
25-
26-
2724
#include <stdio.h>
2825
#include <stdlib.h>
2926
#include <time.h>
@@ -62,45 +59,47 @@ int clear_time_state(void)
6259
#define NUM_FREQ_OUTOFRANGE 4
6360
#define NUM_FREQ_INVALID 2
6461

62+
#define SHIFTED_PPM (1 << 16)
63+
6564
long valid_freq[NUM_FREQ_VALID] = {
66-
-499<<16,
67-
-450<<16,
68-
-400<<16,
69-
-350<<16,
70-
-300<<16,
71-
-250<<16,
72-
-200<<16,
73-
-150<<16,
74-
-100<<16,
75-
-75<<16,
76-
-50<<16,
77-
-25<<16,
78-
-10<<16,
79-
-5<<16,
80-
-1<<16,
65+
-499 * SHIFTED_PPM,
66+
-450 * SHIFTED_PPM,
67+
-400 * SHIFTED_PPM,
68+
-350 * SHIFTED_PPM,
69+
-300 * SHIFTED_PPM,
70+
-250 * SHIFTED_PPM,
71+
-200 * SHIFTED_PPM,
72+
-150 * SHIFTED_PPM,
73+
-100 * SHIFTED_PPM,
74+
-75 * SHIFTED_PPM,
75+
-50 * SHIFTED_PPM,
76+
-25 * SHIFTED_PPM,
77+
-10 * SHIFTED_PPM,
78+
-5 * SHIFTED_PPM,
79+
-1 * SHIFTED_PPM,
8180
-1000,
82-
1<<16,
83-
5<<16,
84-
10<<16,
85-
25<<16,
86-
50<<16,
87-
75<<16,
88-
100<<16,
89-
150<<16,
90-
200<<16,
91-
250<<16,
92-
300<<16,
93-
350<<16,
94-
400<<16,
95-
450<<16,
96-
499<<16,
81+
1 * SHIFTED_PPM,
82+
5 * SHIFTED_PPM,
83+
10 * SHIFTED_PPM,
84+
25 * SHIFTED_PPM,
85+
50 * SHIFTED_PPM,
86+
75 * SHIFTED_PPM,
87+
100 * SHIFTED_PPM,
88+
150 * SHIFTED_PPM,
89+
200 * SHIFTED_PPM,
90+
250 * SHIFTED_PPM,
91+
300 * SHIFTED_PPM,
92+
350 * SHIFTED_PPM,
93+
400 * SHIFTED_PPM,
94+
450 * SHIFTED_PPM,
95+
499 * SHIFTED_PPM,
9796
};
9897

9998
long outofrange_freq[NUM_FREQ_OUTOFRANGE] = {
100-
-1000<<16,
101-
-550<<16,
102-
550<<16,
103-
1000<<16,
99+
-1000 * SHIFTED_PPM,
100+
-550 * SHIFTED_PPM,
101+
550 * SHIFTED_PPM,
102+
1000 * SHIFTED_PPM,
104103
};
105104

106105
#define LONG_MAX (~0UL>>1)

0 commit comments

Comments
 (0)