]> git.ipfire.org Git - thirdparty/git.git/commitdiff
global: constify some pointers that are not written to
authorCollin Funk <collin.funk1@gmail.com>
Fri, 6 Feb 2026 01:46:09 +0000 (17:46 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 6 Feb 2026 01:52:49 +0000 (17:52 -0800)
The recent glibc 2.43 release had the following change listed in its
NEWS file:

    For ISO C23, the functions bsearch, memchr, strchr, strpbrk, strrchr,
    strstr, wcschr, wcspbrk, wcsrchr, wcsstr and wmemchr that return
    pointers into their input arrays now have definitions as macros that
    return a pointer to a const-qualified type when the input argument is
    a pointer to a const-qualified type.

When compiling with GCC 15, which defaults to -std=gnu23, this causes
many warnings like this:

    merge-ort.c: In function ‘apply_directory_rename_modifications’:
    merge-ort.c:2734:36: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     2734 |                 char *last_slash = strrchr(cur_path, '/');
          |                                    ^~~~~~~

This patch fixes the more obvious ones by making them const when we do
not write to the returned pointer.

Signed-off-by: Collin Funk <collin.funk1@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
28 files changed:
add-patch.c
apply.c
builtin/commit.c
builtin/receive-pack.c
builtin/remote.c
builtin/shortlog.c
config.c
convert.c
diff.c
diffcore-rename.c
fmt-merge-msg.c
fsck.c
gpg-interface.c
help.c
http-push.c
mailinfo.c
mem-pool.c
merge-ort.c
object-name.c
pack-revindex.c
pkt-line.c
reflog-walk.c
scalar.c
strbuf.c
string-list.c
t/unit-tests/clar/clar/print.h
transport.c
wrapper.c

index 173a53241ebf07b1cfde0988a4d3ac7330d22b6d..70242617ef01e5e7f2957bbebd726cc6adbb87eb 100644 (file)
@@ -342,7 +342,7 @@ static int parse_hunk_header(struct add_p_state *s, struct hunk *hunk)
 {
        struct hunk_header *header = &hunk->header;
        const char *line = s->plain.buf + hunk->start, *p = line;
-       char *eol = memchr(p, '\n', s->plain.len - hunk->start);
+       const char *eol = memchr(p, '\n', s->plain.len - hunk->start);
 
        if (!eol)
                eol = s->plain.buf + s->plain.len;
diff --git a/apply.c b/apply.c
index 3de4aa4d2eaac58a439e70a4db32d11cd078eac0..9de2eb953e51f3539d58656d3c4fba0bea259fa1 100644 (file)
--- a/apply.c
+++ b/apply.c
@@ -4144,7 +4144,7 @@ static int preimage_oid_in_gitlink_patch(struct patch *p, struct object_id *oid)
         */
        struct fragment *hunk = p->fragments;
        static const char heading[] = "-Subproject commit ";
-       char *preimage;
+       const char *preimage;
 
        if (/* does the patch have only one hunk? */
            hunk && !hunk->next &&
index 8e901fe8db79421c7b667edf84abe7f7db0adc6f..03265465480e7cc0e0d4d1c28504437aaa0232ee 100644 (file)
@@ -816,7 +816,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                                  logfile);
                hook_arg1 = "message";
        } else if (use_message) {
-               char *buffer;
+               const char *buffer;
                buffer = strstr(use_message_buffer, "\n\n");
                if (buffer)
                        strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
index 9c491746168a6f0674328b5c4175477034a555c7..e8b6f960faea1455708b206ad1e948a1603f7309 100644 (file)
@@ -393,7 +393,7 @@ struct command {
 static void proc_receive_ref_append(const char *prefix)
 {
        struct proc_receive_ref *ref_pattern;
-       char *p;
+       const char *p;
        int len;
 
        CALLOC_ARRAY(ref_pattern, 1);
index 7ffc14ba15743a3dd4fba00c3d5c6ba7c44d3f31..ace390c671d61c1dc8088cdb56aa8fa73950bf06 100644 (file)
@@ -332,7 +332,7 @@ static int config_read_branches(const char *key, const char *value,
                info->remote_name = xstrdup(value);
                break;
        case MERGE: {
-               char *space = strchr(value, ' ');
+               const char *space = strchr(value, ' ');
                value = abbrev_branch(value);
                while (space) {
                        char *merge;
index b91acf45c8fe59a5ffe5c52df0cee2d3373e9d57..d80bf1a7d055fcaf2ed1f0a07d536d58aa0dab4b 100644 (file)
@@ -76,7 +76,7 @@ static void insert_one_record(struct shortlog *log,
                if (!eol)
                        eol = oneline + strlen(oneline);
                if (starts_with(oneline, "[PATCH")) {
-                       char *eob = strchr(oneline, ']');
+                       const char *eob = strchr(oneline, ']');
                        if (eob && (!eol || eob < eol))
                                oneline = eob + 1;
                }
index 7f6d53b4737cd84633d5ea7fa74f24909a6ac7aa..156f2a24fa00271c6d6adafac16527e674f5ccd4 100644 (file)
--- a/config.c
+++ b/config.c
@@ -160,7 +160,7 @@ static int handle_path_include(const struct key_value_info *kvi,
         * based on the including config file.
         */
        if (!is_absolute_path(path)) {
-               char *slash;
+               const char *slash;
 
                if (!kvi || kvi->origin_type != CONFIG_ORIGIN_FILE) {
                        ret = error(_("relative config includes must come from files"));
index c7d6a85c226db7377eaaf8b82281234109129b47..a34ec6ecdc057e1d5df6c53acced89e23dda2645 100644 (file)
--- a/convert.c
+++ b/convert.c
@@ -1122,7 +1122,8 @@ static int count_ident(const char *cp, unsigned long size)
 static int ident_to_git(const char *src, size_t len,
                        struct strbuf *buf, int ident)
 {
-       char *dst, *dollar;
+       char *dst;
+       const char *dollar;
 
        if (!ident || (src && !count_ident(src, len)))
                return 0;
diff --git a/diff.c b/diff.c
index a68ddd2168ba1c7f949751f30347d75d5e913295..2d92665159d5408b9870c9e8cd9f052a1fe5c16b 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -1961,7 +1961,7 @@ static int fn_out_diff_words_write_helper(struct diff_options *o,
        struct strbuf sb = STRBUF_INIT;
 
        while (count) {
-               char *p = memchr(buf, '\n', count);
+               const char *p = memchr(buf, '\n', count);
                if (print)
                        strbuf_addstr(&sb, diff_line_prefix(o));
 
@@ -3049,7 +3049,7 @@ static long gather_dirstat(struct diff_options *opt, struct dirstat_dir *dir,
                struct dirstat_file *f = dir->files;
                int namelen = strlen(f->name);
                unsigned long changes;
-               char *slash;
+               const char *slash;
 
                if (namelen < baselen)
                        break;
index 7723bc3334e0843d034b4ce85755e20e963bf53e..d9476db35acbf7023bb4555254f367cf4564fd91 100644 (file)
@@ -379,7 +379,7 @@ struct dir_rename_info {
 
 static char *get_dirname(const char *filename)
 {
-       char *slash = strrchr(filename, '/');
+       const char *slash = strrchr(filename, '/');
        return slash ? xstrndup(filename, slash - filename) : xstrdup("");
 }
 
index c9085edc40e934713dff459735fe2839563dfd14..1626667c0dc5c62dc28ab6ecdc94224ee1171773 100644 (file)
@@ -246,7 +246,8 @@ static void add_branch_desc(struct strbuf *out, const char *name)
 static void record_person_from_buf(int which, struct string_list *people,
                                   const char *buffer)
 {
-       char *name_buf, *name, *name_end;
+       char *name_buf;
+       const char *name, *name_end;
        struct string_list_item *elem;
        const char *field;
 
diff --git a/fsck.c b/fsck.c
index 3afec0d0d3828c33a042e667fd8eff11642e8d89..0f02cf8f77bf1a19ddb4322e5982ca3c744d255b 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -1026,7 +1026,7 @@ int fsck_tag_standalone(const struct object_id *oid, const char *buffer,
                        int *tagged_type)
 {
        int ret = 0;
-       char *eol;
+       const char *eol;
        struct strbuf sb = STRBUF_INIT;
        const char *buffer_end = buffer + size;
        const char *p;
index 47222bf31b6ef2ae90d0ea5992ae8d45e9f8228a..377c0cf49f83ad06c7d5ab7dad9745e91529fae7 100644 (file)
@@ -398,7 +398,7 @@ static void parse_ssh_output(struct signature_check *sigc)
 {
        const char *line, *principal, *search;
        char *to_free;
-       char *key = NULL;
+       const char *key = NULL;
 
        /*
         * ssh-keygen output should be:
diff --git a/help.c b/help.c
index 3c36d9c218f82477022920bc6fb7c48e9aae2780..be334d764284a728aac8090cf9027a80c4d8751e 100644 (file)
--- a/help.c
+++ b/help.c
@@ -857,7 +857,7 @@ struct similar_ref_cb {
 static int append_similar_ref(const struct reference *ref, void *cb_data)
 {
        struct similar_ref_cb *cb = (struct similar_ref_cb *)(cb_data);
-       char *branch = strrchr(ref->name, '/') + 1;
+       const char *branch = strrchr(ref->name, '/') + 1;
 
        /* A remote branch of the same name is deemed similar */
        if (starts_with(ref->name, "refs/remotes/") &&
index cc0f80934615ba8f953c6e52f54bcc846c3e7c16..9ae6062198e14fdd81233aa7cf269e7da9964598 100644 (file)
@@ -1768,7 +1768,7 @@ int cmd_main(int argc, const char **argv)
                                usage(http_push_usage);
                }
                if (!repo->url) {
-                       char *path = strstr(arg, "//");
+                       const char *path = strstr(arg, "//");
                        str_end_url_with_slash(arg, &repo->url);
                        repo->path_len = strlen(repo->url);
                        if (path) {
index 99ac596e096e70e11926eed2e930b8f94ddeb032..a2f06dbd96ff6ffbaa4d44fe62a15a66ea11662c 100644 (file)
@@ -1141,7 +1141,7 @@ static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf
 {
        const char *sp = data->buf;
        while (1) {
-               char *ep = strchr(sp, '\n');
+               const char *ep = strchr(sp, '\n');
                int len;
                if (!ep)
                        len = strlen(sp);
index 62441dcc71968f3aaaccdac4dea3cb4ff2f18708..8bc77cb0e80a356be3bc61b30b2e77030ae162dc 100644 (file)
@@ -169,7 +169,7 @@ char *mem_pool_strdup(struct mem_pool *pool, const char *str)
 
 char *mem_pool_strndup(struct mem_pool *pool, const char *str, size_t len)
 {
-       char *p = memchr(str, '\0', len);
+       const char *p = memchr(str, '\0', len);
        size_t actual_len = (p ? p - str : len);
        char *ret = mem_pool_alloc(pool, actual_len+1);
 
index e80e4f735a60f049c68c653946d0a6d407926afd..6f30471b49f1ea4f1d32d18f54954a4e20e322b6 100644 (file)
@@ -2731,7 +2731,7 @@ static void apply_directory_rename_modifications(struct merge_options *opt,
 
        while (1) {
                /* Find the parent directory of cur_path */
-               char *last_slash = strrchr(cur_path, '/');
+               const char *last_slash = strrchr(cur_path, '/');
                if (last_slash) {
                        parent_name = mem_pool_strndup(&opt->priv->pool,
                                                       cur_path,
index 8b862c124e05a9270afa7b83e6f7871f0a8a15cc..e1b09d823c2f803f62a3925bf21384dc873f1c93 100644 (file)
@@ -1756,7 +1756,7 @@ int repo_interpret_branch_name(struct repository *r,
                               struct strbuf *buf,
                               const struct interpret_branch_name_options *options)
 {
-       char *at;
+       const char *at;
        const char *start;
        int len;
 
index 8598b941c8c4191397094eaa852d68cdd7997d05..56cd803a6798d55fab44e11d110ad0bef53a9316 100644 (file)
@@ -544,7 +544,7 @@ static int midx_key_to_pack_pos(struct multi_pack_index *m,
                                struct midx_pack_key *key,
                                uint32_t *pos)
 {
-       uint32_t *found;
+       const uint32_t *found;
 
        if (key->pack >= m->num_packs + m->num_packs_in_base)
                BUG("MIDX pack lookup out of bounds (%"PRIu32" >= %"PRIu32")",
index fc583feb26510d5c62e14e68f8fa65524f9ad0e5..3fc3e9ea7059be7b0a383b2f12afee82010a67e1 100644 (file)
@@ -384,10 +384,10 @@ int packet_length(const char lenbuf_hex[4], size_t size)
                hexval(lenbuf_hex[3]);
 }
 
-static char *find_packfile_uri_path(const char *buffer)
+static const char *find_packfile_uri_path(const char *buffer)
 {
        const char *URI_MARK = "://";
-       char *path;
+       const char *path;
        int len;
 
        /* First char is sideband mark */
@@ -417,7 +417,7 @@ enum packet_read_status packet_read_with_status(int fd, char **src_buffer,
 {
        int len;
        char linelen[4];
-       char *uri_path_start;
+       const char *uri_path_start;
 
        if (get_packet_data(fd, src_buffer, src_len, linelen, 4, options) < 0) {
                *pktlen = -1;
index 4f1ce047498116fb229306845cb90401baf4c247..4dbeaa93a7703fe865c5f73c0506c92aabe0d7f1 100644 (file)
@@ -157,7 +157,8 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
        int recno = -1;
        struct string_list_item *item;
        struct complete_reflogs *reflogs;
-       char *branch, *at = strchr(name, '@');
+       char *branch;
+       const char *at = strchr(name, '@');
        struct commit_reflog *commit_reflog;
        enum selector_type selector = SELECTOR_NONE;
 
index c9df9348ecba46cd5d8afe5d894e34cf04936324..4efb6ac36d888e0ac6de0eb604cfde9d7b7b5996 100644 (file)
--- a/scalar.c
+++ b/scalar.c
@@ -393,7 +393,7 @@ static int delete_enlistment(struct strbuf *enlistment)
 {
        struct strbuf parent = STRBUF_INIT;
        size_t offset;
-       char *path_sep;
+       const char *path_sep;
 
        if (unregister_dir())
                return error(_("failed to unregister repository"));
index 59678bf5b03e0b2c9884283771e23c36224d91c7..3939863cf31ffdaa0293b804b140d6cf26120e38 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
@@ -1119,6 +1119,6 @@ void strbuf_stripspace(struct strbuf *sb, const char *comment_prefix)
 
 void strbuf_strip_file_from_path(struct strbuf *sb)
 {
-       char *path_sep = find_last_dir_sep(sb->buf);
+       const char *path_sep = find_last_dir_sep(sb->buf);
        strbuf_setlen(sb, path_sep ? path_sep - sb->buf + 1 : 0);
 }
index 08dc00984ccbd6e72398d7e58e7a6cb7fa2e39b9..7c34a425da5d0ae47c81cd8ddfb2248b79c19eb9 100644 (file)
@@ -327,7 +327,7 @@ static int split_string(struct string_list *list, const char *string, const char
                BUG("string_list_split() called without strdup_strings");
 
        for (;;) {
-               char *end;
+               const char *end;
 
                if (flags & STRING_LIST_SPLIT_TRIM) {
                        /* ltrim */
index 6a2321b399d192da86e8431120b494e16f57f645..59b7dc14a104a883f8ba15288f8d4ec660ecebb3 100644 (file)
@@ -127,7 +127,7 @@ static void clar_print_tap_error(int num, const struct clar_report *report, cons
 
 static void print_escaped(const char *str)
 {
-       char *c;
+       const char *c;
 
        while ((c = strchr(str, '\'')) != NULL) {
                printf("%.*s", (int)(c - str), str);
index c7f06a7382e605a82316747836300446e5ce74c9..845fd441bec7fa1e67be2075942ce76b2a332714 100644 (file)
@@ -1657,7 +1657,7 @@ int transport_disconnect(struct transport *transport)
  */
 char *transport_anonymize_url(const char *url)
 {
-       char *scheme_prefix, *anon_part;
+       const char *scheme_prefix, *anon_part;
        size_t anon_len, prefix_len = 0;
 
        anon_part = strchr(url, '@');
index b794fb20e71879292a3774c7b1ca6fe8dcdd7737..16f5a63fbb614a1cdc01bdc2421cab36ec3a65f7 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -115,7 +115,7 @@ void *xmemdupz(const void *data, size_t len)
 
 char *xstrndup(const char *str, size_t len)
 {
-       char *p = memchr(str, '\0', len);
+       const char *p = memchr(str, '\0', len);
        return xmemdupz(str, p ? p - str : len);
 }