Remove dangerous mg_pfn_realloc(). Add %q printf specifier

This commit is contained in:
cpq 2022-08-10 18:27:58 +01:00
parent 24cb9467c0
commit 2ba3630de9
8 changed files with 65 additions and 96 deletions

View File

@ -2623,6 +2623,7 @@ Supported format specifiers:
- `hhu`, `hu`, `u`, `lu`, `llu` - same but for unsigned variants
- `hhx`, `hx`, `x`, `lx`, `llx` - same, unsigned and hex output
- `s` - expect `char *`
- `q` - expect `char *`, outputs JSON-escaped string (extension)
- `Q` - expect `char *`, outputs double-quoted JSON-escaped string (extension)
- `H` - expect `int`, `void *`, outputs double-quoted hex string (extension)
- `V` - expect `int`, `void *`, outputs double-quoted base64 string (extension)
@ -2736,28 +2737,6 @@ void myfn(char c, void *p);
size_t len = mg_rprintf(myfn, myfn_p, "Double quoted string: %Q!", "hi");
```
### mg\_pfn\_realloc()
```c
void mg_pfn_realloc(char ch, void *param);
```
Print a character to a malloced str
Parameters:
- `ch` - char to be printed
- `param` - Pointer to a `char *`. Memory for the buffer will be reallocated to fit the new char
Usage example:
```c
char *s = NULL;
mg_rprintf(mg_pfn_realloc, &s, "Hello, %s", world); // s == "Hello, world"
mg_rprintf(mg_pfn_realloc, &s, "!"); // s == "Hello, world!"
free(s);
```
### mg\_pfn\_iobuf()
```c
@ -2768,12 +2747,12 @@ Print a character to a [Generic IO buffer](#struct-mg_iobuf)
Parameters:
- `ch` - char to be printed
- `param` - pointer to a struct mg_iobuf
- `param` - must be `struct mg_iobuf *`
Usage example:
```c
mg_rprintf(mg_pfn_iobuf, &c->send, "hi!");
mg_rprintf(mg_pfn_iobuf, &c->send, "hi!"); // Append to the output buffer
```
### mg\_to64()

View File

@ -2795,11 +2795,11 @@ static const struct packed_file {
size_t size;
time_t mtime;
} packed_files[] = {
{"/web_root/index.html", v1, sizeof(v1), 1654437619},
{"/web_root/main.js", v2, sizeof(v2), 1656444283},
{"/web_root/preact.min.js", v3, sizeof(v3), 1652374364},
{"/web_root/style.css", v4, sizeof(v4), 1654709515},
{"/web_root/user.png", v5, sizeof(v5), 1626172939},
{"/web_root/index.html", v1, sizeof(v1), 1659483223},
{"/web_root/main.js", v2, sizeof(v2), 1659483223},
{"/web_root/preact.min.js", v3, sizeof(v3), 1659483223},
{"/web_root/style.css", v4, sizeof(v4), 1659483223},
{"/web_root/user.png", v5, sizeof(v5), 1659483223},
{NULL, NULL, 0, 0}
};

View File

@ -45,11 +45,11 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
} else if (ev == MG_EV_WS_MSG) {
// Got websocket frame. Received data is wm->data
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
char *resp = NULL;
struct mg_rpc_req r = {&s_rpc_head, 0, mg_pfn_realloc, &resp, 0, wm->data};
struct mg_iobuf io = {0, 0, 0, 512};
struct mg_rpc_req r = {&s_rpc_head, 0, mg_pfn_iobuf, &io, 0, wm->data};
mg_rpc_process(&r);
if (resp) mg_ws_send(c, resp, strlen(resp), WEBSOCKET_OP_TEXT);
free(resp);
if (io.buf) mg_ws_send(c, (char *) io.buf, io.len, WEBSOCKET_OP_TEXT);
mg_iobuf_free(&io);
}
(void) fn_data;
}

View File

