Skip to content

Commit 725c173

Browse files
committed
fix printf with 64bit time_t
Printing long long with %ld is UB. Converting long long to long could overflow. To be compatible with 32bit time_t we need an explicit cast to long long.
1 parent 3fa681f commit 725c173

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,9 @@ This is a complete example program, similar to `ts_print_mt.c`:
585585
continue;
586586
#endif
587587

588-
printf("%ld.%06ld: (slot %d) %6d %6d %6d\n",
589-
samp_mt[j][i].tv.tv_sec,
590-
samp_mt[j][i].tv.tv_usec,
588+
printf("%lld.%06lld: (slot %d) %6d %6d %6d\n",
589+
(long long)samp_mt[j][i].tv.tv_sec,
590+
(long long)samp_mt[j][i].tv.tv_usec,
591591
samp_mt[j][i].slot,
592592
samp_mt[j][i].x,
593593
samp_mt[j][i].y,

plugins/input-evdev-raw.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,11 @@ static int ts_input_read(struct tslib_module_info *inf,
461461
samp->tv = ev.time;
462462
#ifdef DEBUG
463463
fprintf(stderr,
464-
"RAW nr %d ---------------------> %d %d %d %ld.%ld\n",
464+
"RAW nr %d ---------------------> %d %d %d %lld.%06lld\n",
465465
total,
466466
samp->x, samp->y, samp->pressure,
467-
(long)samp->tv.tv_sec,
468-
(long)samp->tv.tv_usec);
467+
(long long)samp->tv.tv_sec,
468+
(long long)samp->tv.tv_usec);
469469
#endif /* DEBUG */
470470
samp++;
471471
total++;
@@ -651,11 +651,11 @@ static int ts_input_read_mt(struct tslib_module_info *inf,
651651
}
652652

653653
#ifdef DEBUG
654-
printf("INPUT-RAW nr %d: read type %d code %3d value %4d time %ld.%ld\n",
654+
printf("INPUT-RAW nr %d: read type %d code %3d value %4d time %lld.%06lld\n",
655655
total,
656-
ev.type, ev.code,
657-
ev.value, (long)ev.time.tv_sec,
658-
(long)ev.time.tv_usec);
656+
ev.type, ev.code, ev.value,
657+
(long long)ev.time.tv_sec,
658+
(long long)ev.time.tv_usec);
659659
#endif
660660
switch (ev.type) {
661661
case EV_KEY:

plugins/input-raw.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ static int ts_input_read(struct tslib_module_info *inf,
393393
samp->tv.tv_usec = ev.input_event_usec;
394394
#ifdef DEBUG
395395
fprintf(stderr,
396-
"RAW---------------------> %d %d %d %ld.%ld\n",
396+
"RAW---------------------> %d %d %d %lld.%06lld\n",
397397
samp->x, samp->y, samp->pressure,
398-
(long)samp->tv.tv_sec,
399-
(long)samp->tv.tv_usec);
398+
(long long)samp->tv.tv_sec,
399+
(long long)samp->tv.tv_usec);
400400
#endif /* DEBUG */
401401
samp++;
402402
total++;
@@ -648,10 +648,10 @@ static int ts_input_read_mt(struct tslib_module_info *inf,
648648

649649
for (it = 0; it < rd / sizeof(struct input_event); it++) {
650650
#ifdef DEBUG
651-
printf("INPUT-RAW: read type %d code %3d value %4d time %ld.%ld\n",
651+
printf("INPUT-RAW: read type %d code %3d value %4d time %lld.%06lld\n",
652652
i->ev[it].type, i->ev[it].code,
653-
i->ev[it].value, (long)i->ev[it].input_event_sec,
654-
(long)i->ev[it].input_event_usec);
653+
i->ev[it].value, (long long)i->ev[it].input_event_sec,
654+
(long long)i->ev[it].input_event_usec);
655655
#endif
656656
switch (i->ev[it].type) {
657657
case EV_KEY:

tests/ts_print.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ int main(int argc, char **argv)
106106
if (ret != 1)
107107
continue;
108108

109-
printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec, samp.x, samp.y, samp.pressure);
109+
printf("%lld.%06lld: %6d %6d %6d\n", (long long)samp.tv.tv_sec, (long long)samp.tv.tv_usec, samp.x, samp.y, samp.pressure);
110110

111111
}
112112

tests/ts_print_mt.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ int main(int argc, char **argv)
241241
if (!(samp_mt[j][i].valid & TSLIB_MT_VALID))
242242
continue;
243243

244-
printf(YELLOW "sample %d - %ld.%06ld -" RESET " (slot %d) %6d %6d %6d\n",
244+
printf(YELLOW "sample %d - %lld.%06lld -" RESET " (slot %d) %6d %6d %6d\n",
245245
j,
246-
samp_mt[j][i].tv.tv_sec,
247-
samp_mt[j][i].tv.tv_usec,
246+
(long long)samp_mt[j][i].tv.tv_sec,
247+
(long long)samp_mt[j][i].tv.tv_usec,
248248
samp_mt[j][i].slot,
249249
samp_mt[j][i].x,
250250
samp_mt[j][i].y,

tests/ts_print_raw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int main(int argc, char **argv)
8383
if (ret != 1)
8484
continue;
8585

86-
printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec, samp.x, samp.y, samp.pressure);
86+
printf("%lld.%06lld: %6d %6d %6d\n", (long long)samp.tv.tv_sec, (long long)samp.tv.tv_usec, samp.x, samp.y, samp.pressure);
8787

8888
}
8989

tests/ts_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ int main(int argc, char **argv)
202202
quit_pressed = 1;
203203
}
204204

205-
printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec,
205+
printf("%lld.%06lld: %6d %6d %6d\n", (long long)samp.tv.tv_sec, (long long)samp.tv.tv_usec,
206206
samp.x, samp.y, samp.pressure);
207207

208208
if (samp.pressure > 0) {

tests/ts_test_mt_sdl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ int main(int argc, char **argv)
185185
samp_mt[0][i].x, samp_mt[0][i].y);
186186

187187
if (verbose) {
188-
printf("%ld.%06ld: (slot %d) %6d %6d %6d\n",
189-
samp_mt[0][i].tv.tv_sec,
190-
samp_mt[0][i].tv.tv_usec,
188+
printf("%lld.%06lld: (slot %d) %6d %6d %6d\n",
189+
(long long)samp_mt[0][i].tv.tv_sec,
190+
(long long)samp_mt[0][i].tv.tv_usec,
191191
samp_mt[0][i].slot,
192192
samp_mt[0][i].x,
193193
samp_mt[0][i].y,

0 commit comments

Comments
 (0)