Skip to content

Commit 5a1ff12

Browse files
authored
Merge pull request #3292 from cesanta/tests
fixes for C89 (remove mg_queue_vprintf())
2 parents a205fd7 + ad58a48 commit 5a1ff12

File tree

6 files changed

+35
-43
lines changed

6 files changed

+35
-43
lines changed

mongoose.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5121,7 +5121,7 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
51215121
if ((pkt->ip->ver >> 4) != 4) return; // Not IP
51225122
ihl = pkt->ip->ver & 0x0F;
51235123
if (ihl < 5) return; // bad IHL
5124-
if (pkt->pay.len < (ihl * 4)) return; // Truncated / malformed
5124+
if (pkt->pay.len < (uint16_t)(ihl * 4)) return; // Truncated / malformed
51255125
// There can be link padding, take length from IP header
51265126
len = mg_ntohs(pkt->ip->len); // IP datagram length
51275127
if (len < (ihl * 4) || len > pkt->pay.len) return; // malformed
@@ -5166,7 +5166,7 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
51665166
pkt->tcp = (struct tcp *) (pkt->pay.buf);
51675167
if (pkt->pay.len < sizeof(*pkt->tcp)) return;
51685168
off = pkt->tcp->off >> 4; // account for opts
5169-
if (pkt->pay.len < (4 * off)) return;
5169+
if (pkt->pay.len < (uint16_t)(4 * off)) return;
51705170
mkpay(pkt, (uint32_t *) pkt->tcp + off);
51715171
MG_VERBOSE(("TCP %M:%hu -> %M:%hu len %u", mg_print_ip4, &pkt->ip->src,
51725172
mg_ntohs(pkt->tcp->sport), mg_print_ip4, &pkt->ip->dst,
@@ -7811,26 +7811,19 @@ bool mg_ota_end(void) {
78117811

78127812

78137813

7814-
size_t mg_queue_vprintf(struct mg_queue *q, const char *fmt, va_list *ap) {
7815-
va_list ap_copy;
7816-
va_copy(ap_copy, *ap);
7817-
size_t len = mg_vsnprintf(NULL, 0, fmt, &ap_copy);
7818-
char *buf;
7819-
if (len == 0 || mg_queue_book(q, &buf, len + 1) < len + 1) {
7820-
len = 0; // Nah. Not enough space
7821-
} else {
7822-
len = mg_vsnprintf((char *) buf, len + 1, fmt, ap);
7823-
mg_queue_add(q, len);
7824-
}
7825-
return len;
7826-
}
7827-
78287814
size_t mg_queue_printf(struct mg_queue *q, const char *fmt, ...) {
7829-
va_list ap;
7815+
char *buf;
78307816
size_t len;
7831-
va_start(ap, fmt);
7832-
len = mg_queue_vprintf(q, fmt, &ap);
7833-
va_end(ap);
7817+
va_list ap1, ap2;
7818+
va_start(ap1, fmt);
7819+
len = mg_vsnprintf(NULL, 0, fmt, &ap1);
7820+
va_end(ap1);
7821+
if (len == 0 || mg_queue_book(q, &buf, len + 1) < len + 1)
7822+
return 0; // Nah. Not enough space
7823+
va_start(ap2, fmt);
7824+
len = mg_vsnprintf(buf, len + 1, fmt, &ap2);
7825+
mg_queue_add(q, len);
7826+
va_end(ap2);
78347827
return len;
78357828
}
78367829

mongoose.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,6 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
12111211
size_t mg_snprintf(char *, size_t, const char *fmt, ...);
12121212
char *mg_vmprintf(const char *fmt, va_list *ap);
12131213
char *mg_mprintf(const char *fmt, ...);
1214-
size_t mg_queue_vprintf(struct mg_queue *, const char *fmt, va_list *);
12151214
size_t mg_queue_printf(struct mg_queue *, const char *fmt, ...);
12161215

12171216
// %M print helper functions

src/net_builtin.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
963963
if ((pkt->ip->ver >> 4) != 4) return; // Not IP
964964
ihl = pkt->ip->ver & 0x0F;
965965
if (ihl < 5) return; // bad IHL
966-
if (pkt->pay.len < (ihl * 4)) return; // Truncated / malformed
966+
if (pkt->pay.len < (uint16_t)(ihl * 4)) return; // Truncated / malformed
967967
// There can be link padding, take length from IP header
968968
len = mg_ntohs(pkt->ip->len); // IP datagram length
969969
if (len < (ihl * 4) || len > pkt->pay.len) return; // malformed
@@ -1008,7 +1008,7 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
10081008
pkt->tcp = (struct tcp *) (pkt->pay.buf);
10091009
if (pkt->pay.len < sizeof(*pkt->tcp)) return;
10101010
off = pkt->tcp->off >> 4; // account for opts
1011-
if (pkt->pay.len < (4 * off)) return;
1011+
if (pkt->pay.len < (uint16_t)(4 * off)) return;
10121012
mkpay(pkt, (uint32_t *) pkt->tcp + off);
10131013
MG_VERBOSE(("TCP %M:%hu -> %M:%hu len %u", mg_print_ip4, &pkt->ip->src,
10141014
mg_ntohs(pkt->tcp->sport), mg_print_ip4, &pkt->ip->dst,

src/printf.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22
#include "fmt.h"
33
#include "util.h"
44

5-
size_t mg_queue_vprintf(struct mg_queue *q, const char *fmt, va_list *ap) {
6-
char *buf;
7-
size_t len;
8-
va_list ap_copy;
9-
va_copy(ap_copy, *ap);
10-
len = mg_vsnprintf(NULL, 0, fmt, &ap_copy);
11-
if (len == 0 || mg_queue_book(q, &buf, len + 1) < len + 1) {
12-
len = 0; // Nah. Not enough space
13-
} else {
14-
len = mg_vsnprintf((char *) buf, len + 1, fmt, ap);
15-
mg_queue_add(q, len);
16-
}
17-
return len;
18-
}
19-
205
size_t mg_queue_printf(struct mg_queue *q, const char *fmt, ...) {
21-
va_list ap;
6+
char *buf;
227
size_t len;
23-
va_start(ap, fmt);
24-
len = mg_queue_vprintf(q, fmt, &ap);
25-
va_end(ap);
8+
va_list ap1, ap2;
9+
va_start(ap1, fmt);
10+
len = mg_vsnprintf(NULL, 0, fmt, &ap1);
11+
va_end(ap1);
12+
if (len == 0 || mg_queue_book(q, &buf, len + 1) < len + 1)
13+
return 0; // Nah. Not enough space
14+
va_start(ap2, fmt);
15+
len = mg_vsnprintf(buf, len + 1, fmt, &ap2);
16+
mg_queue_add(q, len);
17+
va_end(ap2);
2618
return len;
2719
}
2820

src/printf.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap);
99
size_t mg_snprintf(char *, size_t, const char *fmt, ...);
1010
char *mg_vmprintf(const char *fmt, va_list *ap);
1111
char *mg_mprintf(const char *fmt, ...);
12-
size_t mg_queue_vprintf(struct mg_queue *, const char *fmt, va_list *);
1312
size_t mg_queue_printf(struct mg_queue *, const char *fmt, ...);
1413

1514
// %M print helper functions

test/unit_test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,15 @@ static void test_str(void) {
22652265
ASSERT(sn("%s ", "a"));
22662266
ASSERT(sn("%s %s", "a", "b"));
22672267
ASSERT(sn("%2s %s", "a", "b"));
2268+
{ // mg_queue_printf()
2269+
struct mg_queue q;
2270+
char buf[128];
2271+
mg_queue_init(&q, buf, sizeof(buf));
2272+
void *p = (void *) 0xffffffffffffffff;
2273+
size_t len = mg_queue_printf(&q, "A%p%p%pB", p, p, p);
2274+
ASSERT(len == 56);
2275+
ASSERT(memcmp(buf + 4, "A0xffffffffffffffff0xffffffffffffffff0xffffffffffffffffB", 56) == 0);
2276+
}
22682277

22692278
// Non-standard formatting
22702279
{

0 commit comments

Comments
 (0)