mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-27 15:01:03 +08:00
Remove dangerous mg_pfn_realloc(). Add %q printf specifier
This commit is contained in:
parent
24cb9467c0
commit
2ba3630de9
@ -2623,6 +2623,7 @@ Supported format specifiers:
|
|||||||
- `hhu`, `hu`, `u`, `lu`, `llu` - same but for unsigned variants
|
- `hhu`, `hu`, `u`, `lu`, `llu` - same but for unsigned variants
|
||||||
- `hhx`, `hx`, `x`, `lx`, `llx` - same, unsigned and hex output
|
- `hhx`, `hx`, `x`, `lx`, `llx` - same, unsigned and hex output
|
||||||
- `s` - expect `char *`
|
- `s` - expect `char *`
|
||||||
|
- `q` - expect `char *`, outputs JSON-escaped string (extension)
|
||||||
- `Q` - expect `char *`, outputs double-quoted JSON-escaped string (extension)
|
- `Q` - expect `char *`, outputs double-quoted JSON-escaped string (extension)
|
||||||
- `H` - expect `int`, `void *`, outputs double-quoted hex string (extension)
|
- `H` - expect `int`, `void *`, outputs double-quoted hex string (extension)
|
||||||
- `V` - expect `int`, `void *`, outputs double-quoted base64 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");
|
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()
|
### mg\_pfn\_iobuf()
|
||||||
|
|
||||||
```c
|
```c
|
||||||
@ -2768,12 +2747,12 @@ Print a character to a [Generic IO buffer](#struct-mg_iobuf)
|
|||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
- `ch` - char to be printed
|
- `ch` - char to be printed
|
||||||
- `param` - pointer to a struct mg_iobuf
|
- `param` - must be `struct mg_iobuf *`
|
||||||
|
|
||||||
Usage example:
|
Usage example:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
mg_rprintf(mg_pfn_iobuf, &c->send, "hi!");
|
mg_rprintf(mg_pfn_iobuf, &c->send, "hi!"); // Append to the output buffer
|
||||||
```
|
```
|
||||||
|
|
||||||
### mg\_to64()
|
### mg\_to64()
|
||||||
|
@ -2795,11 +2795,11 @@ static const struct packed_file {
|
|||||||
size_t size;
|
size_t size;
|
||||||
time_t mtime;
|
time_t mtime;
|
||||||
} packed_files[] = {
|
} packed_files[] = {
|
||||||
{"/web_root/index.html", v1, sizeof(v1), 1654437619},
|
{"/web_root/index.html", v1, sizeof(v1), 1659483223},
|
||||||
{"/web_root/main.js", v2, sizeof(v2), 1656444283},
|
{"/web_root/main.js", v2, sizeof(v2), 1659483223},
|
||||||
{"/web_root/preact.min.js", v3, sizeof(v3), 1652374364},
|
{"/web_root/preact.min.js", v3, sizeof(v3), 1659483223},
|
||||||
{"/web_root/style.css", v4, sizeof(v4), 1654709515},
|
{"/web_root/style.css", v4, sizeof(v4), 1659483223},
|
||||||
{"/web_root/user.png", v5, sizeof(v5), 1626172939},
|
{"/web_root/user.png", v5, sizeof(v5), 1659483223},
|
||||||
{NULL, NULL, 0, 0}
|
{NULL, NULL, 0, 0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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) {
|
} else if (ev == MG_EV_WS_MSG) {
|
||||||
// Got websocket frame. Received data is wm->data
|
// Got websocket frame. Received data is wm->data
|
||||||
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
|
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
|
||||||
char *resp = NULL;
|
struct mg_iobuf io = {0, 0, 0, 512};
|
||||||
struct mg_rpc_req r = {&s_rpc_head, 0, mg_pfn_realloc, &resp, 0, wm->data};
|
struct mg_rpc_req r = {&s_rpc_head, 0, mg_pfn_iobuf, &io, 0, wm->data};
|
||||||
mg_rpc_process(&r);
|
mg_rpc_process(&r);
|
||||||
if (resp) mg_ws_send(c, resp, strlen(resp), WEBSOCKET_OP_TEXT);
|
if (io.buf) mg_ws_send(c, (char *) io.buf, io.len, WEBSOCKET_OP_TEXT);
|
||||||
free(resp);
|
mg_iobuf_free(&io);
|
||||||
}
|
}
|
||||||
(void) fn_data;
|
(void) fn_data;
|
||||||
}
|
}
|
||||||
|
34
mongoose.c
34
mongoose.c
@ -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) {
|
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};
|
struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0};
|
||||||
size_t n = mg_vrprintf(mg_putchar_iobuf_static, &io, fmt, ap);
|
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,
|
static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
size_t i = 0, extra = 2;
|
size_t i = 0, extra = 0;
|
||||||
out('"', ptr);
|
|
||||||
for (i = 0; i < len && buf[i] != '\0'; i++) {
|
for (i = 0; i < len && buf[i] != '\0'; i++) {
|
||||||
char c = mg_escape(buf[i]);
|
char c = mg_escape(buf[i]);
|
||||||
if (c) {
|
if (c) {
|
||||||
@ -707,10 +692,18 @@ static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
|
|||||||
out(buf[i], ptr);
|
out(buf[i], ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out('"', ptr);
|
|
||||||
return i + extra;
|
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,
|
static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
size_t i, n = 0;
|
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);
|
size_t len = (size_t) va_arg(*ap, int);
|
||||||
uint8_t *buf = va_arg(*ap, uint8_t *);
|
uint8_t *buf = va_arg(*ap, uint8_t *);
|
||||||
n += bcpy(out, param, buf, len);
|
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 *);
|
char *p = va_arg(*ap, char *);
|
||||||
size_t (*f)(void (*)(char, void *), void *, char *, size_t) =
|
size_t (*f)(void (*)(char, void *), void *, char *, size_t) = scpy;
|
||||||
c == 's' ? scpy : qcpy;
|
if (c == 'Q') f = Qcpy;
|
||||||
|
if (c == 'q') f = qcpy;
|
||||||
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
|
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
|
||||||
for (j = 0; !minus && pr < w && j + pr < w; j++)
|
for (j = 0; !minus && pr < w && j + pr < w; j++)
|
||||||
n += f(out, param, &pad, 1);
|
n += f(out, param, &pad, 1);
|
||||||
|
@ -785,8 +785,7 @@ char *mg_remove_double_dots(char *s);
|
|||||||
|
|
||||||
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
|
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
|
||||||
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
|
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); // iobuf printer
|
||||||
void mg_pfn_iobuf(char ch, void *param); // Print to iobuf
|
|
||||||
|
|
||||||
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex);
|
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);
|
double mg_atod(const char *buf, int len, int *numlen);
|
||||||
|
34
src/fmt.c
34
src/fmt.c
@ -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) {
|
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};
|
struct mg_iobuf io = {(uint8_t *) buf, len, 0, 0};
|
||||||
size_t n = mg_vrprintf(mg_putchar_iobuf_static, &io, fmt, ap);
|
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,
|
static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
size_t i = 0, extra = 2;
|
size_t i = 0, extra = 0;
|
||||||
out('"', ptr);
|
|
||||||
for (i = 0; i < len && buf[i] != '\0'; i++) {
|
for (i = 0; i < len && buf[i] != '\0'; i++) {
|
||||||
char c = mg_escape(buf[i]);
|
char c = mg_escape(buf[i]);
|
||||||
if (c) {
|
if (c) {
|
||||||
@ -295,10 +280,18 @@ static size_t qcpy(void (*out)(char, void *), void *ptr, char *buf,
|
|||||||
out(buf[i], ptr);
|
out(buf[i], ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out('"', ptr);
|
|
||||||
return i + extra;
|
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,
|
static size_t bcpy(void (*out)(char, void *), void *ptr, uint8_t *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
size_t i, n = 0;
|
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);
|
size_t len = (size_t) va_arg(*ap, int);
|
||||||
uint8_t *buf = va_arg(*ap, uint8_t *);
|
uint8_t *buf = va_arg(*ap, uint8_t *);
|
||||||
n += bcpy(out, param, buf, len);
|
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 *);
|
char *p = va_arg(*ap, char *);
|
||||||
size_t (*f)(void (*)(char, void *), void *, char *, size_t) =
|
size_t (*f)(void (*)(char, void *), void *, char *, size_t) = scpy;
|
||||||
c == 's' ? scpy : qcpy;
|
if (c == 'Q') f = Qcpy;
|
||||||
|
if (c == 'q') f = qcpy;
|
||||||
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
|
if (pr == ~0U) pr = p == NULL ? 0 : strlen(p);
|
||||||
for (j = 0; !minus && pr < w && j + pr < w; j++)
|
for (j = 0; !minus && pr < w && j + pr < w; j++)
|
||||||
n += f(out, param, &pad, 1);
|
n += f(out, param, &pad, 1);
|
||||||
|
@ -5,8 +5,7 @@
|
|||||||
|
|
||||||
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
|
typedef void (*mg_pfn_t)(char, void *); // Custom putchar
|
||||||
typedef size_t (*mg_pm_t)(mg_pfn_t, void *, va_list *); // %M printer
|
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); // iobuf printer
|
||||||
void mg_pfn_iobuf(char ch, void *param); // Print to iobuf
|
|
||||||
|
|
||||||
size_t mg_lld(char *buf, int64_t val, bool is_signed, bool is_hex);
|
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);
|
double mg_atod(const char *buf, int len, int *numlen);
|
||||||
|
@ -1469,13 +1469,19 @@ static void test_str(void) {
|
|||||||
|
|
||||||
// Non-standard formatting
|
// Non-standard formatting
|
||||||
{
|
{
|
||||||
char buf[100], *p;
|
char buf[100], *p = NULL;
|
||||||
|
struct mg_iobuf io = {0, 0, 0, 16};
|
||||||
const char *expected;
|
const char *expected;
|
||||||
|
|
||||||
expected = "\"\"";
|
expected = "\"\"";
|
||||||
mg_snprintf(buf, sizeof(buf), "%Q", "");
|
mg_snprintf(buf, sizeof(buf), "%Q", "");
|
||||||
ASSERT(strcmp(buf, expected) == 0);
|
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\"";
|
expected = "\"a'b\"";
|
||||||
mg_snprintf(buf, sizeof(buf), "%Q", "a'b");
|
mg_snprintf(buf, sizeof(buf), "%Q", "a'b");
|
||||||
ASSERT(strcmp(buf, expected) == 0);
|
ASSERT(strcmp(buf, expected) == 0);
|
||||||
@ -1497,12 +1503,12 @@ static void test_str(void) {
|
|||||||
ASSERT(strcmp(p, "[9876543210,7]") == 0);
|
ASSERT(strcmp(p, "[9876543210,7]") == 0);
|
||||||
free(p);
|
free(p);
|
||||||
|
|
||||||
p = mg_mprintf("[%M", pf2, 10);
|
mg_rprintf(mg_pfn_iobuf, &io, "[%M", pf2, 10);
|
||||||
mg_rprintf(mg_pfn_realloc, &p, ",");
|
mg_rprintf(mg_pfn_iobuf, &io, ",");
|
||||||
mg_rprintf(mg_pfn_realloc, &p, "%d]", 7);
|
mg_rprintf(mg_pfn_iobuf, &io, "%d]", 7);
|
||||||
printf("-> %s\n", p);
|
printf("-> %s\n", io.buf);
|
||||||
ASSERT(strcmp(p, "[9876543210,7]") == 0);
|
ASSERT(strcmp((char *) io.buf, "[9876543210,7]") == 0);
|
||||||
free(p);
|
mg_iobuf_free(&io);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -2450,23 +2456,22 @@ static void test_json(void) {
|
|||||||
|
|
||||||
static void test_rpc(void) {
|
static void test_rpc(void) {
|
||||||
struct mg_rpc *head = NULL;
|
struct mg_rpc *head = NULL;
|
||||||
char *s = NULL;
|
struct mg_iobuf io = {0, 0, 0, 256};
|
||||||
struct mg_rpc_req req = {&head, 0, mg_pfn_realloc, &s, 0, {0, 0}};
|
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);
|
mg_rpc_add(&head, mg_str("rpc.list"), mg_rpc_list, NULL);
|
||||||
|
|
||||||
{
|
{
|
||||||
req.frame = mg_str("{\"method\":\"rpc.list\"}");
|
req.frame = mg_str("{\"method\":\"rpc.list\"}");
|
||||||
mg_rpc_process(&req);
|
mg_rpc_process(&req);
|
||||||
ASSERT(s == NULL);
|
ASSERT(io.buf == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const char *resp = "{\"id\":1,\"result\":[\"rpc.list\"]}";
|
const char *resp = "{\"id\":1,\"result\":[\"rpc.list\"]}";
|
||||||
req.frame = mg_str("{\"id\": 1,\"method\":\"rpc.list\"}");
|
req.frame = mg_str("{\"id\": 1,\"method\":\"rpc.list\"}");
|
||||||
mg_rpc_process(&req);
|
mg_rpc_process(&req);
|
||||||
MG_INFO(("-> %s", s));
|
ASSERT(strcmp((char *) io.buf, resp) == 0);
|
||||||
ASSERT(strcmp(s, resp) == 0);
|
mg_iobuf_free(&io);
|
||||||
free(s), s = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -2475,18 +2480,17 @@ static void test_rpc(void) {
|
|||||||
"found\"}}";
|
"found\"}}";
|
||||||
req.frame = mg_str("{\"id\": true,\"method\":\"foo\"}");
|
req.frame = mg_str("{\"id\": true,\"method\":\"foo\"}");
|
||||||
mg_rpc_process(&req);
|
mg_rpc_process(&req);
|
||||||
MG_INFO(("-> %s", s));
|
// MG_INFO(("-> %s", io.buf));
|
||||||
ASSERT(strcmp(s, resp) == 0);
|
ASSERT(strcmp((char *) io.buf, resp) == 0);
|
||||||
free(s), s = NULL;
|
mg_iobuf_free(&io);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const char *resp = "{\"error\":{\"code\":-32700,\"message\":\"haha\"}}";
|
const char *resp = "{\"error\":{\"code\":-32700,\"message\":\"haha\"}}";
|
||||||
req.frame = mg_str("haha");
|
req.frame = mg_str("haha");
|
||||||
mg_rpc_process(&req);
|
mg_rpc_process(&req);
|
||||||
MG_INFO(("-> %s", s));
|
ASSERT(strcmp((char *) io.buf, resp) == 0);
|
||||||
ASSERT(strcmp(s, resp) == 0);
|
mg_iobuf_free(&io);
|
||||||
free(s), s = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mg_rpc_del(&head, NULL);
|
mg_rpc_del(&head, NULL);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user