Skip to content

Commit e194960

Browse files
improve code quality with compiler flags
1 parent ec019d9 commit e194960

3 files changed

Lines changed: 22 additions & 19 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
CC ?= cc
2-
CFLAGS ?= -O2 -Iinclude -Wall -Wextra -std=c99
2+
# Stronger warnings for code quality
3+
CFLAGS ?= -O2 -Iinclude -Wall -Wextra -Wpedantic -Wconversion -Wshadow \
4+
-Wcast-align -Wcast-qual -Wpointer-arith -Wformat=2 \
5+
-Wmissing-prototypes -Wstrict-prototypes -Wredundant-decls -Wundef \
6+
-std=c11
37
AR ?= ar
48

59
PREFIX ?= /usr/local

src/space_packet.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ size_t sp_packet_serialize(const sp_packet_t *pkt, uint8_t *buf,
8080

8181
if (crc_present) {
8282
/* compute CRC over secondary header and payload */
83-
size_t crc_area_len =
84-
(pkt->ph.sec_hdr_flag ? pkt->sec_hdr_len : 0) + pkt->payload_len;
83+
size_t crc_area_len = (pkt->ph.sec_hdr_flag ? (size_t)pkt->sec_hdr_len : (size_t)0) + (size_t)pkt->payload_len;
8584
uint16_t crc = sp_crc16_ccitt(&buf[6], crc_area_len);
8685
buf[off++] = (uint8_t)((crc >> 8) & 0xFF);
8786
buf[off++] = (uint8_t)(crc & 0xFF);
@@ -100,12 +99,12 @@ int sp_packet_parse(sp_packet_t *out, uint8_t *buf, size_t buf_len) {
10099
uint16_t second = ((uint16_t)buf[2] << 8) | buf[3];
101100
uint16_t length_field = ((uint16_t)buf[4] << 8) | buf[5];
102101

103-
out->ph.version = (first >> 13) & 0x7;
104-
out->ph.type = (first >> 12) & 0x1;
105-
out->ph.sec_hdr_flag = (first >> 11) & 0x1;
106-
out->ph.apid = first & 0x07FF;
107-
out->ph.seq_flags = (second >> 14) & 0x3;
108-
out->ph.seq_count = second & 0x3FFF;
102+
out->ph.version = (unsigned)((first >> 13) & 0x7);
103+
out->ph.type = (unsigned)((first >> 12) & 0x1);
104+
out->ph.sec_hdr_flag = (unsigned)((first >> 11) & 0x1);
105+
out->ph.apid = (unsigned short)(first & 0x07FF);
106+
out->ph.seq_flags = (unsigned)((second >> 14) & 0x3);
107+
out->ph.seq_count = (unsigned short)(second & 0x3FFF);
109108
out->ph.packet_length = length_field;
110109

111110
uint16_t total_len = (uint16_t)(length_field + 1);
@@ -194,12 +193,12 @@ void sp_set_primary_header(sp_packet_t *pkt,
194193
uint8_t seq_flags,
195194
uint16_t seq_count) {
196195
if (!pkt) return;
197-
pkt->ph.version = version & 0x7;
198-
pkt->ph.type = type & 0x1;
199-
pkt->ph.sec_hdr_flag = sec_hdr_flag ? 1 : 0;
200-
pkt->ph.apid = apid & 0x07FF;
201-
pkt->ph.seq_flags = seq_flags & 0x3;
202-
pkt->ph.seq_count = seq_count & 0x3FFF;
196+
pkt->ph.version = (unsigned)(version & 0x7);
197+
pkt->ph.type = (unsigned)(type & 0x1);
198+
pkt->ph.sec_hdr_flag = (unsigned)(sec_hdr_flag ? 1 : 0);
199+
pkt->ph.apid = (unsigned short)(apid & 0x07FF);
200+
pkt->ph.seq_flags = (unsigned)(seq_flags & 0x3);
201+
pkt->ph.seq_count = (unsigned short)(seq_count & 0x3FFF);
203202
}
204203

205204
void sp_set_secondary_header(sp_packet_t *pkt, const uint8_t *sec_hdr, uint16_t sec_hdr_len) {

tests/unit_tests.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdlib.h>
55
#include <string.h>
66

7-
int test_roundtrip_no_secheader(void) {
7+
static int test_roundtrip_no_secheader(void) {
88
uint8_t payload[] = {1, 2, 3, 4, 5};
99
sp_packet_t pkt = {0};
1010
pkt.ph.apid = 0x01;
@@ -39,7 +39,7 @@ int test_roundtrip_no_secheader(void) {
3939
return 0;
4040
}
4141

42-
int test_roundtrip_with_secheader_crc(void) {
42+
static int test_roundtrip_with_secheader_crc(void) {
4343
uint8_t payload[] = {10, 11, 12, 13};
4444
const uint8_t sec_hdr[] = {0x1, 0x0}; // flags LSB=1 -> CRC present
4545

@@ -77,7 +77,7 @@ int test_roundtrip_with_secheader_crc(void) {
7777
return 0;
7878
}
7979

80-
int test_crc_mismatch(void) {
80+
static int test_crc_mismatch(void) {
8181
uint8_t payload[] = {20, 21, 22};
8282
const uint8_t sec_hdr[] = {0x1, 0x0};
8383

@@ -111,7 +111,7 @@ int test_crc_mismatch(void) {
111111
return ok ? 1 : 0; // expect parse to fail (ok==0)
112112
}
113113

114-
int test_malformed_short_buffer(void) {
114+
static int test_malformed_short_buffer(void) {
115115
uint8_t tiny[] = {0x00, 0x01};
116116
sp_packet_t parsed;
117117
int ok = sp_packet_parse(&parsed, tiny, sizeof(tiny));

0 commit comments

Comments
 (0)