Skip to content

Commit 6d70a19

Browse files
committed
switch from hand formatting to clang-format
1 parent 3392981 commit 6d70a19

File tree

19 files changed

+1089
-973
lines changed

19 files changed

+1089
-973
lines changed

.clang-format

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
BasedOnStyle: LLVM
2+
IndentWidth: 4
3+
TabWidth: 4
4+
UseTab: Never
5+
AlignConsecutiveDeclarations:
6+
Enabled: true
7+
AcrossEmptyLines: true
8+
AcrossComments: true
9+
AlignArrayOfStructures: Right
10+
AlignConsecutiveMacros:
11+
Enabled: true
12+
AcrossEmptyLines: true
13+
BraceWrapping:
14+
AfterFunction: false
15+
AfterStruct: false
16+
AfterEnum: false
17+
AfterControlStatement: false
18+
BeforeElse: true
19+
BeforeWhile: true
20+
ColumnLimit: 120

.github/workflows/make.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ jobs:
1717
- name: Build
1818
shell: bash
1919
run: make -C native
20+
- name: Build
21+
shell: bash
22+
run: make -C check-format
2023
- name: Test
2124
shell: bash
2225
run: make -C native test

native/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ fuzz: $(ODIR)/fuzzmain
7171
mkdir -p test/fuzz_output test/fuzz_queue
7272
AFL_SKIP_CPUFREQ=1 afl-fuzz -i test/fuzz_input -o test/fuzz_output $(ODIR)/fuzzmain test/fuzz_queue -
7373

74+
check-format: $(DEPS) $(wildcard test/test*.c)
75+
clang-format --dry-run --Werror $^
7476

75-
.PHONY: clean grind coverage fuzz test install
77+
.PHONY: clean grind coverage fuzz test install check-format
7678

7779
test: $(tests_ok)
7880

native/buffer.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
#ifndef FILE_LIBBUFFER_SEEN
1616
#define FILE_LIBBUFFER_SEEN
1717

18-
19-
#include <stdlib.h>
2018
#include <stdio.h>
19+
#include <stdlib.h>
2120

