]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
librpc: Simplify dcerpc_binding_string()
authorVolker Lendecke <vl@samba.org>
Sun, 24 Jan 2021 14:14:58 +0000 (15:14 +0100)
committerVolker Lendecke <vl@samba.org>
Thu, 28 Jan 2021 16:58:35 +0000 (16:58 +0000)
Make it follow a more conventional memory handling style for reallocs.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Samuel Cabrero <scabrero@samba.org>
librpc/rpc/binding.c

index 7e20966b121833d425dbc9bfabd62a9d68bc8a23..817498d8700c0fe62105d924cac88d6f0fd09655 100644 (file)
@@ -213,49 +213,50 @@ const char *epm_floor_string(TALLOC_CTX *mem_ctx, struct epm_floor *epm_floor)
 */
 _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_binding *b)
 {
-       char *s = talloc_strdup(mem_ctx, "");
-       char *o = s;
+       char *s = NULL;
+       char *tmp = NULL;
        size_t i;
        const char *t_name = NULL;
        bool option_section = false;
        const char *target_hostname = NULL;
 
+       s = talloc_strdup(mem_ctx, "");
+       if (s == NULL) {
+               goto nomem;
+       }
+
        if (b->transport != NCA_UNKNOWN) {
                t_name = derpc_transport_string_by_transport(b->transport);
                if (!t_name) {
-                       talloc_free(o);
-                       return NULL;
+                       goto nomem;
                }
        }
 
        if (!GUID_all_zero(&b->object)) {
                struct GUID_txt_buf buf;
 
-               o = s;
-               s = talloc_asprintf_append_buffer(
+               tmp = talloc_asprintf_append_buffer(
                        s, "%s@", GUID_buf_string(&b->object, &buf));
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
        if (t_name != NULL) {
-               o = s;
-               s = talloc_asprintf_append_buffer(s, "%s:", t_name);
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               tmp = talloc_asprintf_append_buffer(s, "%s:", t_name);
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
        if (b->host) {
-               o = s;
-               s = talloc_asprintf_append_buffer(s, "%s", b->host);
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               tmp = talloc_asprintf_append_buffer(s, "%s", b->host);
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
        target_hostname = b->target_hostname;
@@ -277,20 +278,18 @@ _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_bi
                return s;
        }
 
-       o = s;
-       s = talloc_asprintf_append_buffer(s, "[");
-       if (s == NULL) {
-               talloc_free(o);
-               return NULL;
+       tmp = talloc_asprintf_append_buffer(s, "[");
+       if (tmp == NULL) {
+               goto nomem;
        }
+       s = tmp;
 
        if (b->endpoint) {
-               o = s;
-               s = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               tmp = talloc_asprintf_append_buffer(s, "%s", b->endpoint);
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
        for (i=0;i<ARRAY_SIZE(ncacn_options);i++) {
@@ -298,61 +297,59 @@ _PUBLIC_ char *dcerpc_binding_string(TALLOC_CTX *mem_ctx, const struct dcerpc_bi
                        continue;
                }
 
-               o = s;
-               s = talloc_asprintf_append_buffer(s, ",%s", ncacn_options[i].name);
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               tmp = talloc_asprintf_append_buffer(
+                       s, ",%s", ncacn_options[i].name);
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
        if (target_hostname) {
-               o = s;
-               s = talloc_asprintf_append_buffer(s, ",target_hostname=%s",
-                                                 b->target_hostname);
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               tmp = talloc_asprintf_append_buffer(
+                       s, ",target_hostname=%s", b->target_hostname);
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
        if (b->target_principal) {
-               o = s;
-               s = talloc_asprintf_append_buffer(s, ",target_principal=%s",
-                                                 b->target_principal);
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               tmp = talloc_asprintf_append_buffer(
+                       s, ",target_principal=%s", b->target_principal);
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
        if (b->assoc_group_id != 0) {
-               o = s;
-               s = talloc_asprintf_append_buffer(s, ",assoc_group_id=0x%08x",
-                                                 b->assoc_group_id);
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               tmp = talloc_asprintf_append_buffer(
+                       s, ",assoc_group_id=0x%08x", b->assoc_group_id);
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
        for (i=0;b->options && b->options[i];i++) {
-               o = s;
-               s = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
-               if (s == NULL) {
-                       talloc_free(o);
-                       return NULL;
+               tmp  = talloc_asprintf_append_buffer(s, ",%s", b->options[i]);
+               if (tmp == NULL) {
+                       goto nomem;
                }
+               s = tmp;
        }
 
-       o = s;
-       s = talloc_asprintf_append_buffer(s, "]");
-       if (s == NULL) {
-               talloc_free(o);
-               return NULL;
+       tmp = talloc_asprintf_append_buffer(s, "]");
+       if (tmp == NULL) {
+               goto nomem;
        }
+       s = tmp;
 
        return s;
+nomem:
+       TALLOC_FREE(s);
+       return NULL;
 }
 
 /*