]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/common/addr_parsing.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / common / addr_parsing.c
index 4159dff67cebb8528d335937e1b57db63026eee4..0e183c667c2cdd09e150306a1718ed71fe9a7743 100644 (file)
 #include <netdb.h>
 
 #define BUF_SIZE 128
+#define ROUND_UP_128(x) (-(-(x) & -128))
 
-int safe_cat(char **pstr, int *plen, int pos, const char *str2)
+int safe_cat(char **pstr, int *plen, int pos, const char *src)
 {
-  int len2 = strlen(str2);
-
-  //printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2);
-  while (*plen < pos + len2 + 1) {
-    *plen += BUF_SIZE;
-
-    void *_realloc = realloc(*pstr, (size_t)*plen);
-
-    if (!_realloc) {
+  size_t len2 = strlen(src);
+  size_t new_size = pos + len2 + 1;
+  if (*plen < new_size) {
+    size_t round_up = ROUND_UP_128(new_size);
+    void* p = realloc(*pstr, round_up);
+    if (!p) {
       printf("Out of memory\n");
       exit(1);
     } else {
-      *pstr = _realloc;
+      *pstr = p;
     }
-    //printf("safe_cat '%s' max %d pos %d '%s' len %d\n", *pstr, *plen, pos, str2, len2);
   }
-
-  strncpy((*pstr)+pos, str2, len2);
-  (*pstr)[pos+len2] = '\0';
-
+  memcpy(*pstr + pos, src, len2 + 1);
   return pos + len2;
 }