Save the results of wmem_strndup(NULL, ...) in a non-const pointer.
authorGuy Harris <guy@alum.mit.edu>
Mon, 28 Dec 2015 23:01:21 +0000 (15:01 -0800)
committerGuy Harris <guy@alum.mit.edu>
Mon, 28 Dec 2015 23:01:48 +0000 (23:01 +0000)
That way, we have a non-const pointer to use when freeing it, and don't
have to undo the constification with a cast.

Rename "has_slash" to "slash", while we're at it, as it's not a Boolean
indicating whether the string has a slash, it's either a pointer to the
slash in question or NULL if the string has no slash.

Change-Id: Ia55b39bddb67c8ca71f7b09ee5eb82efaa3bdf0c
Reviewed-on: https://code.wireshark.org/review/12891
Reviewed-by: Guy Harris <guy@alum.mit.edu>
epan/ftypes/ftype-ipv4.c
epan/ftypes/ftype-ipv6.c

index 4ea6f27a5f7b25a33658e6727812fa4100fd75ba..3f0b6f77f3ad6a15cc9bfa8d0d5f0aeb69e05d46 100644 (file)
@@ -46,21 +46,21 @@ val_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_,
        guint32 addr;
        unsigned int nmask_bits;
 
-       const char *has_slash, *net_str;
-       char *addr_str;
+       const char *slash, *net_str;
+       const char *addr_str;
+       char *addr_str_to_free = NULL;
        fvalue_t *nmask_fvalue;
-       gboolean free_addr_str = FALSE;
 
        /* Look for CIDR: Is there a single slash in the string? */
-       has_slash = strchr(s, '/');
-       if (has_slash) {
+       slash = strchr(s, '/');
+       if (slash) {
                /* Make a copy of the string up to but not including the
                 * slash; that's the address portion. */
-               addr_str = wmem_strndup(NULL, s, has_slash - s);
-               free_addr_str = TRUE;
+               addr_str_to_free = wmem_strndup(NULL, s, slash - s);
+               addr_str = addr_str_to_free;
        }
        else {
-               addr_str = (char*)s;
+               addr_str = s;
        }
 
        if (!get_host_ipaddr(addr_str, &addr)) {
@@ -68,19 +68,19 @@ val_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_,
                        *err_msg = g_strdup_printf("\"%s\" is not a valid hostname or IPv4 address.",
                            addr_str);
                }
-               if (free_addr_str)
-                       wmem_free(NULL, addr_str);
+               if (addr_str_to_free)
+                       wmem_free(NULL, addr_str_to_free);
                return FALSE;
        }
 
-       if (free_addr_str)
-               wmem_free(NULL, addr_str);
+       if (addr_str_to_free)
+               wmem_free(NULL, addr_str_to_free);
        ipv4_addr_set_net_order_addr(&(fv->value.ipv4), addr);
 
        /* If CIDR, get netmask bits. */
-       if (has_slash) {
+       if (slash) {
                /* Skip past the slash */
-               net_str = has_slash + 1;
+               net_str = slash + 1;
 
                /* XXX - this is inefficient */
                nmask_fvalue = fvalue_from_unparsed(FT_UINT32, net_str, FALSE, err_msg);
index 34f619325bbc8a7096da54b9bb5548f57fe97eb8..e1093c6b67d5ce4396a5dea037886c5e0bd8848c 100644 (file)
@@ -36,35 +36,38 @@ ipv6_fvalue_set(fvalue_t *fv, const guint8 *value)
 static gboolean
 ipv6_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_, gchar **err_msg)
 {
-       const char *has_slash;
-       char *addr_str;
+       const char *slash;
+       const char *addr_str;
+       char *addr_str_to_free = NULL;
        unsigned int nmask_bits;
        fvalue_t *nmask_fvalue;
-       gboolean free_addr_str = FALSE;
 
        /* Look for prefix: Is there a single slash in the string? */
-       if ((has_slash = strchr(s, '/'))) {
-               addr_str = wmem_strndup(NULL, s, has_slash-s);
-               free_addr_str = TRUE;
+       slash = strchr(s, '/');
+       if (slash) {
+               /* Make a copy of the string up to but not including the
+                * slash; that's the address portion. */
+               addr_str_to_free = wmem_strndup(NULL, s, slash-s);
+               addr_str = addr_str_to_free;
        }
        else
-               addr_str = (char*)s;
+               addr_str = s;
 
        if (!get_host_ipaddr6(addr_str, &(fv->value.ipv6.addr))) {
                if (err_msg != NULL)
                        *err_msg = g_strdup_printf("\"%s\" is not a valid hostname or IPv6 address.", s);
-               if (free_addr_str)
-                       wmem_free(NULL, addr_str);
+               if (addr_str_to_free)
+                       wmem_free(NULL, addr_str_to_free);
                return FALSE;
        }
 
-       if (free_addr_str)
-               wmem_free(NULL, addr_str);
+       if (addr_str_to_free)
+               wmem_free(NULL, addr_str_to_free);
 
        /* If prefix */
-       if (has_slash) {
+       if (slash) {
                /* XXX - this is inefficient */
-               nmask_fvalue = fvalue_from_unparsed(FT_UINT32, has_slash+1, FALSE, err_msg);
+               nmask_fvalue = fvalue_from_unparsed(FT_UINT32, slash+1, FALSE, err_msg);
                if (!nmask_fvalue) {
                        return FALSE;
                }