2221
// Outputs non-printable characters as octal, which allows the resulting
2322
// string to be a valid C-string constant.
24-
void printbuf(char* c, int n) {
23+
void printbuf(char *c, int n) {
2524
printf("unsigned char* buf=\"");
2625
for (int i = 0; i < n; i++) {
27-
switch (c[i]) {
26+
switch (c[i]) {
2827

2928
case '\n':
3029
printf("\\n");
@@ -44,14 +43,13 @@ void printbuf(char* c, int n) {
4443
} else {
4544
printf("%c", c[i]);
4645
}
47-
break;
48-
}
46+
break;
47+
}
4948
}
5049
printf("\"\n");
5150
}
5251

53-
54-
char* formatbuf(char* buf, int sz) {
52+
char *formatbuf(char *buf, int sz) {
5553
// nicely format a buffer to hex/ascii with a length offset. each 16 bytes
5654
// or part thereof of the input take 76 bytes include newline character. caller
5755
// must free() the buffer.
@@ -62,24 +60,25 @@ char* formatbuf(char* buf, int sz) {
6260
// 00000030 69 63 65 90 00 00 28 41 ice···(A
6361
//
6462
// ----8--- -------------------------48--------------------- --------17-------\n
65-
const char* hexd = "0123456789abcdef";
66-
int lines = (sz + (16-1)) / 16; // sz/16 rounded up!
67-
if (lines == 0) lines++;
68-
int osz = lines * 76 + 1; // +trailing null
69-
char* r = malloc(osz);
63+
const char *hexd = "0123456789abcdef";
64+
int lines = (sz + (16 - 1)) / 16; // sz/16 rounded up!
65+
if (lines == 0)
66+
lines++;
67+
int osz = lines * 76 + 1; // +trailing null
68+
char *r = malloc(osz);
7069
for (int i = 0; i < lines; i++) {
7170
// this provides our trailing null!
72-
sprintf(r+i*76, "%08x %48s %17s\n", i*16, "", "");
73-
for (int j = i*16; j < sz && j < (i+1)*16; j++) {
71+
sprintf(r + i * 76, "%08x %48s %17s\n", i * 16, "", "");
72+
for (int j = i * 16; j < sz && j < (i + 1) * 16; j++) {
7473
int c = j % 16;
7574
int e = c > 7 ? 1 : 0;
76-
r[i*76+9 +c*3+e] = hexd[(buf[j] >> 4) & 0x0F]; // upper nibble
77-
r[i*76+10+c*3+e] = hexd[buf[j] & 0x0F]; // lower nibble
75+
r[i * 76 + 9 + c * 3 + e] = hexd[(buf[j] >> 4) & 0x0F]; // upper nibble
76+
r[i * 76 + 10 + c * 3 + e] = hexd[buf[j] & 0x0F]; // lower nibble
7877
// printable ascii?
7978
if (buf[j] >= 32 && buf[j] < 127) {
80-
r[i*76+9+49+c+e] = buf[j];
79+
r[i * 76 + 9 + 49 + c + e] = buf[j];
8180
} else {
82-
r[i*76+9+49+c+e] = '.';
81+
r[i * 76 + 9 + 49 + c + e] = '.';
8382
}
8483
}
8584
}

native/buffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#ifndef FILE_LIBBUFFER_SEEN
1616
#define FILE_LIBBUFFER_SEEN
1717

18-
void printbuf(char* c, int n);
19-
char* formatbuf(char* buf, int sz);
18+
void printbuf(char *c, int n);
19+
char *formatbuf(char *buf, int sz);
2020

2121
#endif

native/fuzzmain.c

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,30 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
1615
#define _GNU_SOURCE
1716

1817
#define __STDC_FORMAT_MACROS
1918
#include <inttypes.h>
2019

2120
#define KXVER 3
21+
#include "k.h"
22+
#include "libchronicle.h"
23+
#include "mock_k.h"
24+
#include "serdes_k.h"
25+
#include <ctype.h>
26+
#include <errno.h>
27+
#include <fcntl.h>
28+
#include <stdarg.h>
2229
#include <stdio.h>
2330
#include <stdlib.h>
24-
#include <errno.h>
2531
#include <string.h>
26-
#include <fcntl.h>
2732
#include <unistd.h>
28-
#include <stdarg.h>
29-
#include <ctype.h>
30-
#include "k.h"
31-
#include "mock_k.h"
32-
#include "serdes_k.h"
33-
#include "libchronicle.h"
3433

3534
// This is a stand-alone tool for replaying a queue for use with fuzzer
3635
int print_data = 0;
3736
int print_meta = 0;
3837

39-
int print_msg(void* ctx, uint64_t index, void* msg) {
38+
int print_msg(void *ctx, uint64_t index, void *msg) {
4039
K x = (K)msg;
4140
if (print_data) {
4241
printf("[%" PRIu64 "] KC ", index);
@@ -53,7 +52,9 @@ uint32_t xorshift128(uint32_t state[static 4]) {
5352
uint32_t s, t = state[3];
5453
t ^= t << 11;
5554
t ^= t >> 8;
56-
state[3] = state[2]; state[2] = state[1]; state[1] = s = state[0];
55+
state[3] = state[2];
56+
state[2] = state[1];
57+
state[1] = s = state[0];
5758
t ^= s;
5859
t ^= s >> 19;
5960
state[0] = t;
@@ -68,13 +69,13 @@ uint32_t xorshift128(uint32_t state[static 4]) {
6869
// Reported but unresolved https://www.mail-archive.com/[email protected]/msg209613.html
6970
static inline uint64_t rdtsc(void) {
7071
uint32_t a, d;
71-
__asm__ __volatile__("rdtscp" : "=a" (a), "=d" (d));
72-
return (((uint64_t) d << 32) | a);
72+
__asm__ __volatile__("rdtscp" : "=a"(a), "=d"(d));
73+
return (((uint64_t)d << 32) | a);
7374
}
74-
int c_mkstemp(char* pattern) {
75+
int c_mkstemp(char *pattern) {
7576
uint32_t xor_state[4];
7677
xor_state[0] = xor_state[1] = xor_state[2] = xor_state[3] = (uint32_t)rdtsc();
77-
char* rep = strstr(pattern, "XXXXXX");
78+
char *rep = strstr(pattern, "XXXXXX");
7879
if (rep) {
7980
for (int i = 0; i < 6; i++) {
8081
uint8_t incre = xorshift128(xor_state) & 0x1F;
@@ -93,11 +94,11 @@ int c_mkstemp(char* pattern) {
9394
int main(const int argc, char **argv) {
9495
int c;
9596
opterr = 0;
96-
int verboseflag = 0;
97-
FILE * fuzzfid = NULL;
97+
int verboseflag = 0;
98+
FILE *fuzzfid = NULL;
9899

99100
while ((c = getopt(argc, argv, "dmv:")) != -1)
100-
switch (c) {
101+
switch (c) {
101102
case 'd':
102103
print_data = 1;
103104
break;
@@ -113,7 +114,7 @@ int main(const int argc, char **argv) {
113114
default:
114115
fprintf(stderr, "%c??", c);
115116
exit(3);
116-
}
117+
}
117118

118119
if (optind + 2 > argc) {
119120
printf("Missing mandatory argument.\n Expected: %s [-d] [-m] [-v] QUEUE FUZZFILE\n", argv[0]);
@@ -128,68 +129,70 @@ int main(const int argc, char **argv) {
128129
printf(" the second `bytes` appends that many random bytes as a new entry to the queue\n");
129130
printf(" As the script is played, the random number generator seed, recieved index and byte count\n");
130131
printf(" are written to a log. This is then re-opened to verify the data in the queue matches.\n");
131-
132+
132133
exit(1);
133134
}
134135

135136
// mandatory arguments
136-
char* dir = argv[optind];
137-
char* fuzz = argv[optind+1];
137+
char *dir = argv[optind];
138+
char *fuzz = argv[optind + 1];
138139

139140
fuzzfid = stdin;
140141
if (strcmp(fuzz, "-") == 0) {
141142
fprintf(stderr, "fuzzing from stdin\n");
142143
} else {
143144
fuzzfid = fopen(fuzz, "r");
144-
if (fuzzfid == NULL) {
145+
if (fuzzfid == NULL) {
145146
fprintf(stderr, "Unable to open %s\n", fuzz);
146147
exit(4);
147148
}
148149
}
149150

150-
queue_t* queue = chronicle_init(dir);
151+
queue_t *queue = chronicle_init(dir);
151152
chronicle_set_decoder(queue, &parse_kx, &free_kx);
152153
chronicle_set_encoder(queue, &sizeof_kx, &append_kx);
153154
chronicle_set_version(queue, 5);
154155
chronicle_set_roll_scheme(queue, "FAST_HOURLY");
155156
chronicle_set_create(queue, 1);
156-
if (chronicle_open(queue) != 0) exit(-1);
157-
158-
tailer_t* tailer = chronicle_tailer(queue, print_msg, NULL, 0);
157+
if (chronicle_open(queue) != 0)
158+
exit(-1);
159+
160+
tailer_t *tailer = chronicle_tailer(queue, print_msg, NULL, 0);
159161
chronicle_peek_queue(queue);
160162

161163
if (verboseflag) {
162164
chronicle_debug();
163165
}
164-
165-
size_t linecap = 0;
166-
ssize_t linelen = 0;
167-
char *msgp = NULL;
168-
char *parsep = NULL;
169166

170-
int line = 0;
167+
size_t linecap = 0;
168+
ssize_t linelen = 0;
169+
char *msgp = NULL;
170+
char *parsep = NULL;
171+
172+
int line = 0;
171173
long long bytes;
172174
long long time;
173-
uint64_t clock = 0;
174-
uint64_t index = 0;
175+
uint64_t clock = 0;
176+
uint64_t index = 0;
175177

176-
uint32_t xor_state[4];
178+
uint32_t xor_state[4];
177179

178-
char* tmpfile = strdup("/tmp/shmmain.XXXXXX");
179-
int tmpfid = c_mkstemp(tmpfile);
180+
char *tmpfile = strdup("/tmp/shmmain.XXXXXX");
181+
int tmpfid = c_mkstemp(tmpfile);
180182
printf("logging fuzz expectations to %s fid %d msg %s\n", tmpfile, tmpfid, strerror(errno));
181-
FILE* tmp = fdopen(tmpfid, "w+");
183+
FILE *tmp = fdopen(tmpfid, "w+");
182184
printf(" tmp is %p %s\n", tmp, strerror(errno));
183185
while ((linelen = getline(&msgp, &linecap, fuzzfid)) > 0) {
184186
line++;
185187
parsep = msgp;
186188
time = strtoll(parsep, &parsep, 0);
187-
if (*parsep == ' ') parsep++;
189+
if (*parsep == ' ')
190+
parsep++;
188191
bytes = strtoll(parsep, &parsep, 0);
189192

190193
printf(" FUZ: %lld millis, %lld bytes\n", time, bytes);
191194
clock += time;
192-
xor_state[0] = xor_state[1] = xor_state[2] = xor_state[3] = (uint32_t)line+1;
195+
xor_state[0] = xor_state[1] = xor_state[2] = xor_state[3] = (uint32_t)line + 1;
193196
K x = ktn(KC, bytes);
194197
for (long long b = 0; b < bytes; b++) {
195198
kG(x)[b] = (uint8_t)xorshift128(xor_state);
@@ -199,11 +202,14 @@ int main(const int argc, char **argv) {
199202
r0(x);
200203
}
201204
// we're done parsing input, now replay checking using temp file
202-
//fclose(tmp);
203-
//close(tmpfid);
205+
// fclose(tmp);
206+
// close(tmpfid);
204207

205208
fflush(tmp);
206-
if (fseek(tmp, 0L, SEEK_SET) != 0) { printf("abort not fseek"); abort(); };
209+
if (fseek(tmp, 0L, SEEK_SET) != 0) {
210+
printf("abort not fseek");
211+
abort();
212+
};
207213

208214
int rline = 0;
209215
int r;
@@ -216,7 +222,7 @@ int main(const int argc, char **argv) {
216222

217223
// TODO: verify!!
218224
} else if (rline != line) {
219-
printf ("error, %s r=%d at line %d!\n\n", tmpfile, r, rline);
225+
printf("error, %s r=%d at line %d!\n\n", tmpfile, r, rline);
220226
}
221227
rline++;
222228
} while (r != EOF);
@@ -225,7 +231,8 @@ int main(const int argc, char **argv) {
225231

226232
// unlink(tmpfile);
227233
free(tmpfile);
228-
if (msgp) free(msgp);
234+
if (msgp)
235+
free(msgp);
229236
printf("parse finished\n");
230237

231238
exit(rline == line ? 0 : 5); // exit with fail

0 commit comments

Comments
 (0)