Merge pull request #100 from painless-security/milestone/monitoring
[trust_router.git] / common / tr_name.c
index 84f0c1f..e1f81b4 100644 (file)
@@ -52,9 +52,11 @@ TR_NAME *tr_new_name (const char *name)
 {
   TR_NAME *new;
 
-  if (new = malloc(sizeof(TR_NAME))) {
-    new->len = strlen(name);
-    if (new->buf = malloc((new->len)+1)) {
+  new = malloc(sizeof(TR_NAME));
+  if (new) {
+    new->len = (int) strlen(name);
+    new->buf = malloc(1 + (size_t) new->len);
+    if (new->buf) {
       strcpy(new->buf, name);
     } else {
       free(new);
@@ -64,7 +66,7 @@ TR_NAME *tr_new_name (const char *name)
   return new;
 }
 
-TR_NAME *tr_dup_name (TR_NAME *from)
+TR_NAME *tr_dup_name (const TR_NAME *from)
 {
   TR_NAME *to;
 
@@ -74,15 +76,15 @@ TR_NAME *tr_dup_name (TR_NAME *from)
 
   if (NULL != (to = malloc(sizeof(TR_NAME)))) {
     to->len = from->len;
-    if (NULL != (to->buf = malloc(to->len+1))) {
-      strncpy(to->buf, from->buf, from->len);
+    if (NULL != (to->buf = malloc(1 + (size_t) to->len))) {
+      strncpy(to->buf, from->buf, (size_t) from->len);
       to->buf[to->len] = 0;    /* NULL terminate for debugging printf()s */
     }
   }
   return to;
 }
 
-int tr_name_cmp(TR_NAME *one, TR_NAME *two)
+int tr_name_cmp(const TR_NAME *one, const TR_NAME *two)
 {
   int len=one->len;
   int cmp=0;
@@ -90,7 +92,7 @@ int tr_name_cmp(TR_NAME *one, TR_NAME *two)
   if (two->len<one->len)
     len=two->len; /* len now min(one->len,two->len) */
 
-  cmp=strncmp(one->buf, two->buf, len);
+  cmp=strncmp(one->buf, two->buf, (size_t) len);
   if (cmp==0) {
     if (one->len<two->len)
       return -1;
@@ -109,7 +111,7 @@ int tr_name_cmp(TR_NAME *one, TR_NAME *two)
  * @param two_str Ordinary C null-terminated string
  * @return 0 on match, <0 if one precedes two, >0 if two precedes one
  */
-int tr_name_cmp_str(TR_NAME *one, const char *two_str)
+int tr_name_cmp_str(const TR_NAME *one, const char *two_str)
 {
   TR_NAME *two=tr_new_name(two_str);
   int cmp=tr_name_cmp(one, two);
@@ -126,7 +128,7 @@ int tr_name_cmp_str(TR_NAME *one, const char *two_str)
  * @return 1 if the the string (str) matches the wildcard string (wc_str), 0 if not.
  *
  */
-int tr_name_prefix_wildcard_match(TR_NAME *str, TR_NAME *wc_str)
+int tr_name_prefix_wildcard_match(const TR_NAME *str, const TR_NAME *wc_str)
 {
   const char *wc_post=NULL;
   size_t wc_len = 0;
@@ -134,7 +136,8 @@ int tr_name_prefix_wildcard_match(TR_NAME *str, TR_NAME *wc_str)
   if ((!str) || (!wc_str))
     return 0;
 
-  if (0 == (wc_len = wc_str->len))
+  wc_len = (size_t) wc_str->len;
+  if (wc_len == 0)
     return 0;
 
   if ('*' == wc_str->buf[0]) {
@@ -145,7 +148,7 @@ int tr_name_prefix_wildcard_match(TR_NAME *str, TR_NAME *wc_str)
     /* No wildcard, but the strings are the same length so may match.
      * Compare the full strings. */
     wc_post=wc_str->buf;
-    wc_len=wc_str->len;
+    wc_len = (size_t) wc_str->len;
   } else {
     /* No wildcard and strings are different length, so no match */
     return 0;
@@ -165,24 +168,25 @@ void tr_name_strlcat(char *dest, const TR_NAME *src, size_t len)
   size_t used_len;
   if (src->len >= len)
     used_len = len-1;
-  else used_len = src->len;
+  else
+    used_len = (size_t) src->len;
   if (used_len > 0)
     strncat(dest, src->buf, used_len);
   else dest[0] = '\0';
 }
 
 
-char * tr_name_strdup(TR_NAME *src)
+char * tr_name_strdup(const TR_NAME *src)
 {
-  char *s = calloc(src->len+1, 1);
+  char *s = calloc(1 + (size_t) src->len, 1);
   if (s) {
-    memcpy(s, src->buf, src->len);
+    memcpy(s, src->buf, (size_t) src->len);
     s[src->len] = '\0';
   }
   return s;
 }
 
-json_t *tr_name_to_json_string(TR_NAME *src)
+json_t *tr_name_to_json_string(const TR_NAME *src)
 {
   char *s=tr_name_strdup(src);
   json_t *js=json_string(s);
@@ -191,16 +195,16 @@ json_t *tr_name_to_json_string(TR_NAME *src)
   return js;
 }
 
-TR_NAME *tr_name_cat(TR_NAME *n1, TR_NAME *n2)
+TR_NAME *tr_name_cat(const TR_NAME *n1, const TR_NAME *n2)
 {
-  char *s=malloc(n1->len+n2->len+1);
+  char *s=malloc((size_t) n1->len + (size_t) n2->len + 1);
   TR_NAME *name=NULL;
 
   if (s==NULL)
     return NULL;
   *s=0;
-  strncat(s, n1->buf, n1->len);
-  strncat(s, n2->buf, n2->len);
+  strncat(s, n1->buf, (size_t) n1->len);
+  strncat(s, n2->buf, (size_t) n2->len);
   name=tr_new_name(s);
   free(s);
   return name;