avoid string overflows
[blktrace.git] / verify_blkparse.c
CommitLineData
42a58cd1
JA
1#include <stdio.h>
2#include <stdlib.h>
3#include <fcntl.h>
212421d8 4#include <string.h>
42a58cd1
JA
5#include <unistd.h>
6
ced8a9bf
JA
7#define MAX_CPUS (512)
8
42a58cd1
JA
9int main(int argc, char *argv[])
10{
11 double this_time, last_time;
212421d8 12 char line[256], last_line[256], *p;
b444507c 13 int major, minor, cpu, nr, alias;
fd8a68ba 14 unsigned long long total_entries;
b444507c 15 unsigned int last_seq[MAX_CPUS], seq;
42a58cd1
JA
16 FILE *f;
17
ced8a9bf
JA
18 for (nr = 0; nr < MAX_CPUS; nr++)
19 last_seq[nr] = -1;
20
42a58cd1
JA
21 if (argc < 2) {
22 fprintf(stderr, "%s: file\n", argv[0]);
23 return 1;
24 }
25
26 f = fopen(argv[1], "r");
27 if (!f) {
28 perror("fopen");
29 return 1;
30 }
31
32 last_time = 0;
8c0221bf 33 alias = nr = 0;
fd8a68ba 34 total_entries = 0;
42a58cd1 35 while ((p = fgets(line, sizeof(line), f)) != NULL) {
58261c75 36 if (sscanf(p, "%3d,%3d %2d %8d %lf", &major, &minor, &cpu, &seq, &this_time) != 5)
42a58cd1
JA
37 break;
38
cf4b68a5 39 if (this_time < last_time) {
212421d8
JA
40 fprintf(stdout, "last: %s", last_line);
41 fprintf(stdout, "this: %s", p);
cf4b68a5 42 nr++;
212421d8
JA
43 }
44
45 last_time = this_time;
63c7bd75 46
ced8a9bf
JA
47 if (cpu >= MAX_CPUS) {
48 fprintf(stderr, "cpu%d too large\n", cpu);
49 break;
50 }
51
52 if (last_seq[cpu] == seq) {
b99d1637 53 fprintf(stdout, "alias on sequence %u\n", seq);
63c7bd75 54 alias++;
b99d1637 55 }
63c7bd75 56
ced8a9bf 57 last_seq[cpu] = seq;
fd8a68ba 58 total_entries++;
212421d8 59 strcpy(last_line, line);
42a58cd1
JA
60 }
61
fd8a68ba 62 fprintf(stdout, "Events %Lu: %d unordered, %d aliases\n", total_entries, nr, alias);
42a58cd1 63 fclose(f);
cf4b68a5
JA
64
65 return nr != 0;
42a58cd1 66}