]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
C26: refactor to avoid modifying data via const pointer
authorStefan Metzmacher <metze@samba.org>
Mon, 30 Mar 2026 15:17:47 +0000 (17:17 +0200)
committerStefan Metzmacher <metze@samba.org>
Thu, 21 May 2026 17:25:33 +0000 (17:25 +0000)
Refactor code that assigns to a struct member, then calls strchr/strrchr on it
for in-place modification. Use a local mutable variable for modification, then
assign the final result to the struct member.

Pair-Programmed-With: Andreas Schneider <asn@samba.org>

BUG: https://bugzilla.samba.org/show_bug.cgi?id=16006

Signed-off-by: Andreas Schneider <asn@samba.org>
Signed-off-by: Stefan Metzmacher <metze@samba.org>
auth/credentials/credentials.c
librpc/rpc/binding.c
source3/lib/wins_srv.c
source3/printing/nt_printing.c
source4/dsdb/common/util.c
source4/dsdb/samdb/cracknames.c
source4/rpc_server/lsa/lsa_init.c

index 56ecacf8211793892069b679a1f109f2f43f4da4..63efdcd43cc1065da13fadfc5f0ae5ae270ac179 100644 (file)
@@ -387,14 +387,15 @@ _PUBLIC_ char *cli_credentials_get_principal_and_obtained(struct cli_credentials
 
                cred->principal = NULL;
                if (princ != NULL) {
+                       char *s = NULL;
                        char *p = NULL;
 
-                       cred->principal = talloc_strdup(cred, princ);
-                       if (cred->principal == NULL) {
+                       s = talloc_strdup(cred, princ);
+                       if (s == NULL) {
                                return NULL;
                        }
 
-                       p = strchr(cred->principal, '@');
+                       p = strchr(s, '@');
                        if (p != NULL) {
                                p += 1;
 
@@ -402,6 +403,7 @@ _PUBLIC_ char *cli_credentials_get_principal_and_obtained(struct cli_credentials
                                        *p = toupper(p[0]);
                                }
                        }
+                       cred->principal = s;
                }
 
                if (cred->principal_obtained == CRED_CALLBACK) {
@@ -484,20 +486,24 @@ _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
                /* If `val = NULL` is passed, principal is reset */
                cred->principal = NULL;
                if (val != NULL) {
-                       char *p = strchr(val, '@');
-                       if (p != NULL) {
+                       const char *cp = NULL;
+                       char *s = NULL;
+                       char *p = NULL;
+
+                       cp = strchr(val, '@');
+                       if (cp != NULL) {
                                /* For realm names, only ASCII is allowed */
-                               if (!str_is_ascii(p + 1)) {
+                               if (!str_is_ascii(cp + 1)) {
                                        return false;
                                }
                        }
 
-                       cred->principal = talloc_strdup(cred, val);
-                       if (cred->principal == NULL) {
+                       s = talloc_strdup(cred, val);
+                       if (s == NULL) {
                                return false;
                        }
 
-                       p = strchr(cred->principal, '@');
+                       p = strchr(s, '@');
                        if (p != NULL) {
                                p += 1;
 
@@ -505,6 +511,7 @@ _PUBLIC_ bool cli_credentials_set_principal(struct cli_credentials *cred,
                                        *p = toupper(p[0]);
                                }
                        }
+                       cred->principal = s;
                }
                cred->principal_obtained = obtained;
                cli_credentials_invalidate_ccache(cred, cred->principal_obtained);
index 750dfa21898b121be60169da367d1f367f91db6e..d871f3afb137a923c813578a6865ad0de3370f2d 100644 (file)
@@ -384,10 +384,11 @@ _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, stru
        }
 
        for (i=0; options != NULL; i++) {
-               const char *name = options;
+               char *option = options;
+               const char *name = NULL;
                const char *value = NULL;
 
-               p = strchr(options, ',');
+               p = strchr(option, ',');
                if (p != NULL) {
                        *p = '\0';
                        options = p+1;
@@ -395,7 +396,8 @@ _PUBLIC_ NTSTATUS dcerpc_parse_binding(TALLOC_CTX *mem_ctx, const char *_s, stru
                        options = NULL;
                }
 
-               p = strchr(name, '=');
+               name = option;
+               p = strchr(option, '=');
                if (p != NULL) {
                        *p = '\0';
                        value = p + 1;
index ea94dc1fa14eb052568f2f6a78cbf3105b812ebd..d9165796a6261815a3b3b7538c064b79bd2a4e6f 100644 (file)
@@ -182,14 +182,17 @@ struct tagged_ip {
 */
 static void parse_ip(struct tagged_ip *ip, const char *str)
 {
-       char *s = strchr(str, ':');
-       if (!s) {
+       const char *c = NULL;
+       char *s = NULL;
+
+       c = strchr(str, ':');
+       if (c == NULL) {
                fstrcpy(ip->tag, "*");
                ip->ip = interpret_addr2(str);
                return;
        } 
 
-       ip->ip = interpret_addr2(s+1);
+       ip->ip = interpret_addr2(c+1);
        fstrcpy(ip->tag, str);
        s = strchr(ip->tag, ':');
        if (s) {
index 7e6cea4184f2f950711a5231209822a60e1fa44f..c34fbc165f89ce66c773706d3125149cb9f4fb71 100644 (file)
@@ -1162,6 +1162,7 @@ done:
 ****************************************************************************/
 
 #define strip_driver_path(_mem_ctx, _element) do { \
+       const char *_p = NULL; \
        if (_element && ((_p = strrchr((_element), '\\')) != NULL)) { \
                (_element) = talloc_asprintf((_mem_ctx), "%s", _p+1); \
                W_ERROR_HAVE_NO_MEMORY((_element)); \
@@ -1183,7 +1184,6 @@ static WERROR clean_up_driver_struct_level(TALLOC_CTX *mem_ctx,
        const char *short_architecture;
        int i, cversion;
        WERROR err;
-       char *_p;
 
        if (!*driver_path || !*data_file) {
                return WERR_INVALID_PARAMETER;
index 5f634d152af01c0a96128beacb1b7b48ed88f295..24c57d91f4fec70412b1580b0b73f0d1db0988ca 100644 (file)
@@ -5846,10 +5846,11 @@ int dsdb_search_one(struct ldb_context *ldb,
 /* returns back the forest DNS name */
 const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
 {
-       const char *forest_name = ldb_dn_canonical_string(mem_ctx,
-                                                         ldb_get_root_basedn(ldb));
-       char *p;
+       char *forest_name = NULL;
+       char *p = NULL;
 
+       forest_name = ldb_dn_canonical_string(mem_ctx,
+                                             ldb_get_root_basedn(ldb));
        if (forest_name == NULL) {
                return NULL;
        }
@@ -5865,10 +5866,11 @@ const char *samdb_forest_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
 /* returns back the default domain DNS name */
 const char *samdb_default_domain_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx)
 {
-       const char *domain_name = ldb_dn_canonical_string(mem_ctx,
-                                                         ldb_get_default_basedn(ldb));
+       char *domain_name = NULL;
        char *p;
 
+       domain_name = ldb_dn_canonical_string(mem_ctx,
+                                             ldb_get_default_basedn(ldb));
        if (domain_name == NULL) {
                return NULL;
        }
index c1066e269a14e6aa40767665833e7d225959d655..65752ac45a73771491ed93a8e1b64266cffc6234 100644 (file)
@@ -960,7 +960,8 @@ static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_
        struct ldb_message **result_res = NULL;
        struct ldb_message *result = NULL;
        int i;
-       char *p;
+       char *s = NULL;
+       char *p = NULL;
        struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
 
        const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
@@ -1191,12 +1192,13 @@ static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_
                }
        }
 
-       info1->dns_domain_name = ldb_dn_canonical_string(mem_ctx, result->dn);
-       W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
-       p = strchr(info1->dns_domain_name, '/');
+       s = ldb_dn_canonical_string(mem_ctx, result->dn);
+       W_ERROR_HAVE_NO_MEMORY(s);
+       p = strchr(s, '/');
        if (p) {
                p[0] = '\0';
        }
+       info1->dns_domain_name = s;
 
        /* here we can use result and domain_res[0] */
        switch (format_desired) {
@@ -1539,7 +1541,8 @@ NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx,
 {
        WERROR werr;
        struct drsuapi_DsNameInfo1 info1;
-       char *p;
+       char *s = NULL;
+       char *p = NULL;
 
        /* Handle anonymous bind */
        if (!name || !*name) {
@@ -1568,16 +1571,17 @@ NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx,
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
-       if (*nt4_domain == NULL) {
+       s = talloc_strdup(mem_ctx, info1.result_name);
+       if (s == NULL) {
                return NT_STATUS_NO_MEMORY;
        }
 
-       p = strchr(*nt4_domain, '\\');
+       p = strchr(s, '\\');
        if (!p) {
                return NT_STATUS_INVALID_PARAMETER;
        }
        p[0] = '\0';
+       *nt4_domain = s;
 
        *nt4_account = talloc_strdup(mem_ctx, &p[1]);
        if (*nt4_account == NULL) {
index bc5dadd5bd69187a9864ce6242ec4e144c8ee5f6..a60bbbb4a26347a2a0277f074e387495c1e657de 100644 (file)
@@ -63,6 +63,7 @@ NTSTATUS dcesrv_lsa_get_policy_state(struct dcesrv_call_state *dce_call,
                "fSMORoleOwner",
                NULL
        };
+       char *s;
        char *p;
        int ret;
 
@@ -119,23 +120,25 @@ NTSTATUS dcesrv_lsa_get_policy_state(struct dcesrv_call_state *dce_call,
 
        state->domain_name = lpcfg_sam_name(dce_call->conn->dce_ctx->lp_ctx);
 
-       state->domain_dns = ldb_dn_canonical_string(state, state->domain_dn);
-       if (!state->domain_dns) {
+       s = ldb_dn_canonical_string(state, state->domain_dn);
+       if (!s) {
                return NT_STATUS_NO_SUCH_DOMAIN;
        }
-       p = strchr(state->domain_dns, '/');
+       p = strchr(s, '/');
        if (p) {
                *p = '\0';
        }
+       state->domain_dns = s;
 
-       state->forest_dns = ldb_dn_canonical_string(state, state->forest_dn);
-       if (!state->forest_dns) {
+       s = ldb_dn_canonical_string(state, state->forest_dn);
+       if (!s) {
                return NT_STATUS_NO_SUCH_DOMAIN;
        }
-       p = strchr(state->forest_dns, '/');
+       p = strchr(s, '/');
        if (p) {
                *p = '\0';
        }
+       state->forest_dns = s;
 
        /* work out the builtin_dn - useful for so many calls its worth
           fetching here */