Skip to content

Commit 8143d8f

Browse files
committed
badger/tests: more minor lint reduction in C code
based on reading cppcheck output
1 parent 6a58ede commit 8143d8f

File tree

8 files changed

+24
-34
lines changed

8 files changed

+24
-34
lines changed

badger/tests/crc32.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@ static uint32_t crc_table[256];
1111
/* Generate the CRC table. Must be called before calculating the CRC value. */
1212
void init_crc32(uint32_t poly)
1313
{
14-
uint32_t crc;
15-
int i, j;
16-
17-
for(i = 0; i < 256; i++) {
18-
crc = i;
19-
for(j = 8; j > 0; j--) {
20-
if(crc & 1) crc = (crc >> 1) ^ poly;
14+
for(unsigned i = 0; i < 256; i++) {
15+
uint32_t crc = i;
16+
for(int j = 8; j > 0; j--) {
17+
if (crc & 1) crc = (crc >> 1) ^ poly;
2118
else crc >>= 1;
2219
}
2320
crc_table[i] = crc;
2421
}
25-
2622
crc_table_init = 1;
2723
}
2824

badger/tests/crc_derive.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
static unsigned long crc_eval( unsigned int order, unsigned long poly, unsigned long crc,
1111
unsigned int width, unsigned long inword)
1212
{
13-
unsigned int bit, u;
1413
unsigned long mask_c = 1UL << (order-1);
1514
unsigned long mask_d = 1UL << (width-1);
16-
for (u=0; u<width; u++) {
17-
bit = ((crc&mask_c)!=0) ^ ((inword&mask_d)!=0);
15+
for (unsigned int u=0; u<width; u++) {
16+
unsigned int bit = ((crc&mask_c)!=0) ^ ((inword&mask_d)!=0);
1817
if (0) printf("u=%u crc=%lx inword=%lx bit=%u poly=%lx\n",
1918
u, crc, inword, bit, poly);
2019
inword = inword << 1;

badger/tests/crc_selfcheck.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99

1010
#define ETH_MAXLEN 1500 /* maximum line length */
1111

12-
struct pbuf {
13-
char buf[ETH_MAXLEN+12];
14-
int cur, len;
15-
};
16-
1712
static int ethernet_check(char *packet, unsigned len)
1813
{
1914
char *p=packet;
@@ -50,17 +45,16 @@ static int ethernet_check(char *packet, unsigned len)
5045
static unsigned int readmemh(FILE *f, char *buff, size_t avail)
5146
{
5247
size_t u;
53-
int rc;
5448
for (u=0; u<avail; u++) {
5549
unsigned int h;
56-
rc=fscanf(f, "%x", &h);
50+
int rc = fscanf(f, "%x", &h);
5751
if (rc!=1) break;
58-
buff[u]=h;
52+
buff[u] = h;
5953
}
6054
return u;
6155
}
6256

63-
int main(int argc, char *argv[])
57+
int main(int argc, const char *argv[])
6458
{
6559
FILE *f;
6660
unsigned int l;

badger/tests/ethernet_model.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int ethernet_model(int out_octet, int out_valid, int *in_octet, int *in_valid, i
6262
static int sleepctr=0;
6363
static int sleepmax=1;
6464
char in_txt[15], out_txt[15];
65-
int ethernet_model_debug = 0; /* adjustable */
65+
const int ethernet_model_debug = 0; /* adjustable */
6666

6767
if (out_valid) {
6868
sprintf(out_txt, "0x%2.2x", (unsigned int)(out_octet&0xff));

badger/tests/host_cmds_gen.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ int main(int argc, char *argv[])
4343
char idata[200];
4444
if (argc < 2) {
4545
fprintf(stderr, "Usage: %s mac_subset.gold\n", argv[0]);
46+
fprintf(stderr, " always writes to host_cmds.dat\n");
4647
return 1;
4748
}
4849
FILE *fin = fopen(argv[1], "r");
4950
if (!fin) {
50-
perror("mac_subset.gold");
51+
perror(argv[1]);
5152
return 1;
5253
}
5354
FILE *fout = fopen("host_cmds.dat", "w");
5455
if (!fout) {
5556
perror("host_cmds.dat");
57+
fclose(fin);
5658
return 1;
5759
}
5860
while (fgets(idata, sizeof(idata), fin)) {

badger/tests/pcap2v.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void print_stream(pcap_t *pbuf)
4444
total++;
4545
/* if (total > 80) break; */
4646
}
47-
printf("tests %d\n", total);
47+
printf("tests %u\n", total);
4848
}
4949

5050
int main(int argc, char *argv[])

badger/tests/udp_model.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ struct udp_state *udp_setup_r(unsigned short udp_port_, int badger_client_)
140140
void udp_receiver_r(struct udp_state *ust, int *in_octet, int *in_valid, int *in_count, int thinking)
141141
{
142142
int val = 0;
143-
int udp_model_debug = 0; /* adjustable */
143+
const int udp_model_debug = 0; /* adjustable */
144144
struct pbuf *inbuf = ust->inbuf; /* makes source look cleaner; maybe optimizes away */
145145

146146
if (inbuf->cur == inbuf->len) {
@@ -220,7 +220,7 @@ void udp_sender(int out_octet, int out_end)
220220
void udp_sender_r(struct udp_state *ust, int out_octet, int out_end)
221221
{
222222
struct pbuf *outbuf = ust->outbuf; /* makes source look cleaner; maybe optimizes away */
223-
int udp_model_debug = 0; /* adjustable */
223+
const int udp_model_debug = 0; /* adjustable */
224224
if (1) {
225225
if (udp_model_debug) fprintf(stderr, "Trying to write %2.2x\n", (unsigned) out_octet);
226226
outbuf->buf[outbuf->cur] = out_octet;

badger/tests/udprtx.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
#define LEN_MOD (981)
1414
#define LEN_F(x) (492+((x)*8)%LEN_MOD)
1515
#define NEXT(x) ((x*3+1)&0xff)
16-
static unsigned udp_handle(char *data, unsigned data_len)
16+
static unsigned udp_handle(const char *data, unsigned data_len)
1717
{
18-
unsigned u;
1918
unsigned key = data[0]&0xff;
2019
unsigned sta = key;
2120
unsigned want_len = LEN_F(key);
@@ -31,7 +30,7 @@ static unsigned udp_handle(char *data, unsigned data_len)
3130
printf("length not %u", want_len);
3231
fail++;
3332
}
34-
if (1) for (u=0; u<data_len; u++) {
33+
if (1) for (unsigned u=0; u<data_len; u++) {
3534
unsigned want_d = sta ^ (u&0xff);
3635
unsigned actual_d = data[u]&0xff;
3736
if (actual_d != want_d) {
@@ -70,25 +69,24 @@ static void primary_loop(int usd, unsigned npack, unsigned juggle)
7069
struct sockaddr sa_xmit;
7170
unsigned int sa_xmit_len;
7271
struct timeval to;
73-
int i, pack_len;
74-
int debug1=0;
72+
int pack_len;
73+
const int debug1=0;
7574
unsigned probes_sent=0, probes_recv=0, probes_fail=0;
7675
unsigned timeouts=0;
7776
static char incoming[1500];
7877
sa_xmit_len=sizeof sa_xmit;
7978
for (probes_sent=0; probes_sent<juggle; probes_sent++) {
8079
send_packet(usd, probes_sent);
8180
}
82-
to.tv_sec=0;
83-
to.tv_usec=0;
8481
for (;npack == 0 || probes_recv < npack;) {
82+
int i;
8583
FD_ZERO(&fds_r);
8684
FD_SET(usd,&fds_r);
8785
FD_ZERO(&fds_e);
8886
FD_SET(usd,&fds_e);
8987
to.tv_sec=0;
9088
to.tv_usec=10000;
91-
i=select(usd+1,&fds_r,NULL,&fds_e,&to);
89+
i = select(usd+1,&fds_r,NULL,&fds_e,&to);
9290
/* Wait on read or error */
9391
if (debug1) printf("select returns %d,", i);
9492
if ((i!=1)||(!FD_ISSET(usd,&fds_r))) {
@@ -123,6 +121,7 @@ static void primary_loop(int usd, unsigned npack, unsigned juggle)
123121
}
124122
to.tv_sec=0;
125123
to.tv_usec=0;
124+
/* Remember there's a continue statement above */
126125
}
127126
printf("%u packets sent, %u received, %u failed, %u timeouts\n",
128127
probes_sent, probes_recv, probes_fail, timeouts);
@@ -131,7 +130,7 @@ static void primary_loop(int usd, unsigned npack, unsigned juggle)
131130
static void stuff_net_addr(struct in_addr *p, char *hostname)
132131
{
133132
struct hostent *server;
134-
server=gethostbyname(hostname);
133+
server = gethostbyname(hostname);
135134
if (server == NULL) {
136135
herror(hostname);
137136
exit(1);

0 commit comments

Comments
 (0)