@ -494,20 +494,6 @@ void mg_pfn_iobuf(char ch, void *param) {
}
}
// We don't use realloc() in mongoose, so resort to inefficient calloc
// Every new character reallocates the whole string
void mg_pfn_realloc(char ch, void *param) {
char *s, *buf = *(char **) param;
size_t len = buf == NULL ? 0 : strlen(buf);
if ((s = (char *) calloc(1, len + 2)) != NULL) {
if (buf != NULL) memcpy(s, buf, len);
s[len] = ch;
s[len + 1] = '\0';
free(buf);
*(char **) param = s;
}
}
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0};
size_t n = mg_vrprintf(mg_putchar_iobuf_static, &io, fmt, ap);
@ -697,8 +683,7 @@ static char mg_escape(int c) {
static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
size_t len) {
size_t i = 0, extra = 2;
out('"', ptr);
size_t i = 0, extra = 0;
for (i = 0; i < len && buf[i] != '\0'; i++) {
char c = mg_escape(buf[i]);
if (c) {
@ -707,10 +692,18 @@ static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
out(buf[i], ptr);
}
}
out('"', ptr);
return i + extra;
}
static size_t Qcpy(void (*out)(char, void *), void *ptr, char *buf,
size_t len) {
size_t n = 2;
out('"', ptr);
n += qcpy(out, ptr, buf, len);
out('"', ptr);
return n;
}
static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
size_t len) {
size_t i, n = 0;
@ -807,10 +800,11 @@ size_t mg_vrprintf(void (*out)(char, void *), void *param, const char *fmt,
size_t len = (size_t) va_arg(*ap, int);
uint8_t *buf = va_arg(*ap, uint8_t *);
n += bcpy(out, param, buf, len);
} else if (c == 's' || c == 'Q') {
} else if (c == 's' || c == 'Q' || c == 'q') {
char *p = va_arg(*ap, char *);
size_t (*f)(void (*)(char, void *), void *, char *, size_t) =
c == 's' ? scpy : qcpy;
size_t (*f)(void (*)(char, void *), void *, char *, size_t) = scpy;
if (c == 'Q') f = Qcpy;
if (c == 'q') f = qcpy;
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
for (j = 0; !minus && pr < w && j + pr < w; j++)
n += f(out, param, &pad, 1);

View File

@ -785,8 +785,7 @@ char *mg_remove_double_dots(char *s);
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
void mg_pfn_realloc(char ch, void *param); // Print to malloced str
void mg_pfn_iobuf(char ch, void *param); // Print to iobuf
void mg_pfn_iobuf(char ch, void *param); // iobuf printer
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex);
double mg_atod(const char *buf, int len, int *numlen);

View File

@ -82,20 +82,6 @@ void mg_pfn_iobuf(char ch, void *param) {
}
}
// We don't use realloc() in mongoose, so resort to inefficient calloc
// Every new character reallocates the whole string
void mg_pfn_realloc(char ch, void *param) {
char *s, *buf = *(char **) param;
size_t len = buf == NULL ? 0 : strlen(buf);
if ((s = (char *) calloc(1, len + 2)) != NULL) {
if (buf != NULL) memcpy(s, buf, len);
s[len] = ch;
s[len + 1] = '\0';
free(buf);
*(char **) param = s;
}
}
size_t mg_vsnprintf(char *buf, size_t len, const char *fmt, va_list *ap) {
struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0};
size_t n = mg_vrprintf(mg_putchar_iobuf_static, &io, fmt, ap);
@ -285,8 +271,7 @@ static char mg_escape(int c) {
static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
size_t len) {
size_t i = 0, extra = 2;
out('"', ptr);
size_t i = 0, extra = 0;
for (i = 0; i < len && buf[i] != '\0'; i++) {
char c = mg_escape(buf[i]);
if (c) {
@ -295,10 +280,18 @@ static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
out(buf[i], ptr);
}
}
out('"', ptr);
return i + extra;
}
static size_t Qcpy(void (*out)(char, void *), void *ptr, char *buf,
size_t len) {
size_t n = 2;
out('"', ptr);
n += qcpy(out, ptr, buf, len);
out('"', ptr);
return n;
}
static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
size_t len) {
size_t i, n = 0;
@ -395,10 +388,11 @@ size_t mg_vrprintf(void (*out)(char, void *), void *param, const char *fmt,
size_t len = (size_t) va_arg(*ap, int);
uint8_t *buf = va_arg(*ap, uint8_t *);
n += bcpy(out, param, buf, len);
} else if (c == 's' || c == 'Q') {
} else if (c == 's' || c == 'Q' || c == 'q') {
char *p = va_arg(*ap, char *);
size_t (*f)(void (*)(char, void *), void *, char *, size_t) =
c == 's' ? scpy : qcpy;
size_t (*f)(void (*)(char, void *), void *, char *, size_t) = scpy;
if (c == 'Q') f = Qcpy;
if (c == 'q') f = qcpy;
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
for (j = 0; !minus && pr < w && j + pr < w; j++)
n += f(out, param, &pad, 1);

View File

@ -5,8 +5,7 @@
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
void mg_pfn_realloc(char ch, void *param); // Print to malloced str
void mg_pfn_iobuf(char ch, void *param); // Print to iobuf
void mg_pfn_iobuf(char ch, void *param); // iobuf printer
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex);
double mg_atod(const char *buf, int len, int *numlen);

View File

@ -1469,13 +1469,19 @@ static void test_str(void) {
// Non-standard formatting
{
char buf[100], *p;
char buf[100], *p = NULL;
struct mg_iobuf io = {0, 0, 0, 16};
const char *expected;
expected = "\"\"";
mg_snprintf(buf, sizeof(buf), "%Q", "");
ASSERT(strcmp(buf, expected) == 0);
expected = "\"hi, \\\"\"";
mg_snprintf(buf, sizeof(buf), "\"hi, %q\"", "\"");
MG_INFO(("[%s] [%s]", buf, expected));
ASSERT(strcmp(buf, expected) == 0);
expected = "\"a'b\"";
mg_snprintf(buf, sizeof(buf), "%Q", "a'b");
ASSERT(strcmp(buf, expected) == 0);
@ -1497,12 +1503,12 @@ static void test_str(void) {
ASSERT(strcmp(p, "[9876543210,7]") == 0);
free(p);
p = mg_mprintf("[%M", pf2, 10);
mg_rprintf(mg_pfn_realloc, &p, ",");
mg_rprintf(mg_pfn_realloc, &p, "%d]", 7);
printf("-> %s\n", p);
ASSERT(strcmp(p, "[9876543210,7]") == 0);
free(p);
mg_rprintf(mg_pfn_iobuf, &io, "[%M", pf2, 10);
mg_rprintf(mg_pfn_iobuf, &io, ",");
mg_rprintf(mg_pfn_iobuf, &io, "%d]", 7);
printf("-> %s\n", io.buf);
ASSERT(strcmp((char *) io.buf, "[9876543210,7]") == 0);
mg_iobuf_free(&io);
}
{
@ -2450,23 +2456,22 @@ static void test_json(void) {
static void test_rpc(void) {
struct mg_rpc *head = NULL;
char *s = NULL;
struct mg_rpc_req req = {&head, 0, mg_pfn_realloc, &s, 0, {0, 0}};
struct mg_iobuf io = {0, 0, 0, 256};
struct mg_rpc_req req = {&head, 0, mg_pfn_iobuf, &io, 0, {0, 0}};
mg_rpc_add(&head, mg_str("rpc.list"), mg_rpc_list, NULL);
{
req.frame = mg_str("{\"method\":\"rpc.list\"}");
mg_rpc_process(&req);
ASSERT(s == NULL);
ASSERT(io.buf == NULL);
}
{
const char *resp = "{\"id\":1,\"result\":[\"rpc.list\"]}";
req.frame = mg_str("{\"id\": 1,\"method\":\"rpc.list\"}");
mg_rpc_process(&req);
MG_INFO(("-> %s", s));
ASSERT(strcmp(s, resp) == 0);
free(s), s = NULL;
ASSERT(strcmp((char *) io.buf, resp) == 0);
mg_iobuf_free(&io);
}
{
@ -2475,18 +2480,17 @@ static void test_rpc(void) {
"found\"}}";
req.frame = mg_str("{\"id\": true,\"method\":\"foo\"}");
mg_rpc_process(&req);
MG_INFO(("-> %s", s));
ASSERT(strcmp(s, resp) == 0);
free(s), s = NULL;
// MG_INFO(("-> %s", io.buf));
ASSERT(strcmp((char *) io.buf, resp) == 0);
mg_iobuf_free(&io);
}
{
const char *resp = "{\"error\":{\"code\":-32700,\"message\":\"haha\"}}";
req.frame = mg_str("haha");
mg_rpc_process(&req);
MG_INFO(("-> %s", s));
ASSERT(strcmp(s, resp) == 0);
free(s), s = NULL;
ASSERT(strcmp((char *) io.buf, resp) == 0);
mg_iobuf_free(&io);
}
mg_rpc_del(&head, NULL);