Skip to content

Commit

Permalink
Merge pull request #2772 from cesanta/strdup
Browse files Browse the repository at this point in the history
bring mg_strdup() back
  • Loading branch information
scaprile authored May 31, 2024
2 parents a4e0225 + ab75bfa commit c1f630f
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 16 deletions.
20 changes: 16 additions & 4 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -6633,8 +6633,7 @@ void mg_rpc_add(struct mg_rpc **head, struct mg_str method,
void (*fn)(struct mg_rpc_req *), void *fn_data) {
struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc));
if (rpc != NULL) {
rpc->method.buf = mg_mprintf("%.*s", method.len, method.buf);
rpc->method.len = method.len;
rpc->method = mg_strdup(method);
rpc->fn = fn;
rpc->fn_data = fn_data;
rpc->next = *head, *head = rpc;
Expand Down Expand Up @@ -8048,6 +8047,20 @@ int mg_casecmp(const char *s1, const char *s2) {
return diff;
}

struct mg_str mg_strdup(const struct mg_str s) {
struct mg_str r = {NULL, 0};
if (s.len > 0 && s.buf != NULL) {
char *sc = (char *) calloc(1, s.len + 1);
if (sc != NULL) {
memcpy(sc, s.buf, s.len);
sc[s.len] = '\0';
r.buf = sc;
r.len = s.len;
}
}
return r;
}

int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
size_t i = 0;
while (i < str1.len && i < str2.len) {
Expand Down Expand Up @@ -10605,8 +10618,7 @@ static int mg_parse_pem(const struct mg_str pem, const struct mg_str label,
const char *c;
struct mg_str caps[5];
if (!mg_match(pem, mg_str("#-----BEGIN #-----#-----END #-----#"), caps)) {
der->buf = mg_mprintf("%.*s", pem.len, pem.buf);
der->len = pem.len;
*der = mg_strdup(pem);
return 0;
}
if (mg_strcmp(caps[1], label) != 0 || mg_strcmp(caps[3], label) != 0) {
Expand Down
5 changes: 3 additions & 2 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ extern "C" {
#define calloc(a, b) mg_calloc(a, b)
#define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a)
#define strdup(s) mg_mprintf("%s", s)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)

// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
static inline void *mg_calloc(size_t cnt, size_t size) {
Expand Down Expand Up @@ -288,7 +288,7 @@ extern uint32_t rt_time_get(void);
#include "cmsis_os2.h" // keep this include
#endif

#define strdup(s) mg_mprintf("%s", s)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)

#if defined(__ARMCC_VERSION)
#define mode_t size_t
Expand Down Expand Up @@ -861,6 +861,7 @@ struct mg_str mg_str_n(const char *s, size_t n);
int mg_casecmp(const char *s1, const char *s2);
int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2);
struct mg_str mg_strdup(const struct mg_str s);
bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
bool mg_span(struct mg_str s, struct mg_str *a, struct mg_str *b, char delim);

Expand Down
2 changes: 1 addition & 1 deletion src/arch_freertos.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define calloc(a, b) mg_calloc(a, b)
#define free(a) vPortFree(a)
#define malloc(a) pvPortMalloc(a)
#define strdup(s) mg_mprintf("%s", s)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)

// Re-route calloc/free to the FreeRTOS's functions, don't use stdlib
static inline void *mg_calloc(size_t cnt, size_t size) {
Expand Down
2 changes: 1 addition & 1 deletion src/arch_rtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extern uint32_t rt_time_get(void);
#include "cmsis_os2.h" // keep this include
#endif

#define strdup(s) mg_mprintf("%s", s)
#define strdup(s) ((char *) mg_strdup(mg_str(s)).buf)

#if defined(__ARMCC_VERSION)
#define mode_t size_t
Expand Down
3 changes: 1 addition & 2 deletions src/rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ void mg_rpc_add(struct mg_rpc **head, struct mg_str method,
void (*fn)(struct mg_rpc_req *), void *fn_data) {
struct mg_rpc *rpc = (struct mg_rpc *) calloc(1, sizeof(*rpc));
if (rpc != NULL) {
rpc->method.buf = mg_mprintf("%.*s", method.len, method.buf);
rpc->method.len = method.len;
rpc->method = mg_strdup(method);
rpc->fn = fn;
rpc->fn_data = fn_data;
rpc->next = *head, *head = rpc;
Expand Down
14 changes: 14 additions & 0 deletions src/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ int mg_casecmp(const char *s1, const char *s2) {
return diff;
}

struct mg_str mg_strdup(const struct mg_str s) {
struct mg_str r = {NULL, 0};
if (s.len > 0 && s.buf != NULL) {
char *sc = (char *) calloc(1, s.len + 1);
if (sc != NULL) {
memcpy(sc, s.buf, s.len);
sc[s.len] = '\0';
r.buf = sc;
r.len = s.len;
}
}
return r;
}

int mg_strcmp(const struct mg_str str1, const struct mg_str str2) {
size_t i = 0;
while (i < str1.len && i < str2.len) {
Expand Down
1 change: 1 addition & 0 deletions src/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct mg_str mg_str_n(const char *s, size_t n);
int mg_casecmp(const char *s1, const char *s2);
int mg_strcmp(const struct mg_str str1, const struct mg_str str2);
int mg_strcasecmp(const struct mg_str str1, const struct mg_str str2);
struct mg_str mg_strdup(const struct mg_str s);
bool mg_match(struct mg_str str, struct mg_str pattern, struct mg_str *caps);
bool mg_span(struct mg_str s, struct mg_str *a, struct mg_str *b, char delim);

Expand Down
3 changes: 1 addition & 2 deletions src/tls_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,7 @@ static int mg_parse_pem(const struct mg_str pem, const struct mg_str label,
const char *c;
struct mg_str caps[5];
if (!mg_match(pem, mg_str("#-----BEGIN #-----#-----END #-----#"), caps)) {
der->buf = mg_mprintf("%.*s", pem.len, pem.buf);
der->len = pem.len;
*der = mg_strdup(pem);
return 0;
}
if (mg_strcmp(caps[1], label) != 0 || mg_strcmp(caps[3], label) != 0) {
Expand Down
6 changes: 6 additions & 0 deletions test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,12 @@ static bool chkdbl(struct mg_str s, double val) {
}

static void test_str(void) {
{
struct mg_str s = mg_strdup(mg_str("a"));
ASSERT(mg_strcmp(s, mg_str("a")) == 0);
free((void *) s.buf);
}

{
const char *s;
struct mg_str a = mg_str("hello"), b = mg_str("a"), c = mg_str(NULL);
Expand Down
3 changes: 1 addition & 2 deletions tutorials/core/multi-threaded/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
// Multithreading code path
struct thread_data *data =
(struct thread_data *) calloc(1, sizeof(*data)); // Worker owns it
data->message.buf = mg_mprintf("%.*s", hm->message.len, hm->message.buf);
data->message.len = hm->message.len; // Pass message
data->message = mg_strdup(hm->message); // Pass message
data->conn_id = c->id;
data->mgr = c->mgr;
start_thread(thread_function, data); // Start thread and pass data
Expand Down
3 changes: 1 addition & 2 deletions tutorials/mqtt/mqtt-server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
while ((pos = mg_mqtt_next_sub(mm, &topic, &qos, pos)) > 0) {
struct sub *sub = calloc(1, sizeof(*sub));
sub->c = c;
sub->topic.buf = mg_mprintf("%.*s", topic.len, topic.buf);
sub->topic.len = topic.len;
sub->topic = mg_strdup(topic);
sub->qos = qos;
LIST_ADD_HEAD(struct sub, &s_subs, sub);
MG_INFO(
Expand Down

0 comments on commit c1f630f

Please sign in to comment.