diff mbox series

[03/23] mongoose: Backport: bring mg_strdup() back from master

Message ID 20240612144010.470339-4-Michael.Glembotzki@iris-sensing.com
State Changes Requested
Headers show
Series Update Mongoose Version to 7.14 | expand

Commit Message

Michael Glembotzki June 12, 2024, 2:36 p.m. UTC
From: Michael Glembotzki <m.glembo@gmail.com>

mg_strdup was deprecated in 7.14, but was brought back in ab75bfa6.

mongoose rev-id: ab75bfa6e70a6985becf1eed912f93ba668be363

Signed-off-by: Michael Glembotzki <Michael.Glembotzki@iris-sensing.com>
---
 mongoose/mongoose.c | 20 ++++++++++++++++----
 mongoose/mongoose.h |  5 +++--
 2 files changed, 19 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/mongoose/mongoose.c b/mongoose/mongoose.c
index 1768bf70..44066893 100644
--- a/mongoose/mongoose.c
+++ b/mongoose/mongoose.c
@@ -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;
@@ -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) {
@@ -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) {
diff --git a/mongoose/mongoose.h b/mongoose/mongoose.h
index b59e2b06..6b71240f 100644
--- a/mongoose/mongoose.h
+++ b/mongoose/mongoose.h
@@ -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) {
@@ -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
@@ -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);