Merge branch 'jennifer/march2016-patches'
[trust_router.git] / common / tr_name.c
index 00757d4..dccf0d4 100644 (file)
@@ -43,15 +43,15 @@ void tr_free_name (TR_NAME *name)
     free (name->buf);
     name->buf = NULL;
   }
-  
+
   free(name);
 }
 
-TR_NAME *tr_new_name (char *name) 
+TR_NAME *tr_new_name (const char *name) 
 {
   TR_NAME *new;
 
-  if (new = malloc(sizeof(TR_NAME))) { 
+  if (new = malloc(sizeof(TR_NAME))) {
     new->len = strlen(name);
     if (new->buf = malloc((new->len)+1)) {
       strcpy(new->buf, name);
@@ -60,18 +60,21 @@ TR_NAME *tr_new_name (char *name)
   return new;
 }
 
-TR_NAME *tr_dup_name (TR_NAME *from) 
+TR_NAME *tr_dup_name (TR_NAME *from)
 {
   TR_NAME *to;
 
-  if (to = malloc(sizeof(TR_NAME))) {
+  if (!from) {
+    return NULL;
+  }
+
+  if (NULL != (to = malloc(sizeof(TR_NAME)))) {
     to->len = from->len;
-    if (to->buf = malloc(to->len+1)) {
+    if (NULL != (to->buf = malloc(to->len+1))) {
       strncpy(to->buf, from->buf, from->len);
       to->buf[to->len] = 0;    /* NULL terminate for debugging printf()s */
     }
   }
-
   return to;
 }
 
@@ -79,9 +82,10 @@ int tr_name_cmp(TR_NAME *one, TR_NAME *two)
 {
   if (one->len != two->len)
     return 1;
-  else 
-    /* TBD -- should really do a length-based comparison */
-    return strcmp(one->buf, two->buf);
+  else {
+    /* lengths equal */
+    return strncmp(one->buf, two->buf, one->len);
+  }
 }
 
 void tr_name_strlcat(char *dest, const TR_NAME *src, size_t len)
@@ -106,4 +110,4 @@ char * tr_name_strdup(TR_NAME *src)
   return s;
 }
 
-  
+